Localhost HTTPS with mkcert: Certificates, Trust and Errors

Generate a development certificate for localhost and loopback IPs, configure Node or Vite HTTPS, and distinguish trust failures, name mismatches, and protocol errors.


Changing http://localhost to https://localhost does not enable TLS. The server needs a certificate and private key, the client must trust the issuer, and the certificate must cover the address you visit.

Modern browsers can treat some HTTP loopback addresses as potentially trustworthy for secure-context features. That does not mean the connection uses TLS, and it does not apply to arbitrary LAN addresses. Decide whether you are testing a browser feature’s context requirements or actual HTTPS behavior. See MDN’s secure-context explanation.

Create a development certificate with mkcert

Install the tool for your OS using the official instructions, then run these commands in your development project:

mkdir .certs
mkcert -install
mkcert -cert-file .certs/localhost.pem -key-file .certs/localhost-key.pem localhost 127.0.0.1 ::1

mkcert -install creates a local development CA and configures local trust. The final command issues a certificate covering the three specified names or addresses. It does not configure your web server or install trust on other devices.

Add .certs/ to your project’s .gitignore. Do not commit or share private keys. In particular, never copy mkcert’s rootCA-key.pem to a phone or another user.

Start an explicit Node HTTPS service

Create local-https.mjs in the project root beside .certs:

import { createServer } from 'node:https';
import { readFileSync } from 'node:fs';

const server = createServer(
  {
    cert: readFileSync(new URL('./.certs/localhost.pem', import.meta.url)),
    key: readFileSync(new URL('./.certs/localhost-key.pem', import.meta.url)),
  },
  (_request, response) => response.end('local HTTPS demo\n')
);
server.listen(8443, process.argv[2] || '127.0.0.1', () => console.log(server.address()));

Run node local-https.mjs, then visit https://localhost:8443/ or https://127.0.0.1:8443/. The example binds only to IPv4. If localhost first reaches IPv6 and fails, compare the explicit IPv4 URL. See loopback address differences.

Clients using the system trust store may already recognize the issuer. To specify the CA explicitly:

mkcert -CAROOT
curl --cacert "/replace-with-the-printed-directory/rootCA.pem" https://localhost:8443/

Replace the directory with the actual output. The expected body is local HTTPS demo. A trust failure is diagnostic information; bypassing verification does not demonstrate a correctly trusted certificate.

Configure Vite with the same certificate

Merge this into your project’s vite.config.ts:

import { defineConfig } from 'vite';
import { readFileSync } from 'node:fs';

export default defineConfig({
  server: {
    host: '127.0.0.1',
    port: 5173,
    strictPort: true,
    https: {
      cert: readFileSync(new URL('./.certs/localhost.pem', import.meta.url)),
      key: readFileSync(new URL('./.certs/localhost-key.pem', import.meta.url)),
    },
  },
});

Restart the development process and use its printed HTTPS address. This config applies to development; preview and production hosting need separate configuration. See server.https and the Vite development/preview guide.

Match the error to the failing layer

Error or symptomFirst check
ERR_CERT_AUTHORITY_INVALIDClient trust in the issuing CA and browser-specific trust settings
Certificate name mismatchActual hostname or IP versus the certificate’s covered names
ERR_SSL_PROTOCOL_ERRORWhether that port really serves HTTPS rather than HTTP
ERR_CONNECTION_REFUSEDProcess, binding, and port using the port checklist
API requests blocked from an HTTPS pageRemaining HTTP endpoints, mixed content, and proxy configuration

What changes for phone access?

A phone visits the computer’s LAN address. The certificate must cover that actual IP or resolvable name, the server must listen on a reachable interface, and the phone must trust the development CA. Installing trust on the computer does not install trust on the phone.

When reissuing the certificate, append the real LAN IP to the original command, for example 192.168.1.20, replacing that example value first. Certificate installation and trust activation vary by phone platform. Follow mkcert’s platform guidance and transfer only the CA certificate used for trust, never its private key. See phone-to-computer connectivity for network checks.

Stop the service after testing. Development CAs and certificates belong in environments you control; a locally issued certificate is not a substitute for a public website certificate.

Local development guides & tools © LocalHost Run