← Guides

How to add X-Content-Type-Options: nosniff

Browsers sometimes ignore the type a server says a file has and guess from its content, so a file served as plain text or an image can end up run as a script. X-Content-Type-Options: nosniff tells the browser to trust the declared type and not guess.

Updated 2026-10-23

Why it matters

  • It closes a class of attacks where an uploaded file is tricked into running as a script.
  • It is one fixed value with no tuning, so it is the easiest header to add.

How to fix it

  1. Add this header to every response from example.com.

Pick what your site runs on. Replace example.com with your own domain where it appears.

After you deploy, re-check the site. A CDN or the browser cache can keep serving the old headers for a few minutes.

nginx

nginx (server or location block)
add_header X-Content-Type-Options "nosniff" always;

Apache

Apache (needs mod_headers)
Header always set X-Content-Type-Options "nosniff"

Caddy

Caddyfile
header {
	X-Content-Type-Options "nosniff"
}

Cloudflare

Cloudflare → Rules → Transform Rules → Modify Response Header (Set static), one per line
X-Content-Type-Options: nosniff

Vercel

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

Netlify

_headers (in your publish directory)
/*
  X-Content-Type-Options: nosniff

Next.js

next.config.ts
const nextConfig = {
  async headers() {
    return [
      {
        source: "/(.*)",
        headers: [
          { key: "X-Content-Type-Options", value: "nosniff" },
        ],
      },
    ];
  },
};
export default nextConfig;

Express

Express middleware (before your routes)
app.use((req, res, next) => {
  res.setHeader("X-Content-Type-Options", "nosniff");
  next();
});

Common mistakes

  • Adding it while some files are served with the wrong type. With nosniff, a stylesheet served as text/plain stops applying. Check your site still looks right.
  • Typing the value differently. The only valid value is nosniff.

Check that it worked

Run your domain through the free security headers checker. It takes a few seconds, needs no account, and shows anything still missing.

Open the security headers checker

Questions

Will it break my site?
Only if a script or stylesheet is served with the wrong content type. If something stops loading, look at the type the server sends for it and correct that.

Related guides

Don't want to check by hand? Upzy watches your site around the clock and tells you when a header, certificate or email record slips. Start free.