- Add
add_header X-Content-Type-Options "nosniff" always;to theserverblock. Withoutalways, 404 and 500 pages are sent without it. - Static file locations that set
Cache-Controldrop the header, and they are the ones that serve your scripts. Share it through anincludefile, or useadd_header_inherit merge;on nginx 1.29.3 or later. - The default
mime.typeshas no.mjsentry, so Nginx serves it asapplication/octet-stream. Addtext/javascript mjs;in atypesblock next to theinclude. - A
typesblock inside alocationreplaces the whole MIME table there:.jsfiles becomeapplication/octet-streamand stop running with nosniff.
The short answer
Add the header to the server block of your site:
server {
listen 443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/ssl/example.com/fullchain.pem;
ssl_certificate_key /etc/ssl/example.com/privkey.pem;
add_header X-Content-Type-Options "nosniff" always;
}sudo nginx -t && sudo systemctl reload nginxalways keeps the header on error pages. Every snippet below was run on
nginx 1.28, 1.30 and 1.31 and checked with curl, on a page, a 404, a
JavaScript file and a nested location.
nosniff blocks scripts and stylesheets served with the wrong type, so check
your MIME types first (step 1). The
X-Content-Type-Options overview explains
what the header blocks and what it can break.
Step 1: check your MIME types
Nginx picks the Content-Type from the file extension, using the types
table loaded by include mime.types;, and falls back to default_type,
usually application/octet-stream. With nosniff, a script served with that
fallback stops running.
Check the type of each kind of script and stylesheet you serve:
curl -sI https://example.com/assets/app.js | grep -i "^content-type"
curl -sI https://example.com/assets/app.mjs | grep -i "^content-type"
curl -sI https://example.com/assets/app.css | grep -i "^content-type"The mime.types file shipped with nginx 1.28, 1.30 and 1.31 maps .js to
application/javascript, .css to text/css and .wasm to
application/wasm, but has no entry for .mjs. Our test returned:
HTTP/1.1 200 OK
Content-Type: application/octet-streamAdd the missing type once, in the http block, after the include:
http {
include /etc/nginx/mime.types;
types {
text/javascript mjs;
}
default_type application/octet-stream;
# ...
}The second types block adds to the table: in our test .mjs became
text/javascript while .js and .css kept their types. Module scripts
need this even without nosniff, since browsers always check their type.
Step 2: do not put types inside a location
A types block inside a location does not add to the table, it replaces
it for that location. This looks harmless:
location /wasm/ {
types {
application/wasm wasm;
}
}But in our test, /wasm/app.js was then served as
application/octet-stream, because .js was no longer in the table, and
nosniff blocks it. Declare extra types at http level, as in step 1.
Step 3: keep it on error pages
Without always, Nginx only adds headers to 200, 201, 204, 206, 301, 302, 303,
304, 307 and 308 responses. Our test with
add_header X-Content-Type-Options "nosniff";:
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
HTTP/1.1 404 Not Found
(no X-Content-Type-Options header)Keep always on the line.
Step 4: keep it on static files
add_header directives are inherited from the parent block only if the
current block has none. The location that serves your scripts usually sets
cache headers, and loses nosniff there:
server {
listen 443 ssl;
server_name example.com;
# ssl_certificate lines...
add_header X-Content-Type-Options "nosniff" always;
location /assets/ {
# This one line removes the server-level header for /assets/.
add_header Cache-Control "public, max-age=31536000, immutable" always;
}
}Our test returned only Cache-Control for /assets/app.js: the page had the
header, the scripts did not. The fix that works on every version is a snippet
you include wherever a block sets its own headers:
# /etc/nginx/snippets/security-headers.conf
add_header X-Content-Type-Options "nosniff" always;server {
listen 443 ssl;
server_name example.com;
# ssl_certificate lines...
include /etc/nginx/snippets/security-headers.conf;
location /assets/ {
add_header Cache-Control "public, max-age=31536000, immutable" always;
include /etc/nginx/snippets/security-headers.conf;
}
}On nginx 1.29.3 or later, which includes the 1.30 stable branch,
add_header_inherit merge; makes nested blocks add their headers to the
parent’s instead of replacing them:
server {
listen 443 ssl;
server_name example.com;
# ssl_certificate lines...
add_header_inherit merge;
add_header X-Content-Type-Options "nosniff" always;
location /assets/ {
# Sent together with the server-level header.
add_header Cache-Control "public, max-age=31536000, immutable" always;
}
}nginx 1.28 rejects it with unknown directive "add_header_inherit". The
Permissions-Policy guide for Nginx and
the Referrer-Policy guide for Nginx use the
same snippet for their headers.
Step 5: one header behind a reverse proxy
When Nginx proxies an application that already sends the header and adds its own line, the response carries two:
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniffThis is harmless: browsers read the first value, and in our Chrome test two
nosniff headers worked like one. A different value from the application
would win, though, because the upstream’s header comes first. Keep one owner:
location / {
proxy_pass http://app;
proxy_hide_header X-Content-Type-Options;
add_header X-Content-Type-Options "nosniff" always;
}Use fastcgi_hide_header for PHP-FPM and uwsgi_hide_header for uWSGI.
Also check that the application sends a correct Content-Type for any
JavaScript it generates: Nginx passes it through unchanged.
Verify it
Check a page, an error page, a script and a stylesheet. Use your own URLs:
curl -sI https://example.com/ | grep -i "^x-content-type-options"
curl -sI https://example.com/does-not-exist | grep -i "^x-content-type-options"
curl -sI https://example.com/assets/app.js | grep -iE "^x-content-type-options|^content-type"
curl -sI https://example.com/assets/app.css | grep -iE "^x-content-type-options|^content-type"Each should print x-content-type-options: nosniff, and the scripts and
stylesheets a JavaScript type or text/css. Then open your pages with the
browser console and look for “Refused to execute script” or “Refused to apply
style”. Finally, scan the site with WebSec0; it checks the page only, so the
curl checks on static files still matter.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Header missing on 404 or 500 pages | add_header without always |
Add always to the line. |
Header on pages but not on /assets/ |
That location has its own add_header |
Include the snippet there, or use add_header_inherit merge; (1.29.3+). |
| “Refused to execute script … MIME type (‘application/octet-stream’)” | Extension missing from mime.types, such as .mjs |
Add it in a types block at http level. |
Scripts in one location served as application/octet-stream |
A types block inside that location |
Move the type to http level. |
| “Refused to execute script … MIME type (‘text/html’)” | The URL returns an error page or the application sets the wrong type | Fix the path, or the application’s Content-Type. |
| WebSec0 fails a header that is present | A first value other than nosniff, such as a stray value from the upstream |
Send exactly nosniff, and hide the upstream’s header. |
unknown directive "add_header_inherit" |
nginx older than 1.29.3 | Upgrade, or use the include approach. |
Server hub
Every header on Nginx
Other web servers
X-Content-Type-Options on other servers
Apache
Guide ↗Set X-Content-Type-Options: nosniff on Apache with Header always set, keep it on errors and redirects, and fix files Apache sends without a Content-Type. Tested configs.
Caddy
Guide ↗Set X-Content-Type-Options: nosniff on Caddy with the header directive, check the MIME types file_server sends and type files without an extension. Tested configs.
Traefik
Guide ↗Set X-Content-Type-Options: nosniff on Traefik with the contentTypeNosniff option, in labels or a file, and fix the Content-Type your backend sends. Tested configs.
Questions
Frequently asked questions
What is the Nginx line for X-Content-Type-Options?
add_header X-Content-Type-Options "nosniff" always; in the server block, or in a snippet included there. Then run nginx -t and reload.
Why is X-Content-Type-Options missing on my JavaScript files?
The location that serves them sets its own add_header, usually Cache-Control, and stops inheriting the server-level headers. Include your security headers snippet there, or use add_header_inherit merge; on nginx 1.29.3 or later.
Why are my .mjs files blocked after adding nosniff?
The mime.types file shipped with nginx 1.28 to 1.31 has no .mjs entry, so Nginx sends application/octet-stream. Add types { text/javascript mjs; } in the http block after include mime.types;. Module scripts need the right type even without nosniff.
Can I add a MIME type inside a location?
Not with a types block: it replaces the whole table for that location, so every other extension falls back to default_type. Add the type once at http level, next to include mime.types;.
My response has two X-Content-Type-Options headers. Is that a problem?
Two nosniff headers are harmless: browsers read the first value. Still keep one owner, with proxy_hide_header X-Content-Type-Options; if Nginx adds the header itself.
References
Sources
- X-Content-Type-Options headerMDN Web Docs
