Cybrove
Platform Guide

How to Add Security Headers to Nginx

Add CSP, HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and Permissions-Policy to Nginx. Copy-paste configuration included.

The 6 Essential Security Headers

Content-Security-Policy (CSP)

Controls which sources of scripts, styles, and resources are allowed. Prevents XSS by blocking inline scripts and unauthorized external scripts.

Strict-Transport-Security (HSTS)

Forces browsers to always use HTTPS, preventing protocol downgrade attacks and cookie hijacking over HTTP.

X-Frame-Options

Prevents your page from being loaded in an iframe, blocking clickjacking attacks.

X-Content-Type-Options

Prevents browsers from MIME-sniffing the content type, stopping attacks that rely on content type confusion.

Referrer-Policy

Controls how much referrer information is sent with requests, protecting user privacy and preventing information leakage.

Permissions-Policy

Controls which browser features (camera, microphone, geolocation) your page can use, reducing attack surface.

Nginx Configuration

Add this to /etc/nginx/sites-available/your-site or within the server block in nginx.conf:

server {
    # Security Headers
    add_header X-Frame-Options "DENY" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self';" always;
    add_header X-XSS-Protection "1; mode=block" always;
}

Use 'always' to ensure headers are sent on error pages too. Place headers in the server block for global coverage. Reload nginx after changes: sudo nginx -t && sudo systemctl reload nginx

How to Test Your Headers

curl -I https://yourdomain.com

Or use Cybrove's free security check to automatically validate all security headers and get specific fix recommendations.

Frequently Asked Questions

How do I add security headers to Nginx?

Add security headers to Nginx by modifying /etc/nginx/sites-available/your-site or within the server block in nginx.conf. The essential headers are CSP, HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and Permissions-Policy. See the configuration example above.

How do I test if my Nginx security headers are working?

Run: curl -I https://yourdomain.com and check the response headers. Or use Cybrove's free security check at cybrove.com/scan which automatically validates all security headers and reports which are missing.

Which security headers are most important for Nginx?

CSP (prevents XSS), HSTS (forces HTTPS), and X-Frame-Options (prevents clickjacking) are the most critical. X-Content-Type-Options and Referrer-Policy provide additional protection with minimal configuration effort.

Check if your Nginx headers are configured correctly

Free security check validates all 6 headers plus SSL, DNS, and email authentication.

Free Security Check