nginx serves CSS and JavaScript with the wrong content type
Content-Type: text/plain on a .css or .js fileShort answer
Browsers refuse a stylesheet unless it is sent as text/css, and refuse a script that is not a JavaScript type, once nosniff is set. In nginx this means mime.types is not included, a location overrides types, or the file is missing and a fallback returns HTML. Check the Content-Type header with curl first.
Fix it fast
1Find out which cause you have
Ask for the file and read only the headers. The Content-Type line tells you which cause applies.
curl -I http://localhost/css/site.css2Match what you see, then jump to its fix
- 1
Content-Type: text/plain for every file type except html, gif and jpgmime.types is not includedFix - 2
The wrong type appears only under one location, and the config has types { } or default_type thereA types block replaces the mapping for that locationFix - 3
Content-Type: text/html for a .css or .js URL, often with the app shell as the bodyThe file is not found and a fallback returns HTMLFix
- 1
What does the error look like?
Content-Type: text/plain on a .css or .js fileContent-Type: text/html on a .css or .js file
Cause 1 of 3
mime.types is not included
1Check
Without a full mapping, nginx knows only a few extensions and everything else gets the default type, text/plain. The complete table ships with nginx in conf/mime.types.
grep -R -n -E "mime.types|default_type" /etc/nginx/nginx.conf2Fix
Include the table in the http block.
http {
include mime.types;
default_type application/octet-stream;
}3Verify
Reload and read the header again; it should say text/css.
sudo nginx -t && sudo nginx -s reload
curl -I http://localhost/css/site.cssConfirmed by nginx.org
Cause 2 of 3
A types block replaces the mapping for that location
1Check
A types block inside a location sets the mapping used in that location. An empty one with default_type makes every response in it use that one type.
grep -R -n -B3 -A3 -E "types +\{" /etc/nginx/2Fix
Delete the override from locations that serve CSS and JS, or keep it only for the download folder it was meant for.
location /download/ {
types { }
default_type application/octet-stream;
}3Verify
Request a stylesheet from the corrected location.
curl -I http://localhost/static/site.cssConfirmed by nginx.org
Cause 3 of 3
The file is not found and a fallback returns HTML
1Check
try_files with a final fallback such as /index.html answers a missing asset with HTML. The browser then sees HTML where it expected CSS or JS, and with nosniff it blocks the response: style requests need text/css, scripts need a JavaScript type.
curl -s http://localhost/css/site.css | head -n 3
grep -R -n "try_files" /etc/nginx/2Fix
Fix root or alias so the asset path exists, or give asset folders their own location that ends in =404.
location /assets/ {
root /var/www/site;
try_files $uri =404;
}3Verify
A real asset returns its own type, and a missing one returns 404.
sudo nginx -t && sudo nginx -s reload
curl -I http://localhost/assets/site.cssConfirmed by nginx.org
How do I stop it coming back?
- Keep include mime.types in the http block of every nginx.conf you ship.
- Serve static asset folders from their own location with try_files $uri =404.
- Add a curl -I check for one CSS and one JS file to your deploy steps.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Argo CD behind an nginx Ingress loops with too many redirects
307 Temporary Redirect on every request to the Argo CD host
The browser and Argo CD disagree about who handles TLS.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Docker php image has no php.ini and my edits do not stay
A setting changed in php.ini inside the container is gone after the container is recreated
The official php image ships php.ini-development and php.ini-production but no active php.ini.
2 causes, each with a fix