Subdomains on localhost on a Mac

Updated · 2 min read

You want shop.localhost and api.localhost instead of localhost:3000 and localhost:8080. It takes two things: the name has to reach your own Mac, and something on port 80 has to send each name to the right port.

1. The name reaches your Mac

  • Chrome and Firefox send any *.localhost name to your own Mac by themselves. There's nothing to set up.
  • macOS 26 does it for every app, Safari included.
  • Safari on macOS 15 asks the system, and macOS 15 doesn't know .localhost names, so Safari says it can't find the server.

/etc/hosts doesn't help much: it has no wildcards, so every name needs its own line, added with your password. For macOS 15 the usual fix is a small local DNS server, such as dnsmasq, and a file in /etc/resolver/ that sends .localhost names to it.

2. Port 80 sends each name to its port

The browser opens shop.localhost on port 80, while your server runs on 3000. A reverse proxy on port 80 passes each name on to its port. With Caddy:

text
http://shop.localhost {
	reverse_proxy localhost:3000
}

http://api.localhost {
	reverse_proxy localhost:8080
}

Two catches: when a server moves to another port, you edit the file and reload Caddy; and only one program can listen on port 80, so the proxy has to be the one.

Things to know

  • Hot reload works through a proxy like this: Caddy passes WebSocket connections on.
  • Cookies set on shop.localhost aren't sent to api.localhost, just as between two real domains. If both need one login, serve them under one name, for example the API at shop.localhost/api.
  • For https://shop.localhost, see HTTPS on localhost.

How Portroom handles it

Pretty URLs, built in

Portroom has the proxy built in. Each project gets http://<name>.localhost, and when its port changes, the name follows at once, with nothing to edit. A stopped project shows a page that opens the site by itself once the server is up. WebSockets and hot reload pass through, and only your own Mac can open these addresses.

On macOS 15, Settings ▸ Ports & URLs ▸ Safari and other apps ▸ Set Up makes Safari work too, with your password once. Remove takes it away.

Pretty URLharbor-hotel.localhost
Actual URLlocalhost:
Network URL192.168.1.42:

Something wrong or out of date on this page? Write to support@portroom.dev.

More guides

Every dev server on your Mac, in one window.

Free for three servers at once, with 14 days of Pro first. No account.