- Add
contentTypeNosniff: trueto aheadersmiddleware and list it on the router of your site. Traefik replaces any value the application sends. - Traefik passes on the backend's
Content-Typeunchanged. Behind a default Nginx,.mjsfiles arrive asapplication/octet-streamand nosniff blocks them: fix the type in the backend. - When the backend sends no
Content-Type, Traefik 2.11 filled intext/plainand 3.6 and 3.7 sent none. Both block a script under nosniff.
The short answer
Define a headers middleware with the option and attach it to the router of
your site. In a dynamic configuration file:
http:
middlewares:
nosniff:
headers:
contentTypeNosniff: true
routers:
site:
rule: "Host(`example.com`)"
entryPoints: [websecure]
tls: {}
service: backend
middlewares: [nosniff]With Docker labels:
services:
app:
image: nginx:1.30
labels:
- "traefik.enable=true"
- "traefik.http.routers.app.rule=Host(`example.com`)"
- "traefik.http.routers.app.entrypoints=websecure"
- "traefik.http.routers.app.tls=true"
- "traefik.http.routers.app.middlewares=nosniff"
- "traefik.http.middlewares.nosniff.headers.contentTypeNosniff=true"Every snippet below was run on Traefik 2.11, 3.6 and 3.7, in front of Nginx
containers, and checked with curl on a page, a 404, static files, an
application that sends its own header and an application that was down.
nosniff blocks scripts and stylesheets served with the wrong type, so check
the types your backend sends (step 2). The
X-Content-Type-Options overview explains
what the header blocks and what it can break.
Step 1: which responses get the header
The middleware acts on everything that goes through the router. In our test:
| Response | X-Content-Type-Options |
|---|---|
| A page or a static file from the backend | nosniff |
| A 404 from the backend | nosniff |
A page where the backend sends sniff |
nosniff, once: Traefik replaced the backend’s value |
| A 502 from Traefik when the backend did not answer | nosniff |
| The entry point’s HTTP to HTTPS redirect | Absent |
Unlike Nginx, Apache and Caddy, there is no always flag to remember and no
duplicate to hide behind a proxy. To send the same headers on every router,
attach the middleware to the HTTPS entry point; the
security headers hub for Traefik shows
how.
Step 2: check the types your backend sends
Traefik does not serve files: it passes on the Content-Type the backend
chose. Check the type of each kind of script and stylesheet through Traefik:
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"Behind an nginx:1.30 container with its default configuration, our test
returned application/javascript for app.js but
application/octet-stream for app.mjs, on all three Traefik versions. With
nosniff, browsers refuse to run that module. Fix it in the backend: for
Nginx, the
X-Content-Type-Options guide for Nginx
shows the one-line types block that maps .mjs to text/javascript.
Step 3: when the backend sends no type
Some backends send no Content-Type at all for unknown files. Traefik’s
behaviour then depends on the version:
| Traefik | Content-Type sent to the browser |
|---|---|
| 2.11 | text/plain; charset=utf-8, detected by Traefik |
| 3.6 and 3.7 | None |
On 3.x, the contentType middleware brings the detection back. Neither
outcome helps a script: text/plain and a missing type are both blocked by
nosniff. Set the right type in the backend.
The middleware’s syntax also changed. contentType: {} works on 3.6 and 3.7,
but in our test Traefik 2.11 rejected it with
contentType cannot be a standalone element, dropped the whole file, and
every router in it answered 404. contentType: { autoDetect: true } worked on
all three versions.
Verify it
Check a page, an error page and each kind of static file. Use your own URLs:
for url in https://example.com/ https://example.com/does-not-exist https://example.com/assets/app.js https://example.com/assets/app.mjs; do
echo "$url"
curl -sI "$url" | grep -iE "^(x-content-type-options|content-type):"
doneEach response should print x-content-type-options: nosniff, and each file a
content-type that matches it. Then open your pages with the browser console
for “MIME type mismatch” or “refused to execute” errors, and scan the site
with WebSec0.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No X-Content-Type-Options at all | No router lists the middleware | Add it to each HTTPS router’s middlewares, or to the entry point. |
.mjs scripts blocked as application/octet-stream |
The backend does not know the extension | Map .mjs to text/javascript in the backend. |
A script blocked as text/plain, or with no type |
The backend sends no Content-Type |
Set the type in the backend; detection does not help scripts. |
Every router in a file answers 404 after adding contentType: {} |
Traefik 2.11 does not accept that form | Use contentType: { autoDetect: true }, or upgrade. |
| No header on a 404 for an unknown host | Traefik answers before any router | Expected: the response has no body to protect. |
Server hub
Every header on Traefik
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.
Nginx
Guide ↗Set X-Content-Type-Options: nosniff on Nginx with add_header and always, keep it on static files, and fix the MIME types it breaks, such as .mjs. Tested configs.
Questions
Frequently asked questions
How do I set X-Content-Type-Options in Traefik?
Add contentTypeNosniff: true to a headers middleware, or the label traefik.http.middlewares.<name>.headers.contentTypeNosniff=true, and list the middleware on the router.
Does Traefik keep the application's X-Content-Type-Options?
No. When the middleware sets the header, Traefik replaces the application's value. In our test, an application sending sniff came out with a single nosniff.
Why are my .mjs files blocked behind Traefik?
Traefik sends the Content-Type the backend chose. A default Nginx backend serves .mjs as application/octet-stream, which nosniff blocks for scripts. Map .mjs to text/javascript in the backend.
Can Traefik detect a missing Content-Type?
Traefik 2.11 did it by default in our test, and the contentType middleware does it on 3.x. A script then gets text/plain, which nosniff still blocks, so set the type in the backend instead.
References
Sources
- Traefik Proxy: Headers middlewareTraefik Labs
- Traefik Proxy: ContentType middlewareTraefik Labs
- X-Content-Type-Options headerMDN Web Docs
