Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Vite dev proxy fails against a local API with a self-signed certificate

ErrorRequests to a local HTTPS API fail with a self-signed certificate error

Short 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.

  • Applies to Vite 7, Vite 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.*
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The browser console shows a certificate error for the API origin and no proxy rule matchesThe browser calls the HTTPS API directlyFix
    3. 3The page at https://localhost:5173 is marked not secure and network requests stop loadingThe dev server itself uses a self-signed certificateFix

What does the error look like?

  • Requests to a local HTTPS API fail with a self-signed certificate error
  • The proxied /api call returns an error while the same API works in a tool that skips certificate checks
  • Network 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/health

Confirmed by github.com

Did this fix it?

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:5001

Confirmed by vite.dev

Did this fix it?

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 dev

Confirmed by vite.dev

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.