Overview
nginx is a high-performance web server and reverse proxy. Properly configuring security headers and settings is essential to protect your web applications from common vulnerabilities and attacks. This guide covers essential security configurations for nginx that help protect against:- Information disclosure
- Cross-site scripting (XSS)
- Clickjacking
- MIME type sniffing
- And other web-based attacks
Security Headers Configuration
All the security headers below should be added to your nginx configuration file, typically located at/etc/nginx/nginx.conf or within specific server blocks.
Disable Server Tokens
Prevents nginx from displaying its version number in error pages and HTTP headers, reducing information disclosure.This setting hides the nginx version from potential attackers, making reconnaissance more difficult.Reference: nginx server_tokens documentation
Content Security Policy (CSP)
Defines which resources the browser is allowed to load, providing strong protection against XSS and data injection attacks.This is a basic CSP that only allows resources from the same origin. You’ll likely need to customize this for your application’s specific needs. For example:
X-Frame-Options
Protects against clickjacking attacks by controlling whether the browser should allow the page to be rendered in a frame or iframe.DENY- Prevents any domain from framing the contentSAMEORIGIN- Only allows the current site to frame the contentALLOW-FROM uri- Permits only the specified URI to frame the content (deprecated)
SAMEORIGIN is recommended for most applications. Use DENY if your site should never be framed.Reference: MDN X-Frame-OptionsX-XSS-Protection
Enables the browser’s built-in XSS filter to detect and block reflected XSS attacks.1- Enables XSS filteringmode=block- Blocks the page from rendering if an attack is detected
Referrer-Policy
Controls how much referrer information is included with requests, protecting user privacy.no-referrer- Never send referrer informationstrict-origin- Send only the origin for cross-origin requests (recommended)strict-origin-when-cross-origin- Full URL for same-origin, only origin for cross-originsame-origin- Send referrer for same-origin requests only
strict-origin provides a good balance between privacy and functionality.Reference: MDN Referrer-PolicyPermissions-Policy
Controls which browser features and APIs can be used in the browser, replacing the deprecated Feature-Policy header.geolocation=()- Disables geolocation APIcamera=()- Disables camera accessmicrophone=()- Disables microphone accessfullscreen=(self)- Allows fullscreen only from same originpayment=()- Disables payment request API
Adjust these permissions based on your application’s needs. For example, if you have a video chat application, you’ll need to enable camera and microphone:
X-Content-Type-Options
Prevents browsers from MIME-sniffing a response away from the declared content-type, which can be exploited to execute malicious code.This header ensures that browsers respect the
Content-Type header set by the server, preventing them from guessing the content type.Reference: Wikipedia - Content SniffingComplete Security Headers Example
Here’s a complete example of all security headers combined in an nginx server block:Testing Your Configuration
After applying security headers, test your nginx configuration:Verify Headers
You can verify the headers are being sent correctly using curl:Additional Nginx Security Best Practices
Disable Unnecessary HTTP Methods
Rate Limiting
Protect against DDoS and brute force attacks:Hide nginx in Error Pages
The
more_clear_headers directive requires the headers-more-nginx-module.SSL/TLS Configuration
For modern SSL/TLS security:Additional Resources
- Webdock - Security Headers Guide
- nginx Official Documentation
- OWASP Secure Headers Project
- Mozilla Web Security Guidelines
Security is a continuous process. Regularly review and update your security configurations as new threats emerge and best practices evolve.