Knowledge Base

Nginx Header Examples

Nginx add_header behavior depends on response status and configuration inheritance. Check the relevant server and location blocks together.

A small HTTPS baseline

# Within an existing HTTPS server block:
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

always applies the field regardless of response status. Without it, add_header applies only to specified status codes. Keep valid Content-Type values: nosniff can expose incorrect MIME configuration.

Default inheritance

Under the default inheritance model, a child level inherits parent add_header rules only when the child defines no add_header rules of its own. Adding Cache-Control in a location can therefore remove inherited security headers from that location.

Review every affected location, or explicitly include a shared set of header rules where needed. Nginx 1.29.3 and later also support add_header_inherit, including a merge mode. Use it only after checking the installed version and resulting duplicates.

Upstream headers

add_header adds a field; it does not reliably replace a matching field already sent by an upstream application. For proxied responses, review proxy_hide_header; for FastCGI, review fastcgi_hide_header. Prefer a clear owner instead of stacking contradictory policies.

Validate before reloading

nginx -t

After a successful configuration check, reload through your normal administration process and inspect public success, redirect, and error responses. See duplicate header troubleshooting before suppressing an upstream value.

Sources

Copyright ©2026 MILAN INNOVATIONS LLC. All rights reserved.