Nginx Server Block Missing X-Frame-Options & X-Content-Type-Options
Checks Nginx server { ... } configuration blocks to verify that baseline clickjacking (X-Frame-Options) and MIME-sniffing (X-Content-Type-Options) defense headers are configured.
- Rule Slug
- nginx-missing-security-headers
- Affected Files
- *nginx*.conf, *.nginx, default.conf, sites-available/*, sites-enabled/*, conf.d/*.conf
- Detection Method
- Deterministic Regex
Vulnerable Pattern vs. Hardened Fix
# nginx/default.conf
server {
listen 80;
server_name app.example.com;
# MEDIUM RISK: Missing X-Frame-Options and X-Content-Type-Options
location / {
proxy_pass http://127.0.0.1:3000;
}
}# nginx/default.conf
server {
listen 443 ssl http2;
server_name app.example.com;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
proxy_pass http://127.0.0.1:3000;
}
}Why This Fails in Production
Nginx serves as the edge reverse proxy and static file server for millions of production applications, yet its default configuration ships without baseline browser security headers. When a server { ... } block omits X-Frame-Options (or a Content-Security-Policy frame-ancestors directive) and X-Content-Type-Options: nosniff, every HTML page, dashboard, and user-uploaded asset served through that block loses vital browser-enforced exploit mitigations.
Without X-Frame-Options: DENY or SAMEORIGIN, attackers can load your authenticated application inside a transparent <iframe> on a malicious site and execute clickjacking (UI redressing) attacks, tricking logged-in users into clicking invisible "Delete Account", "Approve Transfer", or "Enable API Key" buttons while believing they are interacting with an innocent page.
Without X-Content-Type-Options: nosniff, browsers may ignore your server’s declared Content-Type header and "sniff" file bytes to interpret a user-uploaded text file, avatar image, or CSV download as executable text/html or application/javascript, turning a harmless file upload endpoint into a stored Cross-Site Scripting (XSS) vulnerability.
One subtle Nginx pitfall to watch for when adding headers: Nginx’s add_header directive does NOT inherit from a parent server block into a child location block if that location block defines any add_header directive of its own. Always include the always parameter on server-level security headers and audit child location blocks carefully.
Remediation Steps
- 1Add add_header X-Frame-Options "SAMEORIGIN" always; and add_header X-Content-Type-Options "nosniff" always; inside your Nginx server { ... } block.
- 2Also configure Referrer-Policy ("strict-origin-when-cross-origin"), Strict-Transport-Security (for HTTPS), and a Content-Security-Policy matching your app’s assets.
- 3Include the always keyword so security headers are emitted on 4xx and 5xx error responses as well as 2xx/3xx responses.
- 4Check nested location blocks: if any location defines its own add_header (such as Cache-Control), include a shared security-headers.conf snippet so security headers are not silently dropped.
Build a production-ready Nginx reverse proxy config with security headers and WebSocket support on OpsCheck.
Frequently Asked Questions
- Why does Nginx require the "always" parameter on add_header?
- Without always, Nginx only emits add_header on 200, 201, 204, 206, 301, 302, 303, 304, 307, and 308 responses, omitting security headers on 400, 404, and 500 error pages.
- Does Content-Security-Policy frame-ancestors replace X-Frame-Options?
- CSP frame-ancestors is the modern successor and offers finer control, but keeping X-Frame-Options: SAMEORIGIN (or DENY) alongside CSP ensures compatibility with legacy browsers and automated security scanners.
- Why did my headers disappear on /static/ even though they are in the server block?
- In Nginx, if a location block contains even one add_header directive (like Cache-Control), it discards all add_header directives inherited from the parent server block. Put shared headers in an include file.