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

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

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/nginx/
Updated: 2026-10-16
Tested on: nginx 1.28, 1.30 and 1.31

## In short

- Add `add_header X-Content-Type-Options "nosniff" always;` to the `server` block. Without `always`, 404 and 500 pages are sent without it.
- Static file locations that set `Cache-Control` drop the header, and they are the ones that serve your scripts. Share it through an `include` file, or use `add_header_inherit merge;` on nginx 1.29.3 or later.
- The default `mime.types` has no `.mjs` entry, so Nginx serves it as `application/octet-stream`. Add `text/javascript mjs;` in a `types` block next to the `include`.
- A `types` block inside a `location` replaces the whole MIME table there: `.js` files become `application/octet-stream` and stop running with nosniff.

## The short answer

Add the header to the `server` block of your site:

```nginx
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;
}
```

```sh
sudo nginx -t && sudo systemctl reload nginx
```

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

```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"
```

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
HTTP/1.1 200 OK
Content-Type: application/octet-stream
```

Add the missing type once, in the `http` block, after the `include`:

```nginx
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:

```nginx
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
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:

```nginx
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:

```nginx
# /etc/nginx/snippets/security-headers.conf
add_header X-Content-Type-Options "nosniff" always;
```

```nginx
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:

```nginx
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](/guides/permissions-policy/nginx/) and
the [Referrer-Policy guide for Nginx](/guides/referrer-policy/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
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
```

This 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:

```nginx
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:

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

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

## Sources

- [Module ngx_http_headers_module: add_header and add_header_inherit](https://nginx.org/en/docs/http/ngx_http_headers_module.html), nginx.org
- [Module ngx_http_core_module: types and default_type](https://nginx.org/en/docs/http/ngx_http_core_module.html#types), nginx.org
- [Module ngx_http_proxy_module: proxy_hide_header](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header), nginx.org
- [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
