Pages load unstyled because /_next/static files return 404 behind nginx
GET https://example.com/_next/static/chunks/main.js 404 (Not Found)Short answer
The browser asks for /_next/static/... and the server that answers has no such file. Three causes cover most cases: an nginx root or alias that builds the wrong path, a standalone build that was started without copying .next/static, or an assetPrefix that points at a host where the files were never uploaded.
Fix it fast
1Find out which cause you have
Request one chunk URL from the failing page, once through nginx and once straight from the Node.js port. The difference tells you who returns the 404.
curl -I https://example.com/_next/static/chunks/<file-from-devtools>.js curl -I http://127.0.0.1:3000/_next/static/chunks/<file-from-devtools>.js2Match what you see, then jump to its fix
- 1
The nginx error log shows an open() path that does not exist, often with a doubled folder namenginx serves /_next/static from a path built the wrong wayFix - 2
The 404 comes from the Node.js port too, and the app runs from .next/standaloneThe standalone server runs without public and .next/staticFix - 3
The page requests chunks from another domain, such as https://cdn.example.com/_next/static/..., and that host returns 404assetPrefix points at a host that does not have the filesFix
- 1
What does the error look like?
GET https://example.com/_next/static/chunks/main.js 404 (Not Found)
Cause 1 of 3
nginx serves /_next/static from a path built the wrong way
1Check
root appends the whole URI to its path; alias replaces the matched location part. Mixing them up gives a path with /_next/static twice or missing.
sudo tail -n 20 /var/log/nginx/error.log
grep -R -n -A4 "_next/static" /etc/nginx/sites-enabled/2Fix
Use root with the folder that contains _next, or alias with the full target folder, and make sure the nginx user can read it.
location /_next/static/ {
alias /srv/my-app/.next/static/;
}3Verify
Reload nginx and request a chunk through it.
sudo nginx -t && sudo systemctl reload nginx
curl -I https://example.com/_next/static/chunks/<file>.jsConfirmed by nginx.org
Cause 2 of 3
The standalone server runs without public and .next/static
1Check
The standalone output does not copy public or .next/static by default. The minimal server.js serves them only after they are copied beside it.
ls .next/standalone/.next/static .next/standalone/public2Fix
Copy both folders after every build, then start server.js.
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/
node .next/standalone/server.js3Verify
Request a chunk from the Node.js port.
curl -I http://127.0.0.1:3000/_next/static/chunks/<file>.jsConfirmed by nextjs.org
Cause 3 of 3
assetPrefix points at a host that does not have the files
1Check
With assetPrefix set, Next.js loads the files under /_next/ from that prefix. The CDN has to hold the contents of .next/static uploaded as _next/static.
grep -n "assetPrefix" next.config.*2Fix
Upload .next/static to the prefix host as _next/static, or remove assetPrefix if you serve from the same domain. Do not upload the rest of .next.
// next.config.js
module.exports = {
// assetPrefix: 'https://cdn.example.com', // remove when no CDN is used
}3Verify
Rebuild, redeploy and check that the chunk URL in the page source returns 200.
curl -I https://cdn.example.com/_next/static/chunks/<file>.jsConfirmed by nextjs.org
How do I stop it coming back?
- Put the static copy step in the same script or Dockerfile stage as the build.
- After each deploy, request one /_next/static URL taken from the live page source.
- Prefer proxying everything to Next.js unless you need nginx to serve static files directly.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
A nginx location block answers 404 although the file exists
open() "/var/www/site/img/logo.png" failed (2: No such file or directory)
nginx builds the file path from the request URI, so a 404 usually means the path it built is not the path you expected.
3 causes, each with a fixServers
A Next.js static export works by clicking links but gives 404 on refresh or deep links
The home page loads, but a direct link to /blog/post-1 returns 404
A static export writes one HTML file per route, such as about.html.
3 causes, each with a fixServers
WebSocket to /_next/webpack-hmr fails when the Next.js dev server is behind nginx
WebSocket connection to 'ws://localhost/_next/webpack-hmr' failed: WebSocket is closed before the connection is established
Hot reload uses a WebSocket, and nginx does not forward the Upgrade and Connection headers unless you tell it to.
3 causes, each with a fixServers
nginx 502 Bad Gateway in front of a Node.js app
connect() failed (111: Connection refused) while connecting to upstream
A 502 means nginx reached for your Node.js app and got nothing usable back.
5 causes, each with a fixServers
nginx gives up waiting for the app and returns 504
upstream timed out (110: Connection timed out) while reading response header from upstream
nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.
5 causes, each with a fixServers
Azure App Service on Linux restarts the container and the site never starts
The container restarts again and again and the site returns an application error
App Service sends a warmup request to the container and restarts it if no HTTP answer comes back in time.
5 causes, each with a fix