Skip to content
On this page

Localhost HTTPS Reverse Proxy

Custom local domain names

You can also just use anything.here.localhost and it will work.

If you want a custom domain extension, like .test do this:

bash
$ brew install dnsmasq
$ mkdir -pv $(brew --prefix)/etc/
$ echo 'address=/.test/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf
$ sudo brew services start dnsmasq
$ sudo mkdir -v /etc/resolver
$ sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'

Validate:

bash
$ scutil --dns

should show something like:

resolver #8
  domain   : test
  nameserver[0] : 127.0.0.1
  flags    : Request A records, Request AAAA records
  reach    : 0x00030002 (Reachable,Local Address,Directly Reachable Address)

Set up local HTTPS

bash
$ brew install mkcert
$ mkcert -install
$ mkcert -cert-file localhost-cert.pem -key-file localhost-key.pem "*.localhost" localhost 127.0.0.1 ::1

Reverse proxy

In Node:

javascript
const app = require('express')();
const { createProxyMiddleware } = require('http-proxy-middleware');
const https = require('https');
const path = require('path');
const fs = require('fs');
const os = require('os');

app.use(
  '/api/',
  createProxyMiddleware({
    target: 'http://127.0.0.1:8000',
    changeOrigin: false,
    pathRewrite: {},
  }),
);

app.use(
  '/',
  createProxyMiddleware({
    target: 'http://127.0.0.1:4200',
    ws: true, // needed for hot reload in most UI frameworks
    changeOrigin: false,
    pathRewrite: {},
  }),
);

// listen on port 80 for HTTP
app.listen(80, '0.0.0.0', () => {
  console.log('HTTP listening on port 80');
});

// listen on port 443 for HTTPS
const server = https.createServer({
  key: fs.readFileSync(path.join(os.homedir(), 'localhost-key.pem')),
  cert: fs.readFileSync(path.join(os.homedir(), 'localhost-cert.pem')),
}, app).listen(443, () => {
  console.log('HTTPS listening on port 443')
});