Use HTTPS locally, then put it on your own domain with Cloudflare Tunnel

Run your dev server over trusted HTTPS with mkcert, then use a Cloudflare Tunnel to reach it from the internet on your own domain.

Problem

Most of the time http://localhost is fine for development. Browsers treat it as a secure context, so most HTTPS-only features still work there. It stops being enough when you use a custom hostname or secure cookies across browsers. The site then behaves differently than it will in production, and you find the bugs late.

The usual shortcuts have their own issues:

  • A self-signed certificate makes browsers show a warning you have to click through.
  • A browser flag that marks a hostname as secure only works in Chrome. It is also risky if that hostname ever resolves to a real server.
  • A certificate from a public CA needs a real domain you control, so it can't cover localhost.

There is a second problem. Sometimes you need someone else to open your local site, such as a client, a teammate, or a webhook provider. Your machine has no public address, and opening a port on your router is a bad idea.

Solution

Step 1: Trusted HTTPS on your machine with mkcert

mkcert creates a local certificate authority that only your device trusts. It then signs certificates with it. Install it once:

Bash
brew install mkcert
brew install nss   # only if you use Firefox
mkcert -install

Then create a certificate for your hostname. Use localhost or a custom name:

Bash
mkcert localhost
# or
mkcert mysite.example

This writes a .pem certificate and a -key.pem key into the current folder. Point your server at them. In Node:

javascript
const https = require('https');
const fs = require('fs');

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

https
  .createServer(options, (req, res) => {
    res.end('hello over https');
  })
  .listen(8000);

Open https://localhost:8000. There should be no warning, because your browser trusts the mkcert CA.

Step 2: Create the tunnel

A Cloudflare Tunnel runs a small program called cloudflared on your machine. It opens an outbound connection to Cloudflare, so you open no inbound ports. You need a free Cloudflare account and a domain that uses Cloudflare's nameservers.

  1. In the Cloudflare dashboard, go to Networking > Tunnels and select Create Tunnel.
  2. Name it, for example local-dev.
  3. Pick your operating system and run the install and run commands the dashboard shows. The tunnel should show as Healthy.

Step 3: Map your domain to your local server

  1. Open the tunnel and go to Routes > Add route > Published application.
  2. Enter a subdomain and choose your domain, for example dev.yourdomain.com.
  3. Set the Service URL to your local server, for example http://localhost:8000.

Cloudflare creates the DNS record for you. Visitors connect to Cloudflare over HTTPS with Cloudflare's certificate, so for this route your local server can stay on plain HTTP. If your server only speaks HTTPS, use https://localhost:8000 as the service URL. If the certificate covers the public hostname rather than localhost, set Origin Server Name to that hostname and leave certificate verification on. Cloudflare's HTTPS origins guide covers the details.

If you prefer the command line, the locally managed flow does the same thing:

Bash
cloudflared tunnel login
cloudflared tunnel create local-dev
cloudflared tunnel route dns local-dev dev.yourdomain.com
cloudflared tunnel run local-dev

Put the service URL and tunnel UUID in ~/.cloudflared/config.yml:

yaml
url: http://localhost:8000
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json

If you only need a quick demo without a domain, Cloudflare's Quick Tunnels give you a temporary trycloudflare.com URL.

Outcome

You can now open your local app at https://localhost:8000 with a trusted certificate. It also runs at https://dev.yourdomain.com for anyone you share it with, without a router change or a public IP. Traffic goes out through Cloudflare, and your machine accepts no inbound connections.

References

  1. web.dev - web.dev/articles/how-to-use-local-https
  2. Cloudflare - developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel
  3. Cloudflare - developers.cloudflare.com/tunnel/setup
  4. Cloudflare - developers.cloudflare.com/tunnel/advanced/local-management/create-local-tunnel
  5. Cloudflare - developers.cloudflare.com/tunnel/troubleshooting

Handmade by Devvrat. All rights reserved.