هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" always;server_tokens off;
include snippets/security-headers.conf;Step by step
- One snippet (quick answer) — the same headers for every site, one place to change.
alwaysadds them also to error pages (404, 500). - HSTS — the browser uses only HTTPS for a year. Add it only when HTTPS works for the domain and all subdomains (
includeSubDomains). Start withmax-age=300for a test day, then raise it. - The inheritance trap — Nginx uses the
add_headerlines of a level **only if that level has noadd_headerof its own**. Alocationwith oneadd_header Cache-Control …loses all the security headers. Fix:include snippets/security-headers.conf;again inside such locations. - CSP in report-only mode first (quick answer) — the browser only reports what it would block (developer console → "Report Only"). Open every page type, collect the outside hosts you really need (fonts, analytics, CDN), add them, for example
script-src 'self' https://cdn.example.net. - Then enforce — rename the header to
Content-Security-Policy. Avoid'unsafe-inline'for scripts; move inline scripts to files, or use nonces. - Hide the version —
server_tokens off;removes the Nginx version from headers and error pages. - Check from outside — the
curl -sIline, or an online header scanner; add the check to your monitoring after changes.
Check that it worked
curl -sI shows all six headers on the home page and on a 404 page (curl -sI https://www.example.com/does-not-exist); the site works without CSP errors in the browser console; nginx -t reports syntax is ok and test is successful.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Headers missing on some URLs | A location has its own add_header, so the outer ones are not inherited. | Include the snippet in that location too. |
| Headers missing on error pages | No always parameter. | Add always to each line. |
| Site looks broken after CSP | Fonts, scripts or images from other hosts are blocked. | Use report-only first; allow the needed hosts. |
| Same header twice | Set by the app and by Nginx. | Set it in one place only (proxy_hide_header for the app's copy). |
| Subdomain on HTTP stopped working | HSTS with includeSubDomains. | Give the subdomain HTTPS; start with a short max-age. |
| The site must be shown in an iframe of a partner | X-Frame-Options DENY. | Use CSP frame-ancestors 'self' https://partner.example.com instead. |
Go further
- Command pages: nginx, curl.
- Next how-tos: Redirect HTTP to HTTPS in Nginx, Check the TLS settings of a website, Rate-limit requests in Nginx, Scan a website with OWASP ZAP.
- Learn it: Traffic tools at work — Nginx, Web services — Nginx. Full guide: Nginx.
1Why do security headers disappear in some Nginx locations?
A location with its own add_header does not inherit the add_header lines from the server block; include the snippet again there.
2Why start a Content Security Policy in report-only mode?
The browser only reports what it would block, so you can find all needed sources before the policy breaks the site.