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:
brew install mkcert
brew install nss # only if you use Firefox
mkcert -installThen create a certificate for your hostname. Use localhost or a custom name:
mkcert localhost
# or
mkcert mysite.exampleThis writes a .pem certificate and a -key.pem key into the current folder. Point your server at them. In Node:
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.
- In the Cloudflare dashboard, go to Networking > Tunnels and select Create Tunnel.
- Name it, for example
local-dev. - 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
- Open the tunnel and go to Routes > Add route > Published application.
- Enter a subdomain and choose your domain, for example
dev.yourdomain.com. - 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:
cloudflared tunnel login
cloudflared tunnel create local-dev
cloudflared tunnel route dns local-dev dev.yourdomain.com
cloudflared tunnel run local-devPut the service URL and tunnel UUID in ~/.cloudflared/config.yml:
url: http://localhost:8000
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.jsonIf 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.
CAUTION
Keep one caution in mind. Anyone with the public URL can reach your dev server, so don't leave a tunnel running with sensitive data behind it. Stop cloudflared when you're done.
References
- web.dev - web.dev/articles/how-to-use-local-https
- Cloudflare - developers.cloudflare.com/cloudflare-one/networks/connectors/cloudflare-tunnel
- Cloudflare - developers.cloudflare.com/tunnel/setup
- Cloudflare - developers.cloudflare.com/tunnel/advanced/local-management/create-local-tunnel
- Cloudflare - developers.cloudflare.com/tunnel/troubleshooting