Vite Proxy Not Working— API Requests Failing [Fixed 2026]
Also covers: correct proxy config · path rewrite · changeOrigin · HTTPS backend · cookie forwarding · WebSocket proxy
⚡ Common Errors
404 Not Found — proxy path mismatch or wrong rewrite
CORS Error — changeOrigin not set or request not going through proxy
ECONNREFUSED — backend server not running on the target port✅ Complete proxy config
server: {
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
rewrite: (path) => path.replace(/^/api/, ""),
},
},
}How Vite Proxy Works
The Vite dev server acts as a reverse proxy for paths you configure. When your frontend fetches /api/users, the Vite server intercepts the request and forwards it to your backend target — completely transparent to the browser, with no CORS headers needed.
Correct server.proxy Configuration
Proxy silently not matching or wrong target portimport { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
export default defineConfig({
plugins: [react()],
server: {
port: 5173,
proxy: {
// Simple — forward /api/* to backend on port 3000
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
},
// Multiple proxy targets
"/auth": {
target: "http://localhost:4000",
changeOrigin: true,
},
// WebSocket proxy
"/ws": {
target: "ws://localhost:3000",
ws: true,
},
},
},
})
// Frontend fetch — use relative paths (no http://localhost:3000)
// ✅ fetch("/api/users") → proxied to http://localhost:3000/api/users
// ❌ fetch("http://localhost:3000/api/users") → bypasses proxy, CORS errorUse rewrite to Strip the /api Prefix
Backend has /users route but you fetch /api/usersIf your backend routes are /users but your frontend fetches /api/users, use rewrite to strip the prefix before forwarding.
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
// ✅ Strips /api prefix: /api/users → /users
rewrite: (path) => path.replace(/^/api/, ""),
},
}
// Without rewrite:
// fetch("/api/users") → backend receives GET /api/users
// Backend must have route: app.get("/api/users", ...)
// With rewrite:
// fetch("/api/users") → backend receives GET /users
// Backend only needs: app.get("/users", ...)
// Multiple prefix strips:
proxy: {
"/api/v1": {
target: "http://localhost:3000",
changeOrigin: true,
rewrite: (path) => path.replace(/^/api/v1/, ""),
// /api/v1/users → /users
},
}Fix CORS with changeOrigin
Still getting CORS error even with proxy configured// changeOrigin: true — sets Host header to match target host
// Without it: Host: localhost:5173 (may be rejected by backend)
// With it: Host: localhost:3000 (matches target)
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true, // ← always include this
},
}
// If STILL getting CORS errors, the request is bypassing the proxy:
// 1. Check the URL is relative: fetch("/api/users") ✅
// Not absolute: fetch("http://localhost:3000/api/users") ❌
// 2. Check Network tab in DevTools:
// Request URL should be: http://localhost:5173/api/users (through Vite)
// Not: http://localhost:3000/api/users (direct — bypasses proxy)
// 3. Enable proxy logging to confirm it's matching:
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
configure: (proxy) => {
proxy.on("proxyReq", (req) => {
console.log("[Proxy]", req.method, req.path) // confirm proxy is firing
})
},
},
}Fix HTTPS Backend with secure: false
Proxy errors with self-signed certificate on backendError: unable to verify the first certificate
DEPTH_ZERO_SELF_SIGNED_CERTproxy: {
"/api": {
target: "https://localhost:3000", // HTTPS backend
changeOrigin: true,
secure: false, // ← disable SSL certificate verification
// Only use secure: false for local dev — never in production
},
}Forward Cookies Through the Proxy
Session cookies not sent — authentication fails through proxy// Frontend — ensure credentials are included in fetch
fetch("/api/profile", {
credentials: "include", // ← sends cookies with the request
})
// vite.config.ts — configure cookie handling
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
// Rewrite Set-Cookie domain from backend host to localhost
cookieDomainRewrite: "localhost",
// If backend sets secure cookies on HTTP:
// configure: (proxy) => {
// proxy.on("proxyRes", (res) => {
// const cookies = res.headers["set-cookie"]
// if (cookies) {
// res.headers["set-cookie"] = cookies.map(c =>
// c.replace(/; Secure/i, "").replace(/; SameSite=None/i, "")
// )
// }
// })
// },
},
}For session-based authentication in development, ensure your backend sets cookies with SameSite=Lax or SameSite=None; Secure and your frontend fetch uses credentials: 'include'. The proxy forwards both the request cookies and the response Set-Cookie headers transparently.
Prevention
- Always use relative paths for API fetches (/api/users, not http://localhost:3000/api/users) — absolute URLs bypass the proxy
- Always set changeOrigin: true — it prevents Host header mismatches that cause backend rejections
- Use rewrite when your backend routes don't include the /api prefix
- Set secure: false only for local dev with self-signed HTTPS certificates
- Enable proxy configure logging during debugging to confirm which requests are being proxied
- Remember Vite proxy is dev-only — configure Nginx or cloud rewrites for production
Frequently Asked Questions
How does the Vite dev server proxy work?+−
Why am I getting 404 when using Vite proxy?+−
What does changeOrigin do in Vite proxy?+−
Why do I still get CORS errors even with Vite proxy configured?+−
Does Vite proxy work in production?+−
How do I proxy WebSocket connections in Vite?+−
Need Expert Help?
We Configure Vite for Production
Softplix engineers set up Vite proxy, build optimisation, and environment configuration for production React and Vue apps. Let us help.
Talk to an Engineer