Skip to content
Subdomains on localhost: 3 Ways to Set Them Up (2026)

Subdomains on localhost: 3 Ways to Set Them Up (2026)

Updated 6 min read

Comprehensive guide to localhost subdomains: *.localhost automatic resolution (RFC 6761), fixed hosts entries, and wildcard setups with dnsmasq.

**The fastest way to use subdomains on localhost: just use *.localhost.** Modern browsers resolve any app.localhost, api.localhost, or tenant1.localhost to 127.0.0.1 automatically, with zero configuration. When you need a custom domain shape, HTTPS, or routing to different ports, map fixed subdomains in the hosts file, or use dnsmasq for wildcards. Here are all three approaches and when to pick each.

Option 1, Free *.localhost subdomains (no setup)

Per RFC 6761, localhost and everything under it is reserved for loopback. Chrome, Edge and Firefox resolve any *.localhost name to 127.0.0.1 without any hosts edit or DNS config:

http://app.localhost:3000
http://api.localhost:3000
http://tenant-acme.localhost:3000
How many times will you retype this command this month?Automate it with ToggleHosts

This is ideal for multi-tenant or subdomain-routed apps: your server reads the Host header and serves the right tenant, while every subdomain points at the same local server. No admin rights, nothing to clean up later. To understand the underlying name, see what is localhost?.

Caveats:

  • It always resolves to 127.0.0.1, you cannot point a subdomain at a different IP this way.
  • Some older tools/runtimes do not honor *.localhost; if a non-browser client fails, fall back to Option 2. Support differs by browser, operating system and tool, so test with the client you really use. Recent versions of curl resolve it to the loopback address themselves.

Option 2, Fixed subdomains via the hosts file

When you want a custom TLD (e.g. .test) or need the name to resolve outside the browser, list each subdomain in the hosts file:

127.0.0.1 myproject.test
127.0.0.1 app.myproject.test
127.0.0.1 api.myproject.test
127.0.0.1 admin.myproject.test

You can also group names on one line: 127.0.0.1 app.myproject.test api.myproject.test. The hosts file matches exact names only, there is no pattern matching, so add a line per subdomain. See how to edit the hosts file and the hosts file syntax guide. Prefer .test over .local (which collides with mDNS/Bonjour): which TLD to use for local development.

After editing, flush DNS so the new names resolve immediately (e.g. ipconfig /flushdns on Windows, or the macOS/Linux equivalent).

Option 3, Wildcard subdomains with dnsmasq

If subdomains are dynamic or too many to list (tenant-1, tenant-2, … or generated per PR), the hosts file is the wrong tool. Use dnsmasq to resolve a domain and all its subdomains with one rule:

# macOS (Homebrew)
brew install dnsmasq
echo 'address=/myapp.test/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf
sudo brew services start dnsmasq
sudo mkdir -p /etc/resolver
echo 'nameserver 127.0.0.1' | sudo tee /etc/resolver/test

Now anything.myapp.test resolves to 127.0.0.1 without touching the hosts file. Full Linux steps and verification: wildcard local domains with dnsmasq.

Test a subdomain without editing anything

You can check your routing before touching the hosts file. Send the Host header yourself, or tell curl which address to use for a name:

# Ask the local server for a given host
curl -H "Host: app.myproject.test" http://127.0.0.1:3000/

# Resolve the name to 127.0.0.1 for this one request only
curl --resolve app.myproject.test:3000:127.0.0.1 http://app.myproject.test:3000/

If the first command returns the right tenant, your server routing works and only the name resolution is left to set up.

Which approach should you use?

  • Just need many subdomains on 127.0.0.1, in the browser? Use *.localhost (Option 1).
  • A few fixed subdomains, custom TLD, or non-browser clients? Hosts file (Option 2).
  • **Dynamic/unbounded subdomains, or HTTPS for *.myapp.test?** dnsmasq (Option 3), plus a wildcard mkcert certificate.

Routing subdomains to different ports

DNS only maps a name to an IP, not a port. To serve app.myproject.test and api.myproject.test from different backends on one machine, put a reverse proxy (Caddy, nginx, Traefik) in front on port 80/443 that routes by Host header to each app's port. The subdomains all resolve to 127.0.0.1; the proxy does the rest.

A minimal Caddyfile that serves two subdomains over HTTPS with its own local certificate authority:

app.myproject.test {
    tls internal
    reverse_proxy localhost:3000
}

api.myproject.test {
    tls internal
    reverse_proxy localhost:4000
}

Run caddy trust once so your system accepts the certificates Caddy issues.

Frameworks that reject unknown hostnames

A subdomain can resolve perfectly and still be refused by the dev server, which checks the Host header it receives. The browser shows a blocked-request or invalid-host page instead of a connection error. Add your names to the allow list:

ToolSetting
Viteserver.allowedHosts: ['.myproject.test']
webpack-dev-serverallowedHosts: ['.myproject.test']
DjangoALLOWED_HOSTS = ['.myproject.test']
Railsconfig.hosts << ".myproject.test"

A leading dot matches the domain and all its subdomains. Vite allows *.localhost by default, which is another reason to start with Option 1.

Cookies and CORS across subdomains

Subdomains are different origins, so a page on app.myproject.test calling api.myproject.test is a cross-origin request and the API must send CORS headers. Cookies depend on how the browser decides what counts as the same site:

  • Under .test, app.myproject.test and api.myproject.test share the site myproject.test. A session cookie with Domain=.myproject.test is visible to both, and SameSite=Lax cookies are sent between them.
  • Under .localhost, app.localhost and api.localhost are treated as different sites because localhost is the top level. Shared cookies and SameSite rules are harder to get right there.

If your app shares a login between subdomains, use a two-part domain such as myproject.test (Option 2 or 3) rather than *.localhost.

Common pitfalls

  • Subdomain refuses to connect: the name resolved but the server is not listening or does not accept that host. Check the port and the dev server's allowed-hosts setting, see localhost refused to connect.
  • HTTPS warning on a subdomain: the certificate must cover it; generate mkcert myproject.test "*.myproject.test". A wildcard covers one level only, so a.b.myproject.test needs its own entry. See local HTTPS with mkcert.
  • Old IP after changes: flush DNS and the browser host cache.

For everyday fixed mappings, ToggleHosts manages hosts entries with safe per-project toggles and automatic DNS flushing, so subdomains stay tidy and you only reach for dnsmasq when you truly need wildcards. To pick a hosts manager, the editor comparisons for Windows, Mac and Linux put the options side by side.

_Last tested: June 2026 on Windows 11, macOS 26 Tahoe and Ubuntu 24.04._

Sources and further reading

Frequently Asked Questions

Yes, for *.localhost. Modern browsers (Chrome, Edge, Firefox) resolve any *.localhost name such as app.localhost or api.localhost to 127.0.0.1 with no configuration, per RFC 6761.

Map it in the hosts file: 127.0.0.1 app.myproject.test. List each subdomain you need on its own name; the hosts file matches exact names only.

The hosts file does not support wildcards. Use dnsmasq with a rule like address=/myapp.test/127.0.0.1, which resolves myapp.test and every subdomain to 127.0.0.1.

Use *.localhost when you want zero setup and 127.0.0.1 is fine. Use .test (with the hosts file or dnsmasq) when you need a custom domain shape, HTTPS certificates, or routing to different ports.

Related Articles

© 2026 ToggleHosts · All rights reserved[email protected]