- Add
Header always set X-Content-Type-Options "nosniff"to theVirtualHost. WithHeader set, 404 pages and redirects go without it. - Apache's MIME tables in the official image and in Debian map
.jsand.mjstotext/javascript, so common scripts keep working with nosniff. - A file whose extension Apache does not know is sent with no
Content-Typeat all. If it is a script, nosniff blocks it: add the type withAddTypeorForceType. - Unlike a Nginx
typesblock,AddTypeinside a<Location>adds one type and keeps the rest of the table.
The short answer
Add the header to the VirtualHost of your site:
<VirtualHost *:443>
ServerName example.com
DocumentRoot "/var/www/example"
SSLEngine on
SSLCertificateFile /etc/ssl/example.com/fullchain.pem
SSLCertificateKeyFile /etc/ssl/example.com/privkey.pem
Header always set X-Content-Type-Options "nosniff"
</VirtualHost>sudo apachectl configtest && sudo apachectl gracefulalways keeps the header on error pages and redirects. Every snippet below
was run on Apache httpd 2.4.58, 2.4.62 and 2.4.69 and checked with curl, on
a page, a 404, a redirect, static files of each type and a proxied
application.
nosniff blocks scripts and stylesheets served with the wrong type, so check
your MIME types (step 1). The
X-Content-Type-Options overview explains
what the header blocks and what it can break.
Step 1: check your MIME types
Apache picks the Content-Type from the file extension, using the table that
TypesConfig loads and the AddType lines in your configuration. Check the
type of each kind of script and stylesheet you serve:
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 tests returned:
| File | Official image (conf/mime.types) |
Debian package (/etc/mime.types) |
|---|---|---|
app.js |
text/javascript |
text/javascript |
app.mjs |
text/javascript |
text/javascript |
app.css |
text/css |
text/css |
app.wasm |
application/wasm |
application/wasm |
site.webmanifest |
No Content-Type |
application/manifest+json |
| A file without an extension | No Content-Type |
No Content-Type |
Unlike Nginx, whose default table has no .mjs entry, Apache serves module
scripts with the right type out of the box. text/javascript is the type
RFC 9239 recommends.
Step 2: give a type to unknown files
When Apache does not know an extension, it sends no Content-Type
header, instead of a fallback like Nginx’s application/octet-stream. With
nosniff, browsers then refuse to run that response as a script or apply it
as a stylesheet. This hits scripts served from URLs without an extension, such
as a /assets/loader endpoint.
Add a type for an extension with AddType, or force one for a URL with
ForceType:
<VirtualHost *:443>
# ...
<Location "/assets/">
Header always set Cache-Control "public, max-age=31536000, immutable"
AddType application/manifest+json .webmanifest
</Location>
<Location "/assets/loader">
ForceType text/javascript
</Location>
</VirtualHost>Our test then returned application/manifest+json for the manifest and
text/javascript for the extensionless file. AddType adds one entry: under
/assets/, .js, .mjs and .css kept their types. This is the opposite of
a Nginx types block inside a location, which replaces the whole table.
Step 3: keep it on error pages and redirects
Apache keeps two tables of response headers. Header set writes to the one
used for successful responses; Header always set writes to the one that is
also sent on errors and redirects. Our test with
Header set X-Content-Type-Options "nosniff":
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
HTTP/1.1 404 Not Found
(no X-Content-Type-Options header)
HTTP/1.1 301 Moved Permanently
(no X-Content-Type-Options header)With Header always set, all three responses carried the header.
A Header line in the VirtualHost also reaches every <Location> and
<Directory>, even one that sets another header. Our test returned nosniff
and Cache-Control for the files under <Location "/assets/"> above. To
cover every site at once, put the line in the server configuration; the
security headers hub for Apache shows one
file with all six security headers, loaded that way.
Step 4: one header behind a reverse proxy
With mod_proxy_http, the application’s header lands in the default table
and Header always set adds yours in the other one. Our test, with an
application sending an invalid value:
HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
X-Content-Type-Options: sniffBrowsers read only the first value, as the
Fetch Standard
specifies, and Apache’s comes first, so nosniff applies. WebSec0 reads the
first value too and passes it. Keep one header anyway:
<VirtualHost *:443>
# ...
ProxyPass "/" "http://127.0.0.1:8080/"
Header unset X-Content-Type-Options
Header always set X-Content-Type-Options "nosniff"
</VirtualHost>Our test then returned a single nosniff, on the application’s pages and on
its 404s. Also check that the application sends a correct Content-Type for
any JavaScript it generates: Apache passes it through unchanged.
Verify it
Check a page, an error page and each kind of static file. Use your own URLs:
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):"
doneEach response should print x-content-type-options: nosniff and a
content-type that matches the file. 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 |
|---|---|---|
Invalid command 'Header' |
mod_headers is not loaded |
a2enmod headers, or uncomment its LoadModule line. |
| Header missing on 404 pages or redirects | Header set without always |
Write Header always set. |
A script is blocked, and its response has no Content-Type |
Apache does not know the extension, or the URL has none | AddType for the extension, or ForceType for the URL. |
A script is blocked as text/plain or application/octet-stream |
An AddType, ForceType or the application sets the wrong type |
Fix the type to text/javascript. |
Two X-Content-Type-Options headers |
The proxied application sends one too | Add Header unset X-Content-Type-Options before the always line. |
| WebSec0 fails a header that is present | The first value is not nosniff |
Make sure Apache’s header comes first, or keep only one. |
| Changes do not show up | Reload skipped, or a CDN cached old headers | apachectl configtest && apachectl graceful, then purge the CDN. |
Server hub
Every header on Apache
Other web servers
X-Content-Type-Options on other servers
Caddy
Guide ↗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.
Nginx
Guide ↗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.
Traefik
Guide ↗Set X-Content-Type-Options: nosniff on Traefik with the contentTypeNosniff option, in labels or a file, and fix the Content-Type your backend sends. Tested configs.
Questions
Frequently asked questions
What is the Apache line for X-Content-Type-Options?
Header always set X-Content-Type-Options "nosniff" in the VirtualHost, with mod_headers enabled. Then run apachectl configtest and apachectl graceful.
Why is X-Content-Type-Options missing on my 404 pages or redirects?
Header set without always writes to the table Apache uses for successful responses only. Error pages and redirects that Apache generates use the always table. Write Header always set.
Will nosniff break my JavaScript on Apache?
Not for .js or .mjs files: Apache 2.4.58 to 2.4.69 and Debian's /etc/mime.types map both to text/javascript. It breaks scripts served from a URL Apache cannot type, such as a file without an extension, which Apache sends with no Content-Type.
How do I set the MIME type of a file without an extension in Apache?
Use ForceType in a <Location> or <Files> section for that URL, for example ForceType text/javascript. For a whole extension, use AddType.
My response has two X-Content-Type-Options headers. Is that a problem?
Browsers read the first value only. Behind mod_proxy_http, Apache's always header came first in our test, so its nosniff applied. Keep one anyway, with Header unset X-Content-Type-Options before the always line.
References
Sources
- Apache Module mod_headersApache HTTP Server Project
- Apache Module mod_mime: AddTypeApache HTTP Server Project
- Apache Core Features: ForceTypeApache HTTP Server Project
- X-Content-Type-Options headerMDN Web Docs
