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

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

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/caddy/
Updated: 2026-10-26
Tested on: Caddy 2.9, 2.10 and 2.11

## In short

- Add `header X-Content-Type-Options "nosniff"` to the site block. Caddy sends it on its own 404 pages, redirects and 502s without any extra flag.
- `file_server` serves `.js` and `.mjs` as `text/javascript`, `.css` as `text/css` and `.wasm` as `application/wasm`, so common files keep working with nosniff.
- A file without an extension is sent with no `Content-Type`. If it is a script, nosniff blocks it: set the type with `header <path> Content-Type "text/javascript"`.
- Behind `reverse_proxy`, Caddy's header comes first, which is the one browsers read. Add `header_down -X-Content-Type-Options` to send one.

## The short answer

Add the header to the site block in your Caddyfile:

```caddyfile
example.com {
	header X-Content-Type-Options "nosniff"
	root * /var/www/example
	file_server
}
```

```sh
caddy validate --config /etc/caddy/Caddyfile && sudo systemctl reload caddy
```

Every snippet below was run on Caddy 2.9, 2.10 and 2.11 and checked with
`curl`, on a page, a 404, a redirect, static files of each type, a proxied
application and an application that was down.

`nosniff` blocks scripts and stylesheets served with the wrong type, so check
your MIME types (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

`file_server` picks the `Content-Type` from the file extension. 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"
```

What our test returned, the same on Caddy 2.9, 2.10 and 2.11:

| File | `Content-Type` |
| --- | --- |
| `app.js` | `text/javascript; charset=utf-8` |
| `app.mjs` | `text/javascript; charset=utf-8` |
| `app.css` | `text/css; charset=utf-8` |
| `app.wasm` | `application/wasm` |
| `site.webmanifest` | `application/manifest+json` |
| A file without an extension | None |

Unlike Nginx, whose default table has no `.mjs` entry, Caddy serves module
scripts with the right type out of the box.

## Step 2: give a type to files without an extension

For a file without an extension, `file_server` sends **no `Content-Type`
header**, even on a `GET`. With `nosniff`, browsers then refuse to run that
response as a script or apply it as a stylesheet. This hits scripts served
from URLs such as `/assets/loader`.

Set the type with a `header` line for that path:

```caddyfile
example.com {
	header X-Content-Type-Options "nosniff"
	header /assets/loader Content-Type "text/javascript"
	root * /var/www/example
	file_server
}
```

Our test then returned a single `Content-Type: text/javascript` for
`/assets/loader`, and `file_server` did not change it. Other files kept their
own types.

## Step 3: which responses get the header

A plain `header` line acts on every response of the site. In our test:

| Response | X-Content-Type-Options |
| --- | --- |
| A page or a static file from `file_server` | Present |
| A 404 from `file_server` | Present |
| A `301` from a `redir` in the site | Present |
| A file under `/assets/` with its own `header /assets/* Cache-Control …` line | Present |
| A 502 when the proxied application was down | Present |
| Caddy's automatic HTTP to HTTPS redirect | Absent |

Unlike Nginx and Apache, there is no `always` flag to remember. To send the
same headers from several sites, put them in a snippet that each site
imports; the [security headers hub for Caddy](/guides/security-headers/caddy/)
shows one snippet with all six security headers.

## Step 4: one header behind reverse_proxy

A `header` line runs before the proxy, and Caddy keeps the application's
header after its own. Our test, with an application sending an invalid value:

```http
HTTP/2 200
x-content-type-options: nosniff
x-content-type-options: sniff
```

Browsers read only the first value, as the
[Fetch Standard](https://fetch.spec.whatwg.org/#x-content-type-options-header)
specifies, and Caddy's comes first, so `nosniff` applies. WebSec0 reads the
first value too and passes it. Keep one header anyway:

```caddyfile
example.com {
	header X-Content-Type-Options "nosniff"
	reverse_proxy 127.0.0.1:8080 {
		header_down -X-Content-Type-Options
	}
}
```

Our test then returned a single `nosniff`, on the application's pages and
404s. Also check that the application sends a correct `Content-Type` for any
JavaScript it generates: Caddy passes it through unchanged.

## 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.css; 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. A script with no `content-type` line will be
blocked. 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 | Caddy adds none by default | Add the `header` line to every site, or import a snippet. |
| A script is blocked, and its response has no `Content-Type` | The file has no extension | Add `header <path> Content-Type "text/javascript"`. |
| A script from the application is blocked | The application sends the wrong `Content-Type` | Fix the type in the application. |
| Two `X-Content-Type-Options` headers | The proxied application sends one too | Add `header_down -X-Content-Type-Options` inside `reverse_proxy`. |
| WebSec0 fails a header that is present | The first value is not `nosniff` | Keep only Caddy's header. |
| Changes do not show up | Reload skipped, or a CDN cached old headers | `caddy validate` and reload, then purge the CDN. |

## Frequently asked questions

### What is the Caddyfile line for X-Content-Type-Options?

`header X-Content-Type-Options "nosniff"` in the site block, or in a snippet every site imports. Check the configuration with `caddy validate` and reload.

### Will nosniff break my JavaScript on Caddy?

Not for `.js` or `.mjs` files: Caddy 2.9 to 2.11 serve both as `text/javascript`. It breaks scripts served from a URL without an extension, which `file_server` sends with no `Content-Type`.

### How do I set the MIME type of a file in Caddy?

Add a `header` line with a path matcher before `file_server`, for example `header /assets/loader Content-Type "text/javascript"`. In our test, `file_server` kept that type.

### My response has two X-Content-Type-Options headers. Is that a problem?

Browsers read the first value only, and behind `reverse_proxy` Caddy's header came first in our test. Keep one anyway, with `header_down -X-Content-Type-Options` inside the proxy block.

### Does Caddy send X-Content-Type-Options by default?

No. Caddy sends no security headers of its own. Add the line to every site that serves pages or static files.

## Sources

- [Caddyfile directive: header](https://caddyserver.com/docs/caddyfile/directives/header), Caddy
- [Caddyfile directive: file_server](https://caddyserver.com/docs/caddyfile/directives/file_server), Caddy
- [Caddyfile directive: reverse_proxy (header_down)](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#headers), Caddy
- [Fetch Standard: X-Content-Type-Options header](https://fetch.spec.whatwg.org/#x-content-type-options-header), WHATWG
- [X-Content-Type-Options header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Content-Type-Options), MDN Web Docs
