Secure headers and privacy
Start with a minimal browser policy
Security headers constrain browser behavior. Content-Security-Policy (CSP) restricts sources and capabilities; frame-ancestors controls which pages may embed this page. X-Frame-Options DENY supports older framing protection, and X-Content-Type-Options nosniff limits MIME sniffing in relevant browser fetches. A simple static site without external scripts can start with this restrictive policy. It is not a universal policy for applications: test scripts, styles, frames, forms and accessibility before enforcing. CSP does not replace output encoding or authorization.
add_header Content-Security-Policy "default-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'" always;add_header X-Frame-Options DENY always;add_header X-Content-Type-Options nosniff always;add_header Referrer-Policy no-referrer always;Apply transport policy only where appropriate
HSTS tells supporting browsers to use HTTPS for later visits after receiving the policy over HTTPS. It is not a TLS configuration and does not protect an unlearned first visit by itself. Start with a small max-age on a verified HTTPS host, then expand after assessing all subdomains and recovery. Do not enable includeSubDomains or preload casually. The next fragment belongs only in a verified TLS server; repeat or include the other security headers at the same level when needed.
add_header Strict-Transport-Security "max-age=300" always;Test inheritance and error responses
The always parameter extends header addition to status codes outside the usual set. Under traditional inheritance, defining any add_header at a child level replaces the inherited add_header list. Use a shared reviewed include for repeated policies. Newer builds from 1.29.3 support add_header_inherit merge, but the older lab binary does not. Avoid duplicate policy fields from proxy and application; decide which layer owns each header. For a sensitive endpoint, use no-store and disable shared proxy caching; neither undoes copies already made or replaces authorization. The snippet assumes a reviewed fixed upstream and that the proxy owns the cache header.
location /account/ { proxy_pass http://127.0.0.1:19095; proxy_cache off; proxy_hide_header Cache-Control; include conf/security-headers.conf; add_header Cache-Control "no-store" always;}Minimize log and error information
Avoid collecting authorization fields, cookies, full query strings or request bodies in ordinary access logs. Even paths and identifiers may be sensitive. Prefer an intentionally small log schema, restricted file access, retention and redaction. Error logs may still contain request details: protect and review them separately. server_tokens off reduces version disclosure but does not fix a vulnerability or guarantee product-name removal. The following http-context format records status, method and timing rather than a full request line; this is a data minimization starting point, not a complete privacy policy.
log_format minimal 'method=$request_method status=$status seconds=$request_time';access_log logs/access.log minimal;server_tokens off;Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Secure headers and privacy
Choose application-specific security headers and privacy-preserving logs, and verify both normal and error responses without weakening backend controls.
Which CSP directive controls embedding ancestors?
Where should HSTS be delivered?
What does a local add_header do under traditional inheritance?
Which access log is least likely to include query secrets?
What does no-store not do?