# How to set X-Content-Type-Options on Traefik

> 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.

Author: Joshua Martinelle, Security engineer at Tenable (https://www.websec0.com/guides/authors/joshua-martinelle/)
Canonical: https://www.websec0.com/guides/x-content-type-options/traefik/
Updated: 2026-10-26
Tested on: Traefik 2.11, 3.6 and 3.7

## In short

- Add `contentTypeNosniff: true` to a `headers` middleware and list it on the router of your site. Traefik replaces any value the application sends.
- Traefik passes on the backend's `Content-Type` unchanged. Behind a default Nginx, `.mjs` files arrive as `application/octet-stream` and nosniff blocks them: fix the type in the backend.
- When the backend sends no `Content-Type`, Traefik 2.11 filled in `text/plain` and 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:

```yaml
http:
  middlewares:
    nosniff:
      headers:
        contentTypeNosniff: true

  routers:
    site:
      rule: "Host(`example.com`)"
      entryPoints: [websecure]
      tls: {}
      service: backend
      middlewares: [nosniff]
```

With Docker labels:

```yaml
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](/guides/x-content-type-options/) 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](/guides/security-headers/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:

```sh
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](/guides/x-content-type-options/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:

```sh
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):"
done
```

Each 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. |

## 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.

## Sources

- [Traefik Proxy: Headers middleware](https://doc.traefik.io/traefik/middlewares/http/headers/), Traefik Labs
- [Traefik Proxy: ContentType middleware](https://doc.traefik.io/traefik/middlewares/http/contenttype/), Traefik Labs
- [Fetch Standard: X-Content-Type-Options header](https://fetch.spec.whatwg.org/#x-content-type-options-header), WHATWG
- [RFC 9239: Updates to ECMAScript Media Types](https://www.rfc-editor.org/rfc/rfc9239.html), IETF
- [X-Content-Type-Options header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Content-Type-Options), MDN Web Docs
