The day nginx swallowed our CSS
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.

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:
=exact match, stops^~prefix, stops regex evaluation- regex
~/~*, first match in config order wins - 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:
open() "/var/www/staticapp.css" failed (2: No such file...)→ slash bugfailed (13: Permission denied)→ ownershipdirectory index ... is forbidden→ asked for a directory
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
nginx -tpasses (syntax only, proves nothing about paths)- every
aliasslash verified against itslocation - one
curl -Iper static directory namei -lon one file per directorynginx -s reload, never restart, for zero-downtime- error_log tailed after reload
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
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.