How to add the X-Content-Type-Options nosniff header

Send X-Content-Type-Options with the value nosniff so browsers trust each response's declared type instead of guessing.

Last reviewed

Why it matters

Without nosniff, a browser may treat an uploaded or user-supplied file as HTML or script even though it was served as something else, which can turn a file upload into script running on the site. With it, the browser uses the declared Content-Type and blocks scripts and stylesheets of the wrong type.

How to fix it

The value is always nosniff.

Replace the highlighted values: www.example.com with the site's hostname.

Cloudflare

Option A: SSL/TLS > Edge Certificates > HTTP Strict Transport Security (HSTS), and turn No-Sniff Header on. It sits inside the HSTS panel, so use this only if you also want HSTS.

Option B: Rules > Overview > Create rule > Response Header Transform Rule, name it, apply it to all incoming requests, choose Set static with Header name X-Content-Type-Options and Value nosniff, and Deploy.

Cloudflare's "Add security headers" managed transform also sends nosniff, but adds other headers with it; the explicit rule is clearer.

Sources: Cloudflare HSTS settings, create a response header rule, managed transforms.

nginx

add_header X-Content-Type-Options "nosniff" always;

A location block with its own add_header drops the inherited ones: repeat the line there.

Source: ngx_http_headers_module.

Apache

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
</IfModule>

Source: mod_headers.

Caddy

header X-Content-Type-Options "nosniff"

Source: the header directive.

Vercel

In vercel.json:

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [{ "key": "X-Content-Type-Options", "value": "nosniff" }]
    }
  ]
}

Source: vercel.json.

Netlify

_headers in the publish directory (or [[headers]] in netlify.toml):

/*
  X-Content-Type-Options: nosniff

Source: Netlify custom headers.

WordPress

Plugin route (HTTP Headers): Settings > HTTP Headers > Security, Edit next to X-Content-Type-Options, select On with value nosniff, and Save Changes.

Code route: inside the send_headers function in wp-content/mu-plugins/security-headers.php (see the HSTS guide for the file). It does not cover static files the server sends directly, so set it at the server if you can:

header( 'X-Content-Type-Options: nosniff' );

Sources: HTTP Headers plugin, send_headers.

Check that it worked

curl -sI https://www.example.com/ | grep -i x-content-type-options

Check a stylesheet and a script from the page source too, since that is where it matters: their content-type should be text/css and a JavaScript type. In devtools, make sure the Console shows no "MIME type" errors. Then run the Owl Keeper Security headers check again.

Test it now with the free HTTP status code checker. No account needed.

What can go wrong

  • A script served without a JavaScript type, or a stylesheet not served as text/css, is blocked. Old servers and misconfigured CDNs sometimes serve .js as text/plain: check the console after deploying.
  • Otherwise this is one of the lowest-risk headers.