Nginx Security Headers: Complete Configuration Guide
Why Security Headers Matter
Security headers protect your website from common attacks: XSS, clickjacking, MIME sniffing, and more. They also contribute to your site's trust signals — Google confirmed HTTPS as a ranking factor, and security headers build on that foundation.
The Essential Headers
1. Strict-Transport-Security (HSTS)
Forces browsers to use HTTPS. Prevents downgrade attacks.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
**max-age=31536000** = 1 year. Browser remembers to always use HTTPS.
2. Content-Security-Policy (CSP)
Controls which resources can load on your page. Prevents XSS.
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self';" always;
3. X-Frame-Options
Prevents your site from being embedded in iframes (clickjacking).
add_header X-Frame-Options "DENY" always;
4. X-Content-Type-Options
Prevents MIME type sniffing.
add_header X-Content-Type-Options "nosniff" always;
5. Referrer-Policy
Controls how much URL info is sent when users navigate away.
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
6. Permissions-Policy
Restricts browser features (camera, microphone, etc).
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
Complete Nginx Configuration
server {
listen 443 ssl http2;
server_name yourdomain.com;
# Security Headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';" always;
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
# Hide server info
server_tokens off;
}
How to Check Your Headers
Use [SEO Snapshot](/) to check all 7 security headers + get a security grade from A+ to F. We also detect cookie security flags, SRI, and mixed content.
FAQ
**Q: Do security headers affect SEO?**
A: HTTPS is a confirmed ranking factor. Other headers don't directly affect rankings but improve trust and prevent attacks.
**Q: What if I break my site with CSP?**
A: Start with CSP in report-only mode, then tighten gradually.
Check your site's SEO score for free
Analyze your site