Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx serves CSS and JavaScript with the wrong content type

ErrorContent-Type: text/plain on a .css or .js file

Short 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.

  • Applies to nginx 1.26, nginx 1.28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.css
  2. 2Match what you see, then jump to its fix

    1. 1Content-Type: text/plain for every file type except html, gif and jpgmime.types is not includedFix
    2. 2The 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. 3Content-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

What does the error look like?

  • Content-Type: text/plain on a .css or .js file
  • Content-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.conf

2Fix

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.css

Confirmed by nginx.org

Did this fix it?

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.css

Confirmed by nginx.org

Did this fix it?

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.css

Confirmed by nginx.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.