ViteProxyError Fix2026 Updated

Vite Proxy Not Working— API Requests Failing [Fixed 2026]

Also covers: correct proxy config · path rewrite · changeOrigin · HTTPS backend · cookie forwarding · WebSocket proxy

January 20265 min readVite 5/6 · TypeScript

⚡ Common Errors

Error
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

vite.config.ts
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.

1

Correct server.proxy Configuration

Proxy silently not matching or wrong target port
3 min
vite.config.ts — proxy patterns
import { 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 error
2

Use rewrite to Strip the /api Prefix

Backend has /users route but you fetch /api/users
2 min

If your backend routes are /users but your frontend fetches /api/users, use rewrite to strip the prefix before forwarding.

rewrite — strip 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
  },
}
3

Fix CORS with changeOrigin

Still getting CORS error even with proxy configured
2 min
changeOrigin and CORS debugging
// 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
      })
    },
  },
}
4

Fix HTTPS Backend with secure: false

Proxy errors with self-signed certificate on backend
2 min
Error
Error: unable to verify the first certificate
DEPTH_ZERO_SELF_SIGNED_CERT
Disable SSL verification for local HTTPS backends
proxy: {
  "/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
  },
}
5

Forward Cookies Through the Proxy

Session cookies not sent — authentication fails through proxy
3 min
Cookie forwarding and credentials
// 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

Frequently Asked Questions

How does the Vite dev server proxy work?+
Vite's proxy intercepts requests from your frontend to paths matching the proxy key (e.g. /api) and forwards them to your backend server. The browser sees requests going to localhost:5173/api — the proxy silently rewrites them to your backend (e.g. localhost:3000/api). This solves CORS in development without changing your backend.
Why am I getting 404 when using Vite proxy?+
A 404 from the proxy means either: the proxy path doesn't match your request URL, the rewrite is stripping too much of the path, or your backend doesn't have a route for the proxied URL. Check that your fetch('/api/users') URL matches the proxy key '/api' and that your backend has GET /api/users defined.
What does changeOrigin do in Vite proxy?+
changeOrigin: true sets the Host header of the proxied request to match the target host instead of localhost. Many APIs and backends validate the Host header for security. Without changeOrigin, the request reaches your backend with Host: localhost:5173 which may be rejected. Always set changeOrigin: true when proxying to a different host.
Why do I still get CORS errors even with Vite proxy configured?+
If you're still getting CORS errors, the request is not going through the proxy. Check: the fetch URL starts with /api (relative, not absolute), you're running the Vite dev server (not production build), and the proxy key in vite.config.ts exactly matches the URL prefix. Absolute URLs like http://api.example.com/users bypass the proxy entirely.
Does Vite proxy work in production?+
No. Vite proxy only runs during development with the Vite dev server. In production, your built assets are served statically — there is no Vite process. For production, configure your web server (Nginx, Caddy) or cloud platform (Vercel rewrites, Railway proxy) to forward API requests to your backend.
How do I proxy WebSocket connections in Vite?+
Add ws: true to your proxy config for the path that serves WebSocket connections. For example: proxy: { '/ws': { target: 'ws://localhost:3001', ws: true } }. This proxies both HTTP upgrades and WebSocket frames through the Vite dev server.

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