Papermint Studio

HTTPS on localhost:9000 on a Mac: Trusted Certs, No Warnings

Run HTTPS on localhost port 9000 on a Mac: make a trusted certificate with mkcert, serve or proxy your dev server, and fix browser certificate warnings.

In short
  • A port only speaks one protocol. If your app on 9000 serves plain HTTP, typing https://localhost:9000 fails until something on that port speaks TLS.
  • Use a local certificate authority, not a bare self-signed cert. mkcert adds a development CA to your Mac's keychain, so browsers trust the certificates it makes without warnings.
  • Two ways to get HTTPS on 9000. Give the dev server the key and certificate directly, or move the app to another port and put a TLS proxy on 9000.
  • Plain http://localhost is already a secure context. You need HTTPS mainly for custom hostnames, Secure cookies, OAuth callbacks and matching production.

To run HTTPS on localhost:9000 on a Mac, create a certificate your Mac trusts with mkcert (brew install mkcert, then mkcert -install and mkcert localhost 127.0.0.1 ::1). Then hand that key and certificate to whatever listens on port 9000. That is either your dev server itself, or a small reverse proxy that accepts HTTPS on 9000 and forwards to your app on another port.

Below are the steps for both setups, the errors you'll see when one piece is missing, and how to use a friendlier name than localhost:9000.

What localhost:9000 actually is

localhost is your own Mac (127.0.0.1 in IPv4, ::1 in IPv6), and 9000 is a TCP port some program is listening on. Port 9000 is popular: PHP-FPM, SonarQube, the MinIO S3 API and older Portainer setups all use it by default. Before you change anything, check what holds the port:

Terminal
lsof -nP -iTCP:9000 -sTCP:LISTEN

The COMMAND and PID columns tell you which program it is. If nothing shows up, the port is free. If the wrong program has it, quit that program or pick another port.

Watch out for PHP-FPM. It listens on 9000 but speaks FastCGI, not HTTP. Opening http://localhost:9000 or https://localhost:9000 in a browser will never work. A web server such as nginx has to sit in front and talk FastCGI to it.

Why https://localhost:9000 fails out of the box

A port doesn't switch between HTTP and HTTPS on its own. When you type https://, the browser starts a TLS handshake. If the program on 9000 answers in plain HTTP, the handshake fails. Chrome shows "This site can't provide a secure connection" (ERR_SSL_PROTOCOL_ERROR), and Firefox shows SSL_ERROR_RX_RECORD_TOO_LONG.

So you need two things:

  1. A certificate for localhost that your browser trusts.
  2. Something on port 9000 that uses that certificate to speak TLS.

Do you really need HTTPS on localhost?

Browsers already treat http://localhost and http://127.0.0.1 as a secure context. Service workers, the clipboard API, camera and microphone access, and other "secure only" features work there over plain HTTP. If that's all you need, you can stop here.

HTTPS starts to matter when:

  • You use a custom hostname like myapp.test. Only localhost gets the secure-context exception; other names need real HTTPS.
  • Your cookies are marked Secure or SameSite=None, and you want them to behave exactly as they will in production.
  • An OAuth provider or payment webhook only accepts https:// callback addresses.
  • You're debugging HTTPS itself: redirects, HSTS, mixed-content warnings or HTTP/2.

Create a trusted local certificate with mkcert

A self-signed certificate on its own brings back the "Your connection is not private" warning every time, because nothing vouches for it. mkcert fixes that. It creates a small certificate authority that lives only on your Mac, adds it to the system keychain, and then signs certificates with it.

Install it with Homebrew. Add nss if you use Firefox, since Firefox keeps its own list of trusted authorities:

Terminal
brew install mkcert
brew install nss   # only needed for Firefox
mkcert -install

mkcert -install asks for your password once, because it adds the CA to the System keychain. You can see it in Keychain Access under System › Certificates, as a certificate whose name starts with "mkcert".

Now make a certificate. List every name you'll use, so one certificate covers them all:

Terminal
mkdir -p ~/certs && cd ~/certs
mkcert localhost 127.0.0.1 ::1 myapp.test

This writes two files, named after the first name plus a count of the extra names:

  • localhost+3.pem is the certificate.
  • localhost+3-key.pem is the private key.

Each name you listed goes into the certificate's subject alternative names, which is what browsers check. A certificate that only has the name in its common name field will be rejected.

Never share the CA's own key. Run mkcert -CAROOT to see where it lives. Anyone who has rootCA-key.pem could make certificates your Mac trusts for any website. Keep it out of repositories, backups you share and Docker images. To remove the CA's trust later:

Terminal
mkcert -uninstall

That takes it out of the keychain (and Firefox, if nss is installed). Delete the CAROOT folder too if you're done with it.

Serve your dev server on port 9000 over HTTPS

If your dev server can take a key and a certificate, give them to it directly. You don't need a proxy at all.

Vite (vite.config.js):

JavaScript
import fs from 'node:fs'
import os from 'node:os'
import { defineConfig } from 'vite'

const dir = `${os.homedir()}/certs`

export default defineConfig({
  server: {
    port: 9000,
    https: {
      key: fs.readFileSync(`${dir}/localhost+3-key.pem`),
      cert: fs.readFileSync(`${dir}/localhost+3.pem`),
    },
  },
})

Plain Node:

JavaScript
import https from 'node:https'
import fs from 'node:fs'

const options = {
  key: fs.readFileSync('localhost+3-key.pem'),
  cert: fs.readFileSync('localhost+3.pem'),
}

