Vite dev proxy fails against a local API with a self-signed certificate
Requests to a local HTTPS API fail with a self-signed certificate errorShort answer
The Vite dev server forwards proxied requests with Node.js, and Node.js refuses a certificate it does not trust. For a local API, set secure: false on that proxy rule so the certificate is not verified. Calls made straight from the browser to the API need the certificate trusted in the system or browser instead.
Fix it fast
1Find out which cause you have
Decide who makes the failing call. If the browser asks the Vite origin (for example /api) the proxy makes it. If the browser calls https://localhost:5001 directly, the browser makes it.
grep -rn "https://localhost\|fetch(\|axios" src | head -n 20 grep -n -A8 "proxy" vite.config.*2Match what you see, then jump to its fix
- 1
The browser calls a path on the Vite origin such as /api, and the call failsThe proxy talks to an HTTPS target with a certificate Node.js does not trustFix - 2
The browser console shows a certificate error for the API origin and no proxy rule matchesThe browser calls the HTTPS API directlyFix - 3
The page at https://localhost:5173 is marked not secure and network requests stop loadingThe dev server itself uses a self-signed certificateFix
- 1
What does the error look like?
Requests to a local HTTPS API fail with a self-signed certificate errorThe proxied /api call returns an error while the same API works in a tool that skips certificate checksNetwork requests stop loading on a page served with a self-signed certificate
Cause 1 of 3
The proxy talks to an HTTPS target with a certificate Node.js does not trust
1Check
The proxy options come from http-proxy-3. Its secure option controls whether SSL certificates are verified.
grep -n -B2 -A6 "target" vite.config.*2Fix
Set secure to false on the rule for the local API only. Keep changeOrigin if the API checks the host header.
// vite.config.js
export default {
server: {
proxy: {
'/api': {
target: 'https://localhost:5001',
changeOrigin: true,
secure: false,
},
},
},
}3Verify
Restart the dev server and request the proxied path.
npm run dev
curl -i http://localhost:5173/api/healthConfirmed by github.com
Cause 2 of 3
The browser calls the HTTPS API directly
1Check
No proxy is involved, so the secure option has no effect. Vite's troubleshooting page says a self-signed certificate also makes Chrome ignore caching directives.
grep -rn "https://localhost" src .env*2Fix
Use a certificate the machine trusts. On macOS, add the certificate to the trusted list as the Vite troubleshooting page describes, or call the API through a proxy rule so the browser never sees its certificate.
# macOS: add your own certificate with `security add-trusted-cert`,
# or open it in Keychain Access and set it to Always Trust.3Verify
Reload the page with the cache disabled and check the console.
curl -I https://localhost:5001Confirmed by vite.dev
Cause 3 of 3
The dev server itself uses a self-signed certificate
1Check
Vite's server.https option needs a valid certificate. The @vitejs/plugin-basic-ssl plugin makes a self-signed one, which browsers do not trust by default.
grep -n -E "https|basic-ssl" vite.config.*2Fix
Create a certificate your machine trusts and pass it in server.https, or drop HTTPS for local development if nothing needs it.
import fs from 'node:fs'
export default {
server: {
https: {
key: fs.readFileSync('./localhost-key.pem'),
cert: fs.readFileSync('./localhost.pem'),
},
},
}3Verify
Open the dev URL; the browser shows no certificate warning.
npm run devConfirmed by vite.dev
How do I stop it coming back?
- Set secure: false only in the dev proxy, never in production config.
- Prefer one trusted local certificate for all local services.
- Keep API URLs in an environment variable so dev and production differ in one place.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix