/config/vite/vite-server-proxy
vite.config server.proxy: proxy API calls in the dev server
The commands on this page have not been executed by our verification runner yet.updated 2026-10-20
- tool
- Vite
- file
- vite.config.js / vite.config.ts
- key
- server.proxy
- type
- Record<string, string | ProxyOptions>
- default
- none
server.proxy makes the Vite dev server forward matching requests to another server. With '/api': 'http://localhost:8080', the browser calls http://localhost:5173/api/users on the same origin as the page, and Vite passes the request to your backend: no CORS headers needed in development. The option affects only vite (dev) and vite preview, not the production build.
Example
import { defineConfig } from 'vite';
export default defineConfig({
server: {
proxy: {
// /api/users -> http://localhost:8080/api/users
'/api': 'http://localhost:8080',
// /auth/login -> https://auth.example.com/login, with the Host header rewritten
'/auth': {
target: 'https://auth.example.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/auth/, ''),
},
// WebSockets
'/socket': { target: 'ws://localhost:8080', ws: true },
},
},
});Options
| Option | Meaning |
|---|---|
| target | backend URL to forward to |
| changeOrigin | set the Host header to the target host (needed for most virtual-hosted backends) |
| rewrite | function that changes the path before forwarding |
| ws | proxy WebSocket upgrades |
| secure | false accepts self-signed certificates on an https target |
| configure | (proxy, options) => {} to add listeners to the underlying http-proxy |
Pitfalls
- Keys are matched as path prefixes; keys starting with
^are regular expressions. - Requests go through the proxy only when the page calls a relative URL (
/api/...). A hard-codedhttp://localhost:8080/apibypasses it and hits CORS again. - In production you need the same routing from your web server or hosting (for example an nginx
location /api/).
Sources
Related pages
errors · corsblocked by CORS policy: No 'Access-Control-Allow-Origin' headerThe browser blocks a cross-origin response without Access-Control-Allow-Origin. Fix it on the server by sending the header for your origin, or proxy the API.flags · eslinteslint --fix: automatically fix lint problemsnpx eslint . --fix applies the fixes that rules provide, such as let to const, and reports what it cannot fix. Preview changes first with --fix-dry-run.commands · prettierprettier --write and --check: format files from the command lineFormat a project with npx prettier . --write, or fail CI on unformatted files with npx prettier . --check. Configure it with .prettierrc and .prettierignore.