متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 194Web · Nginx

How to add security headers in Nginx

A few HTTP response headers make browsers much stricter with your site: always use HTTPS (HSTS), do not guess file types, do not show the page inside other sites' frames (clickjacking), send less referrer data, and load scripts only from places you allow (CSP). Scanners and pen tests check them first. This how-to adds them in one Nginx snippet, avoids the classic add_header inheritance trap, and rolls out a Content Security Policy without breaking the site.

  • 5 الدروس
  • 5 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

194.1

The quick answer

/etc/nginx/snippets/security-headers.conf
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;
/etc/nginx/sites-available/www.example.com (in the HTTPS server block)
server_tokens off;
include snippets/security-headers.conf;
nginx -t && systemctl reload nginx
curl -sI https://www.example.com/ | grep -iE 'strict|content-type-options|frame|referrer|permissions|content-security'

194.2

Step by step

  1. One snippet (quick answer) — the same headers for every site, one place to change. always adds them also to error pages (404, 500).
  2. HSTS — the browser uses only HTTPS for a year. Add it only when HTTPS works for the domain and all subdomains (includeSubDomains). Start with max-age=300 for a test day, then raise it.
  3. The inheritance trap — Nginx uses the add_header lines of a level **only if that level has no add_header of its own**. A location with one add_header Cache-Control … loses all the security headers. Fix: include snippets/security-headers.conf; again inside such locations.
  4. 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.
  5. Then enforce — rename the header to Content-Security-Policy. Avoid 'unsafe-inline' for scripts; move inline scripts to files, or use nonces.
  6. Hide the version — server_tokens off; removes the Nginx version from headers and error pages.
  7. Check from outside — the curl -sI line, or an online header scanner; add the check to your monitoring after changes.

الأوامر في هذا الدرس

194.3

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.

194.4

Common problems

ProblemCauseFix
Headers missing on some URLsA location has its own add_header, so the outer ones are not inherited.Include the snippet in that location too.
Headers missing on error pagesNo always parameter.Add always to each line.
Site looks broken after CSPFonts, scripts or images from other hosts are blocked.Use report-only first; allow the needed hosts.
Same header twiceSet by the app and by Nginx.Set it in one place only (proxy_hide_header for the app's copy).
Subdomain on HTTP stopped workingHSTS with includeSubDomains.Give the subdomain HTTPS; start with a short max-age.
The site must be shown in an iframe of a partnerX-Frame-Options DENY.Use CSP frame-ancestors 'self' https://partner.example.com instead.

194.5

Go further

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.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل