The Vite dev proxy does not forward my /api requests
GET http://localhost:5173/api/users 404 (Not Found)Short answer
Vite proxies only requests whose path starts with a key in server.proxy, and it forwards the full path unless you rewrite it. Check that the browser calls a relative URL like /api/users, that the backend really has the /api prefix, and remember the proxy exists only in the dev and preview servers.
Fix it fast
1Find out which cause you have
Open the Network tab and look at the full URL of one failing call. A call to localhost:5173/api/... means the proxy matched; a call to another host means the code bypasses it.
grep -rn "localhost:\|fetch(\|axios" src/ | head -202Match what you see, then jump to its fix
- 1
The Network tab shows an absolute URL to the backend, or a path whose start differs from the keyThe request path does not start with a proxy key, or the call uses a full URLFix - 2
The proxy works (the backend answers) but replies 404 for /api/usersThe backend has no /api prefix, so the forwarded path returns 404Fix - 3
The remote or virtual-host backend answers with another site's page or a 404 although the path is rightThe backend picks its site by the Host headerFix - 4
The proxy fails to reach an https target that works with certificate checks offThe target is https with a self-signed certificateFix - 5
Everything works with npm run dev, and /api returns 404 or HTML after deployingThe built site is served somewhere the proxy does not existFix
- 1
What does the error look like?
GET http://localhost:5173/api/users 404 (Not Found)The browser calls the backend directly instead of going through the Vite dev server
Cause 1 of 5
The request path does not start with a proxy key, or the call uses a full URL
1Check
Vite proxies a request only when its path starts with a key in server.proxy. A call written as http://localhost:5000/api/x never reaches the Vite server.
grep -n "proxy" -A8 vite.config.*2Fix
Call a relative path that starts with the key. If your config sets a non-relative base, put that base in front of each key.
// vite.config.js
export default defineConfig({
server: { proxy: { '/api': 'http://localhost:5000' } },
})
// client code
fetch('/api/users')3Verify
Restart the dev server, since config changes need it, and repeat the call.
curl -i http://localhost:5173/api/usersConfirmed by vite.dev
Cause 2 of 5
The backend has no /api prefix, so the forwarded path returns 404
1Check
The proxy sends the path on unchanged. Ask the backend directly for both forms.
curl -i http://localhost:5000/users
curl -i http://localhost:5000/api/users2Fix
Strip the prefix with rewrite, and set changeOrigin as the documented example does.
server: {
proxy: {
'/api': {
target: 'http://localhost:5000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
},
}3Verify
The call through Vite now returns what the backend returns directly.
curl -i http://localhost:5173/api/usersConfirmed by vite.dev
Cause 3 of 5
The backend picks its site by the Host header
1Check
By default the Host header keeps the dev server's value. With changeOrigin the proxy changes the host header to the target URL.
curl -i -H "Host: localhost:5173" https://api.example.com/users2Fix
Turn on changeOrigin for that proxy entry.
server: {
proxy: {
'/api': { target: 'https://api.example.com', changeOrigin: true },
},
}3Verify
Repeat the request through the dev server.
curl -i http://localhost:5173/api/usersConfirmed by github.com
Cause 4 of 5
The target is https with a self-signed certificate
1Check
The proxy option secure controls whether the target's SSL certificate is verified.
curl -I https://my-local-api.test/2Fix
Set secure to false for that entry in development only.
server: {
proxy: {
'/api': { target: 'https://my-local-api.test', secure: false },
},
}3Verify
Call the path through the dev server.
curl -i http://localhost:5173/api/usersConfirmed by github.com
Cause 5 of 5
The built site is served somewhere the proxy does not exist
1Check
server.proxy configures the dev server, and the preview server inherits it by default. A static host serving dist/ has no proxy at all.
npm run build && npx vite preview2Fix
In production, point the app at the real API URL (for example through a VITE_ variable) or set up a reverse proxy on the host that serves dist/.
# .env.production
VITE_API_URL=https://api.example.com
// client code
fetch(`${import.meta.env.VITE_API_URL}/users`)3Verify
Build and check the deployed network call goes to the API host.
npm run buildConfirmed by vite.dev
How do I stop it coming back?
- Use relative paths (/api/...) in client code and keep the backend host only in vite.config.
- Restart the dev server after editing vite.config.
- Decide early how production reaches the API, since the dev proxy will not exist there.
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
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix