Vite Server Options & Proxy
The server section of the config controls the dev server: which port and network interface it uses, whether it opens a browser, HTTPS, CORS, file watching — and, very commonly, a proxy to your back-end API.
Port, host and open
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
server: {
port: 3000, // default 5173
strictPort: true, // fail instead of trying 3001, 3002…
host: true, // listen on all addresses (LAN, Docker)
open: '/dashboard' // open the browser at this path on start
},
});By default Vite only listens on localhost, so other devices cannot reach it. Use host: true (or the --host flag) to test on your phone over Wi-Fi or to run inside a container:
npx vite --host
VITE v8.3.4 ready in 289 ms
➜ Local: http://localhost:5173/
➜ Network: http://192.168.1.24:5173/
➜ press h + enter to show help
If the port is busy and strictPort is false, Vite simply picks the next free one:
npx vitePort 5173 is in use, trying another one...
VITE v8.3.4 ready in 263 ms
➜ Local: http://localhost:5174/
➜ Network: use --host to expose
➜ press h + enter to show help
Common server options
| Option | Purpose |
|---|---|
port, strictPort |
Port number and whether to fail when it is taken |
host |
'localhost' (default), true/'0.0.0.0' for all interfaces |
open |
Open the browser (true or a path) |
proxy |
Forward requests to another server |
https |
TLS options; easiest with @vitejs/plugin-basic-ssl |
cors |
CORS settings for the dev server |
allowedHosts |
Extra hostnames allowed to access the server (e.g. a tunnel domain) |
headers |
Extra response headers |
hmr |
HMR WebSocket options (overlay: false hides the error overlay) |
watch |
File watcher options (usePolling: true for some VMs/WSL/Docker setups) |
fs.allow / fs.deny |
Which files may be served |
warmup.clientFiles |
Pre-transform files you know will be requested |
Note:
previewhas its own section (preview.port,preview.proxy, …) with the same shape. It defaults to port 4173.
Why a proxy?
In development your front end runs on http://localhost:5173 but your API might run on http://localhost:8080. Calling it directly from the browser is a cross-origin request, which needs CORS headers on the API. A proxy avoids the problem: the browser calls /api/... on Vite's own origin, and Vite forwards the request server-side.
browser ──GET /api/courses──▶ Vite :5173 ──GET /courses──▶ API :8080
◀──────── JSON ────── ◀──── JSON ─────export default defineConfig({
server: {
proxy: {
// string shorthand: /health -> http://localhost:8080/health
'/health': 'http://localhost:8080',
// with options
'/api': {
target: 'http://localhost:8080',
changeOrigin: true, // set Host header to the target
rewrite: (path) => path.replace(/^\/api/, ''), // /api/courses -> /courses
},
// WebSockets
'/socket': { target: 'ws://localhost:8080', ws: true },
// keys starting with ^ are regular expressions
'^/files/.*\\.pdf