These days we might have multiple local applications that are running on different ports (e.g: http://localhost:3000, http://localhost:3001 and so on). This is pretty troublesome as we have to remember which application is running on which port.

haproxy can redirect a URL to a certain host with different port (this is impossible with hosts). Here’s how we can do it in Mac OS:

Install haproxy

brew install haproxy

Create a new file at /usr/local/etc/haproxy.cfg and put these contents (customize as you wish):

  maxconn 4096

  log global
  mode http

  timeout connect 5000ms
  timeout client  50000ms
  timeout server  50000ms

frontend web_gateway
  bind *:80

  acl is_web hdr_beg(host) myapp.local
  acl is_api hdr(host) -i api.myapp.local
  acl is_admin hdr(host) -i admin.myapp.local

  use_backend web if is_web
  use_backend api if is_api
  use_backend admin if is_admin

backend web
  server web

backend api
  server api

backend admin
  server admin

Make sure you put the hosts declared before in /etc/hosts:  localhost # We can use different domain here  admin.myapp.local  api.myapp.local

Run this command to ensure haproxy will always be running

brew services start haproxy