https.createServer(options, (req, res) => {
  res.end('hello over https\n')
}).listen(9000)

With Express, pass your app in place of the handler function.

Next.js has an experimental flag that can use your files:

Terminal
next dev -p 9000 --experimental-https \
  --experimental-https-key ~/certs/localhost+3-key.pem \
  --experimental-https-cert ~/certs/localhost+3.pem

Open https://localhost:9000 and the padlock should appear with no warning.

Put HTTPS on 9000 in front of an app on another port

Sometimes the app can't do TLS: a Docker container, a Java server, PHP behind PHP-FPM, or a tool you don't control. Then run the app on another port, say 8080, and let a reverse proxy take 9000. Two programs can't listen on the same port, so the app has to move.

Install nginx from Homebrew (brew install nginx). Then add a server block to a file in nginx's servers folder. That folder is /opt/homebrew/etc/nginx/servers/ on Apple silicon and /usr/local/etc/nginx/servers/ on Intel.

server {
    listen 9000 ssl;
    server_name localhost myapp.test;

    ssl_certificate     /Users/you/certs/localhost+3.pem;
    ssl_certificate_key /Users/you/certs/localhost+3-key.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Then check and start it:

Terminal
nginx -t
brew services start nginx

What the lines do:

  • The Upgrade and Connection lines pass WebSockets through, so hot reload keeps working.
  • X-Forwarded-Proto tells the app the browser used HTTPS. Many frameworks need this to build correct redirect and callback URLs.
  • Port 9000 is above 1024, so nginx needs no sudo to listen there.

PHP-FPM works on the same idea, with two differences. nginx uses fastcgi_pass 127.0.0.1:9000, and it takes a different port for HTTPS itself, since PHP-FPM already has 9000.

Fix browser certificate warnings

What you seeWhat it meansFix
ERR_SSL_PROTOCOL_ERROR / SSL_ERROR_RX_RECORD_TOO_LONGThe port answers in plain HTTPGive the server the cert, or put a TLS proxy on that port
NET::ERR_CERT_AUTHORITY_INVALIDThe browser doesn't trust the CARun mkcert -install, then quit and reopen the browser
Warning only in FirefoxFirefox has its own trust storebrew install nss, then run mkcert -install again
NET::ERR_CERT_COMMON_NAME_INVALIDThe name in the address bar isn't in the certificateMake a new cert that lists that name
ERR_CONNECTION_REFUSEDNothing is listening on 9000Start the server; check with lsof

After you add a CA, quit the browser completely (⌘Q, not just closing the window). Browsers cache trust decisions and only pick up the new CA on restart.

Use a friendly domain instead of localhost:port

To open https://myapp.test:9000 instead, do two things:

  • Point the name at your Mac in /etc/hosts.
  • Make sure the certificate includes the name. The mkcert command above already does.

Our guide to editing the hosts file on a Mac has the full steps, including flushing the DNS cache. Use .test, which is reserved for exactly this. Browsers force HTTPS on .dev, and .local hosts entries go through multicast DNS.

The hosts file maps names to addresses only, so the :9000 stays in the URL. Dropping it means running a proxy on port 443 that routes each name to its port.

You may not want to manage certificates, hosts entries and proxy configs by hand. Porter, our free Mac app, does this for dev servers:

  • It gives each server a .local name such as shop.local instead of localhost:5173. It answers those names on the Mac itself, without a hosts file entry.
  • It serves trusted HTTPS from a certificate authority limited to .local names.
  • It forwards requests as a reverse proxy with WebSockets intact.
  • It finds the dev servers already running and suggests a name from the project folder.
  • The names need no admin password.

Troubleshoot mixed content and cookies

Mixed content. Once the page loads over HTTPS, the browser blocks scripts and API calls to http:// addresses. Say your front end on https://localhost:9000 calls an API on http://api.myapp.test:8080: those calls fail. Some browsers make an exception for http://localhost, but don't count on it. You have two fixes:

  • Serve the API over HTTPS too.
  • Proxy the API through the same origin (Vite's server.proxy, or another location /api/ block in nginx), and call it with a relative path such as /api/users.

Hot reload over WebSockets. A page on HTTPS needs wss:// for its live-reload socket. Dev servers handle this when they serve HTTPS themselves. Behind a proxy, the Upgrade headers above are what make it work.

Cookies. A cookie marked Secure is never sent over plain HTTP to a custom hostname, and SameSite=None requires Secure.

Also remember that cookies ignore the port: localhost:3000 and localhost:9000 share the same cookies. If two local apps both set a session cookie, they'll overwrite each other. Giving each app its own hostname keeps them apart.

OAuth callbacks. Register the exact URL, including https, the hostname and :9000. A mismatch in any part makes most providers reject the redirect.

FAQ

Why does https://localhost:9000 say the site can't provide a secure connection?

The program on port 9000 is answering in plain HTTP. Either configure it with a certificate and key, or move it to another port and run a TLS proxy on 9000.

Can I get a Let's Encrypt certificate for localhost?

No. Public certificate authorities don't issue certificates for localhost or names that only exist on your machine. A local CA like mkcert's is the usual answer for development.

Is running mkcert -install safe?

The CA it creates is trusted only on your Mac, and only you hold its private key. The risk is that key leaking. Keep the CAROOT folder private, and run mkcert -uninstall when you no longer need it.

Do I need sudo to use port 9000?

No. Port 9000 is above 1024, so any user can listen on it. You only need your password once, when mkcert -install adds its CA to the keychain.

Keep reading