404 on refresh, CORS and 502: fixing an app after its first deploy
The errors a web app tends to show after its first deploy, each reproduced in Docker and fixed: 404 on refresh, CORS, a hardcoded localhost URL, 502 Bad Gateway and mixed content.
TL;DR
- 404 on refresh: the app routes in the browser, but the server looks for a file called
/dashboard; answer unknown paths withindex.html(try_filesin Caddy (opens in a new tab) and nginx (opens in a new tab)), but not paths under/assets/. - CORS: the browser blocks your page from reading another origin's response unless that API allows the page's exact origin, so fix it on the API or serve the API under the same domain.
- A hardcoded
http://localhost:3000sends every visitor's browser to their own computer, and an HTTPS page cannot fetch from anhttp://URL (mixed content): set the real URL at build time, or use a relative path such as/api. - 502 Bad Gateway: the proxy cannot reach the app, so check the port, that the app listens on
0.0.0.0, and that the proxy uses the app's service name, notlocalhost.
On this page
A 404 on refresh after deploy means your app routes in the browser, but the server it now runs on looks for a real file at that path. Locally, the development server answers every path with index.html; a production web server does not, until you tell it to. The fix is one line in Caddy or nginx. Below, that error and four others that typically appear the first time an app runs somewhere other than your laptop (CORS, a hardcoded localhost URL, 502 Bad Gateway and mixed content) are each reproduced in Docker (opens in a new tab) and fixed. This is one item of the production checklist for a vibe-coded app.
Everything here was run on 11 November 2026: a Vite 8.3.8 React build served by Caddy 2.11.9 and nginx 1.30.6, a small Node.js (opens in a new tab) 24 API, and headless Chromium 158 driven by Playwright for the errors only a browser shows.
404 on refresh after deploy
A single-page app (a Vite or Create React App project, most Lovable (opens in a new tab) and Bolt (opens in a new tab) apps) has one HTML file. Clicking a link to /dashboard lets the router in the browser change the address and draw the page without asking the server; a refresh, or the link in a new tab, asks the server for /dashboard, and a plain static server has no such file and answers 404.
The test served the same dist/ folder four ways:
| Request | Caddy, file_server only | Caddy with fallback | nginx, root only | nginx with fallback |
|---|---|---|---|---|
/ | 200 | 200 | 200 | 200 |
/dashboard | 404 | 200, index.html | 404 | 200, index.html |
/settings/profile | 404 | 200, index.html | 404 | 200, index.html |
/assets/index-….js (exists) | 200 | 200 | 200 | 200 |
/assets/old-1234.js (missing) | 404 | 404 | 404 | 404 |
The fix in Caddy
:80 {
root * /srv
encode
handle /assets/* {
file_server
}
handle {
try_files {path} /index.html
file_server
}
}try_files rewrites the request to the first file that exists: the path itself, else /index.html. Use your domain instead of :80 to get Caddy's automatic HTTPS.
Why the separate /assets/ block? With the fallback applied to every path, a request for a JavaScript file that no longer exists, for example one a visitor's open tab asks for after you deploy a new build, gets index.html with status 200 and type text/html instead of a 404. In the test, a Caddyfile with just try_files {path} /index.html for every path did exactly that. Vite puts its built files in assets/; adjust the path if your build uses another folder.
The fix in nginx
server {
listen 80;
root /usr/share/nginx/html;
location /assets/ {
try_files $uri =404;
}
location / {
try_files $uri $uri/ /index.html;
}
}try_files checks the files in order and, when none exists, makes an internal redirect to the last parameter.
Server-rendered apps (Next.js (opens in a new tab), TanStack Start) route on the server, so they need no fallback: a 404 there comes from the app itself. Static hosts have their own setting for it; on GitHub Pages (opens in a new tab) there is none, and an unknown path gets the site's 404.html.
CORS errors
Your frontend at https://example.com calls an API at https://api.example.com. It worked in the builder's preview; after deploy the console says:
Access to fetch at 'http://localhost:3943/api/hello' from origin 'http://localhost:3932' has been
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.That is Chromium's exact message from the test (the ports stand for two different sites). Your code sees only TypeError: Failed to fetch; for security reasons the reason is not available to JavaScript, only in the console.
Browsers apply the same-origin policy: a page may only read responses from its own origin (scheme, host and port) unless the other origin's response says the page may, with CORS headers. The browser enforces this, not the server (curl gets the same response without complaint), so the fix is never in the frontend but on the API, or in how you lay out the two.
Fix it on the API
The API must name the page's origin. In plain Node.js, inside the request handler, the test API did it like this:
const allowed = process.env.CORS_ORIGIN; // for example https://example.com
const fromAllowedOrigin = Boolean(allowed) && req.headers.origin === allowed;
res.setHeader("Vary", "Origin");
if (fromAllowedOrigin) {
res.setHeader("Access-Control-Allow-Origin", allowed);
}
if (req.method === "OPTIONS") {
if (fromAllowedOrigin) {
res.setHeader("Access-Control-Allow-Methods", "GET, POST");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
res.setHeader("Access-Control-Max-Age", "600");
}
res.writeHead(204).end();
return;
}| Request from the page | Result |
|---|---|
GET, API sends no CORS headers | Blocked, Failed to fetch |
GET, API allows the page's origin | HTTP 200, JSON read |
POST with Content-Type: application/json, API allows the origin and answers the preflight | HTTP 200, JSON read |
GET from http://127.0.0.1:3932, API allows http://localhost:3932 | Blocked: a different origin, even on the same machine |
A JSON POST, or any request with an Authorization header, makes the browser send an OPTIONS "preflight" request first, which is why the API must answer OPTIONS too. Your framework very likely has a CORS middleware that does all of this; set its allowed origin to your production domain, and check whether it still lists only http://localhost:5173. The Boolean(allowed) check matters: without it, an unset CORS_ORIGIN and a request with no Origin header compare equal, and Node.js throws on the undefined header value. Vary: Origin goes on every response, so a cache never hands one origin's answer to another. Never echo back whatever Origin the request carries: that allows every site. If the browser sends cookies (credentials: "include"), the API must name the origin explicitly, since * is not accepted for credentialed requests, and must also send Access-Control-Allow-Credentials: true.
Or avoid CORS entirely
If you control both, serve the API under the frontend's own domain, so the browser sees one origin and never asks about CORS. In Caddy, with the API in a container named api:
:80 {
encode
handle /api/* {
reverse_proxy api:3000
}
handle /assets/* {
root * /srv
file_server
}
handle {
root * /srv
try_files {path} /index.html
file_server
}
}In the test, /, /dashboard and /api/hello all answered 200 from one origin, and a missing asset still got a 404. The frontend then calls /api/hello, a relative path, which also fixes the next two problems.
The app calls localhost, or the preview URL
A URL from development often ends up in the production build: http://localhost:3000 in a VITE_API_URL, or the builder's preview domain in a config file. In a visitor's browser, localhost is the visitor's own computer. In the test, a page served over HTTPS that called http://localhost:3999 failed with net::ERR_CONNECTION_REFUSED in the console and Failed to fetch in the code.
Search the build output before you deploy:
grep -rnoE 'https?://(localhost|127\.0\.0\.1)(:[0-9]+)?' dist/For a build made with VITE_API_URL=http://localhost:3000, it printed ./assets/index-CRyaqg4r.js:9:http://localhost:3000; for a build with the production URL, nothing. Add your builder's preview domain to the pattern too. The fix is to set the production URL when you build (see environment variables in production for why it must be at build time), or to call a relative path such as /api behind the same proxy.
The same goes for sign-in: Supabase (opens in a new tab) and OAuth providers only redirect to addresses on their allow list, so add the new domain there. The guide to deploying an AI-built app covers those redirect settings.
502 Bad Gateway
A 502 from Caddy or nginx means the proxy is running but could not get an answer from your app. The test put an API that listens on port 3000 behind both proxies, configured wrongly in each of the usual ways. Each returned 502, and each proxy's error log named the cause:
| Mistake | Caddy's log | nginx's log |
|---|---|---|
Proxy points at the wrong port (api:8080) | dial tcp 172.22.0.3:8080: connect: connection refused | connect() failed (111: Connection refused) while connecting to upstream, upstream: "http://172.22.0.3:8080/…" |
App listens on 127.0.0.1 inside its container | dial tcp 172.22.0.4:3000: connect: connection refused | |
Proxy points at localhost:3000 | connect() failed (111: Connection refused), upstream: "http://127.0.0.1:3000/…" | |
| App container not running, or on another Docker network | dial tcp: lookup api: i/o timeout |
How to read them:
- Wrong port. The address resolves but nothing listens on that port. Make the proxy's port match the one the app listens on; many apps read it from
PORTand default to 3000, 5173, 8000 or 8080. - The app listens on
127.0.0.1. Inside a container,127.0.0.1is the container itself, so connections from the proxy's container are refused. Make the app listen on0.0.0.0(for exampleapp.listen(port, "0.0.0.0"), or a--host 0.0.0.0flag). localhostin the proxy's config. For a proxy in its own container,localhostis the proxy, not your app. Use the app's service name from Compose (api:3000).- A lookup that fails. The name does not resolve: the app's container is not running, is named differently, or is on another network than the proxy.
To test from the proxy's side, run a request from inside its container, if its image includes wget (Caddy's and nginx's Alpine images do): docker exec <proxy container> wget -qO- http://api:3000/ returned the API's JSON in the test, and Connection refused for port 8080. docker compose ps and docker compose logs api show whether the app is up or crashed on start.
Mixed content
Your site is now on HTTPS, but the frontend still calls an http:// API. The browser blocks it before any request leaves:
Mixed Content: The page at 'https://app.test:3945/' was loaded over HTTPS, but requested an insecure
resource 'http://api.test:3944/api/hello'. This request has been blocked; the content must be served
over HTTPS.fetch(), scripts, stylesheets, iframes and fonts over http:// are blocked on an HTTPS page; plain images, audio and video are upgraded to https:// instead. You did not see this locally because browsers treat http://localhost and http://127.0.0.1 as secure origins, so the test used made-up .test names to reproduce it.
The fix is to serve the API over HTTPS and change the URL, or use a relative path on the same domain. The upgrade-insecure-requests directive of a Content Security Policy makes the browser try https:// for every request, which helps with old links in content but not with an API that only speaks HTTP.
Doing this with OpsCrew
When OpsCrew deploys to a server, it checks a new version's health endpoint on the app's port before the version takes traffic, and removes a version that fails; the message says to check that the app listens on the port OpsCrew passes to it as PORT, and npx opscrew logs --failed shows the failed version's last lines. npx opscrew verify checks DNS, HTTPS, the certificate and the health route one by one. See when a deploy fails. On Cloudflare (opens in a new tab)'s hosting, the plan says what a path that is no file gets, including the app itself for a single-page app; see hosting on Cloudflare.
Back to the checklist
These errors are loud, so they get fixed. The quieter problems, an open database table or a key in the bundle, are the reason for the rest of the production checklist for a vibe-coded app. To set up the server itself, start with how to deploy a web app to a VPS.
Key facts
| Fact | Value | Source |
|---|---|---|
| Loopback addresses and mixed content | http://localhost and http://127.0.0.1 count as secure originsSo a mixed-content bug does not show when the API runs on localhost. | Mixed content (opens in a new tab)Verified on |
| Access-Control-Allow-Origin with credentials | Must name an explicit origin, never * | Cross-Origin Resource Sharing (CORS) (opens in a new tab)Verified on |
Frequently asked questions
How do I fix a 404 on refresh with React Router?
The fix is on the server, not in React Router: every path that is not a real file must return index.html, so that React Router can render the route in the browser.
Why does my app work locally but not after deploy?
Because something in it depends on where it runs: a URL that points at localhost, a variable that was only in your local .env, a server that answers every path in development but not in production, or an HTTP API that the browser allowed on localhost but blocks from an HTTPS page.
Sources
- try_files (Caddyfile directive) (opens in a new tab)
- Module ngx_http_core_module (try_files) (opens in a new tab)
- Cross-Origin Resource Sharing (CORS) (opens in a new tab)
- Mixed content (opens in a new tab)
- When a deploy fails, and how to recover (opens in a new tab)
- Hosting on Cloudflare (opens in a new tab)
Related articles
- Deploying
Deploy a Bolt.new app to your own server
How to deploy a Bolt.new app outside Bolt Cloud: get the code, find out which stack Bolt chose, build it in Docker, serve it over HTTPS, and claim the database into Supabase.
- Deploying
Deploy a Lovable app to your own server (Vite or TanStack Start)
How to deploy a Lovable app to a server you control: tell the Vite and TanStack Start stacks apart, sync it to GitHub, build it in Docker, serve it over HTTPS, and keep or move the Supabase backend.
- Deploying
Migrate a Heroku app and its Postgres database to a VPS
A tested migration from Heroku to your own server: the Procfile as Docker Compose services, config vars to a file, Scheduler to supercronic, Heroku Postgres restored with pg_restore, a DNS cutover and a way back.