Maaz Zindani

Loading the index…

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

Pages load unstyled because /_next/static files return 404 behind nginx

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

  • Applies to Next.js 15, Next.js 16, nginx 1.26, Node.js 22, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The 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. 2The 404 comes from the Node.js port too, and the app runs from .next/standaloneThe standalone server runs without public and .next/staticFix
    3. 3The 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

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

Confirmed by nginx.org

Did this fix it?

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/public

2Fix

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

3Verify

Request a chunk from the Node.js port.

curl -I http://127.0.0.1:3000/_next/static/chunks/<file>.js

Confirmed by nextjs.org

Did this fix it?

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

Confirmed by nextjs.org

Did this fix it?

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:

Related fixes

Servers

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 fix

Next step

Want this handled for good?

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