How to Set the X-Content-Type-Options Header

X-Content-Type-Options: nosniff prevents browsers from guessing content types, blocking MIME-confusion attacks. This guide shows how to add it on all major servers and platforms.

Covers
X-Content-Type-Options, MIME

The X-Content-Type-Options header with the value nosniff instructs browsers to honour the declared Content-Type of a response and not attempt to guess (sniff) the content type. This prevents MIME-confusion attacks where an attacker tricks a browser into executing a file as a script by exploiting loose type detection.

The Only Value You Need

http
X-Content-Type-Options: nosniff

nosniff is the only valid value for this header. It is a simple, one-line addition with no configuration needed beyond enabling it.

Always Set the Correct Content-Typenosniff relies on your server sending the correct Content-Type for each resource. If your server sends text/plain for a JavaScript file, the browser will refuse to execute it. Ensure your MIME types are correctly configured alongside this header.

Set the Header on Your Server

Nginx

nginx
server { listen 443 ssl; server_name example.com; add_header X-Content-Type-Options "nosniff" always; # ... rest of config }
bash
sudo nginx -t && sudo systemctl reload nginx

Verifying the Header

bash
curl -I https://example.com | grep -i x-content-type # Expected: x-content-type-options: nosniff

Or use the ShowDNS Security Headers Scanner.