Vite Proxy CORS Error: Fix It With server.proxy and Headers

A Vite proxy CORS error disappears in dev when fetch() calls a relative /api path and server.proxy forwards it to the API with changeOrigin and rewrite. The error returns after vite build, so production needs an Access-Control-Allow-Origin header on the API, or the app and the API served from one origin.

Key takeaways

  • Call fetch('/api/...') with a relative path. If you pass the full API URL, the request skips the proxy entirely.
  • changeOrigin: true sets the Host header to the target, and rewrite strips or swaps the /api prefix.
  • The proxy exists only on the dev server at localhost:5173, so a deployed bundle makes a cross-origin request again.
  • For a WordPress REST API, rewrite /api to /wp-json in dev and send the header from PHP or Nginx in production.

Fix the Vite proxy CORS error in dev, then add the header in production

The dev fix is a relative-path server.proxy rule. The production fix is a server-side header or a shared origin. You need both, because the proxy never ships with your build.

  1. Change every fetch('https://api.example.com/...') in your site code to fetch('/api/...').
  2. Add the proxy rule to vite.config (next section).
  3. Restart the dev server.
  4. Before deploying, decide how production will answer: same origin, or a CORS header.

I have not run this on a live site for this article. The proxy block follows the example in the Vite server options docs (opens in a new tab), so check your Vite version against them before you copy anything.

The exact server.proxy block for vite.config

The block below sends any request whose path starts with /api to the target. The official docs (opens in a new tab) say a key is matched against the start of the request path, and that a key starting with ^ is read as a RegExp.

JavaScript
import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://jsonplaceholder.typicode.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },
})
JavaScript
const res = await fetch('/api/posts/1')
const post = await res.json()

target is where the request goes. changeOrigin: true sets the Host header to the target host. rewrite edits the path before forwarding, so /api/posts/1 reaches the API as /posts/1.

If the proxy is not working, check these first:

  • Your fetch URL is absolute, so it never reaches the proxy.
  • The key does not match the start of the path.
  • You use a non-relative base. The docs say each key must then be prefixed with that base.
  • You edited vite.config and did not restart the dev server.

Note: To test from a phone on your LAN, set server.host to 0.0.0.0 or true, or run Vite with --host 0.0.0.0. On WSL2 that is not enough, and the Vite docs point to the WSL documentation for LAN access.

Why the browser error disappears in dev

The browser sends your request to localhost:5173, which is the origin the page came from, so the same-origin policy has nothing to block. Node inside the Vite dev server then makes the call to the API on your behalf. The same-origin policy is a browser rule, so a server-to-server request never hits it.

MDN (opens in a new tab) defines an origin as domain, scheme and port. A page on localhost:5173 calling an API on another port or host is cross-origin. The same page calling /api on itself is not.

Why the error comes back after vite build

The Vite docs state that, unless noted, server options apply only to dev. After vite build you have static files, and the browser’s call to /api goes to whatever server hosts them. If the API lives elsewhere, the request is cross-origin again, and that server must opt in with Access-Control-Allow-Origin, as MDN (opens in a new tab) states.

Production optionUse it whenWhat you set
Same originYou control the web serverReverse proxy /api to the API
CORS headerApp and API stay on separate originsAccess-Control-Allow-Origin on the API

I prefer same origin wherever you can get it, since it removes preflights entirely. If you go with a header, two rules from MDN apply. A credentialed request (cookies or HTTP authentication) needs a specific origin and rejects *. Non-simple requests, such as one with a custom header or a JSON Content-Type outside the form types, trigger an OPTIONS preflight, and the server must answer it.

WordPress REST API example: proxy in dev, header in production

For a WordPress backend, set the target to your WordPress site and rewrite /api to /wp-json. Your fetch code stays the same as above. Production is where the work moves: there the CORS header has to be sent from WordPress or from the web server in front of it, because the proxy no longer exists.

JavaScript
proxy: {
  '/api': {
    target: 'https://example.com',
    changeOrigin: true,
    rewrite: (path) => path.replace(/^\/api/, '/wp-json'),
  },
},
JavaScript
const posts = await fetch('/api/wp/v2/posts?per_page=5').then((r) => r.json())

In production, add the header in a small plugin or functions.php:

PHP
add_filter( 'rest_pre_serve_request', function ( $value ) {
    header( 'Access-Control-Allow-Origin: https://app.example.com', true );
    header( 'Vary: Origin' );
    return $value;
} );

The Nginx alternative, if you can edit the server block:

Nginx
add_header Access-Control-Allow-Origin "https://app.example.com" always;

Open the Network tab and read the response headers, then count how many times Access-Control-Allow-Origin appears. If you send cookies, add Access-Control-Allow-Credentials and handle OPTIONS too.

Next step: run vite build, serve the output from your real domain, and watch the console for the first blocked request. Then compare its response headers with the MDN CORS guide (opens in a new tab).

Frequently Asked Questions

What is a CORS error?

A CORS error means the browser blocked a script from reading a response from another origin because the server did not send the right header. The browser console shows which CORS requirement was not met. MDN (opens in a new tab) notes that JavaScript gets no details, so the console is the only place you can read the cause.

What is a CORS proxy?

A CORS proxy is a server that makes the cross-origin request for the browser, so the browser only talks to its own origin. The Vite server.proxy option works this way in dev. In production you need your own reverse proxy or a CORS header, and a public proxy service is the wrong tool for your data.

Why is my Vite proxy not working?

The usual causes are an absolute URL in fetch(), a key that does not match the path prefix, a non-relative base without the prefix on the key, or a dev server that you did not restart after the config change.