brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: add_header inside a nginx location drops the headers from the server block

Nginx
~/tips/tip-nginx-add-header-inheritance

add_header X-Content-Type-Options nosniff always; # server block

Explains why headers set in the server block disappear inside a location with its own add_header.

Security headers set once at server level vanish on any location that adds a header of its own.

add_header X-Content-Type-Options nosniff always;   # server block
location /downloads/ {
    add_header Cache-Control "no-store" always;   # parent header is dropped here
}

add_header is inherited from the level above only when the current level has no add_header of its own. In /downloads/ the X-Content-Type-Options header is therefore gone, so repeat it there or keep shared headers in a file and include it in each block. always adds the header to error responses too. Without it nginx only sends it on 200, 201, 204, 206, 301, 302, 303, 304, 307 and 308.

Further reading: ngx_http_headers_module: add_header

Going deeper: ๐Ÿ›œ Guide: AdGuard Home + Tailscale (Secure Split-Horizon DNS)

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link