TExcelTAD
Home › Vite › Vite Server Options & Proxy

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

js
// 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:

bash · try it yourself
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:

bash · try it yourself
npx vite
Port 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: preview has 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.

text
browser ──GET /api/courses──▶ Vite :5173 ──GET /courses──▶ API :8080
        ◀──────── JSON ──────            ◀──── JSON ─────
js
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
  

: { target: 'http://localhost:9000' },
    },
  },
});

Your code then simply uses relative URLs, which also work in production when the API is served from the same domain:

js
const res = await fetch('/api/courses');

How proxy matching works

The playground mimics Vite's proxy table: keys are checked in order, ^ keys are regular expressions, others are prefixes, and rewrite transforms the path before forwarding.

js · try it yourself
const proxy = {
  '/health': 'http://localhost:8080',
  '/api': { target: 'http://localhost:8080', rewrite: (p) => p.replace(/^\/api/, '') },
  '^/files/.*\\.pdf
  

: { target: 'http://localhost:9000' },
};

function route(url) {
  for (const [key, value] of Object.entries(proxy)) {
    const opts = typeof value === 'string' ? { target: value } : value;
    const match = key.startsWith('^') ? new RegExp(key).test(url) : url.startsWith(key);
    if (match) {
      const path = opts.rewrite ? opts.rewrite(url) : url;
      return 'proxy -> ' + opts.target + path;
    }
  }
  return 'served by Vite';
}

for (const url of ['/api/courses?page=2', '/health', '/files/guide.pdf', '/src/main.ts']) {
  console.log(url.padEnd(20), route(url));
}
/api/courses?page=2  proxy -> http://localhost:8080/courses?page=2
/health              proxy -> http://localhost:8080/health
/files/guide.pdf     proxy -> http://localhost:9000/files/guide.pdf
/src/main.ts         served by Vite

Warning: The proxy exists only in vite (and vite preview if you configure preview.proxy). In production, the same routing must be done by your real web server (nginx, a CDN rule, or the API sharing the domain).

File watching in containers and VMs

On Docker Desktop with bind mounts, WSL2 accessing Windows folders, or some network drives, file-change events may not arrive. Symptoms: saving does nothing. Fix it with polling:

js
export default defineConfig({
  server: {
    host: true,
    watch: { usePolling: true, interval: 300 },
  },
});

File system restrictions

For security, the dev server only serves files inside the workspace root. Requesting something outside returns 403. If you import from a sibling folder in a monorepo, allow it explicitly:

js
export default defineConfig({
  server: {
    fs: { allow: ['..'] },
  },
});

Vite also denies sensitive files such as .env and *.pem by default (server.fs.deny).

Test yourself

With proxy: { '/api': { target: 'http://localhost:8080', rewrite: p => p.replace(/^\/api/, '') } }, where does /api/users go?

  1. http://localhost:8080/api/users
  2. http://localhost:8080/users
  3. http://localhost:5173/users
  4. Nowhere, it 404s

How do you make the dev server reachable from your phone on the same Wi-Fi?

  1. server.open: true
  2. server.host: true or vite --host
  3. server.port: 80
  4. build.ssr: true

Does server.proxy work in your deployed production site?

  1. Yes, automatically
  2. No, it only applies to the Vite dev server (and preview if configured)
  3. Only on Netlify
  4. Only with HTTPS