The day nginx swallowed our CSS

💥 AI Pitfalls 2026-06-28 · Updated 2026-10-27 3 min read

A redesigned site served a 404 for the new CSS in production while localhost was fine. Permissions were fine too; the real cause was nginx location precedence plus an alias path pointing one level too deep. Covers root vs alias, location matching order, the trailing-slash trap, a five-step triage, and cache-busting.

📜 Table of Contents

📖 Keep reading

The day nginx swallowed our CSS

New template, new CSS file. Localhost looked perfect. In production the page came back as bare HTML.

DevTools said 404. The file was on the server, twice confirmed with ls. curl 127.0.0.1/static/app.css returned 200. curl against the domain returned 404. Same file, two answers, so the fault was in between.

Two hours later: an alias pointing one directory too deep.

A translucent glass panel displaced out of register from its aligned stack
The CSS returned 200 from localhost and 404 from the domain — an nginx alias pointing one directory too deep.

1. root versus alias

Directive How the path is built Example
root root plus full URI root /var/www; + /static/a.css → /var/www/static/a.css
alias alias replaces the matched prefix location /static/ { alias /var/www/; } → /var/www/a.css

root appends, alias substitutes. Mix them up and the file is simply not there.

2. The trailing slash is not cosmetic

alias must end with the same slash as its location. Otherwise nginx does plain string substitution:

location /static/ { alias /var/www/static;  }   → /var/www/staticapp.css  404
location /static/ { alias /var/www/static/; }   → /var/www/static/app.css  200

nginx -t passes either way. Valid config, missing file.

3. Location precedence

Order is fixed and ignores where you wrote the blocks:

  1. = exact match, stops
  2. ^~ prefix, stops regex evaluation
  3. regex ~ / ~*, first match in config order wins
  4. plain prefix, longest wins

So a location ~* \.(css|js)$ block written above location /static/ will win for /static/app.css and resolve via root, not your alias. Fix with location ^~ /static/ or by anchoring the regex.

4. Five-step triage

Separate layers: compare curl -I to localhost with -H "Host: example.com" against the public URL.

Dump the real config: nginx -T | grep -n -A6 'location.*static'. That is the include-expanded version, often revealing a stale symlink in sites-enabled.

Check the filesystem: ls -l, then namei -l /var/www/static/app.css. Workers run as www-data or nginx; root being able to read proves nothing. A missing x on a directory also yields 404, not 403.

Read error_log, not access_log:

Rule out caching: append ?v=123. If it loads, the problem is browser cache, expires, or a CDN caching 404s.

5. Cache and versioned filenames

Fingerprinted assets can be cached hard; HTML must not be:

location = /index.html { add_header Cache-Control "no-cache, must-revalidate"; }
location ^~ /static/   { alias /var/www/static/; expires 30d;
                         add_header Cache-Control "public, immutable"; }

If the new HTML references a hash that no longer exists on disk, you get 404s that look like config bugs.

Pre-deploy checklist

FAQ

nginx -t passes but 404s remain? It never checks that the alias path exists, nor worker permissions.

Localhost 200, domain 404? Different server block, different locations. Always send the Host header when testing.

Restart required? No. nginx -s reload is graceful.

root or alias? Use root when URI mirrors the tree; use alias only when a prefix must be rewritten.

Version-specific matching details: see ngx_http_core_module in the official nginx docs.

Related

Related Articles
2026-06-28
AI-generated Chinese text on the cover: it took me 4 iterations to get it right.
2026-06-17
My horse wrote a 47-page dissertation on why the chicken crossed the road
2026-07-03
Using AI is like a recipe that rewrites itself while you cook
2026-07-01
The AI that smells like old books
2026-06-28
My horse refused to write a recipe. Said cooking is oppressive.
2026-06-29
Will AI replace your job? My horse says: already did. You.

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.

💬 Comments (0)

No comments yet. Be the first!

Login to comment