Skip to content

Instantly share code, notes, and snippets.

@ef2k
Last active January 22, 2021 02:42
Show Gist options
  • Save ef2k/f82f63ed4b2d0905b6ba to your computer and use it in GitHub Desktop.
Save ef2k/f82f63ed4b2d0905b6ba to your computer and use it in GitHub Desktop.
Port Forwarding and Domain Tricks (macOS High Sierra)

This guide enables port forwarding for foo.com to localhost:3000. Tested on macOS High Sierra.

Add the domain to /etc/hosts

$ sudo nvim /etc/hosts

Add the following:

127.0.0.1   foo.com

Create an anchors file for port forwarding

$ sudo nvim /etc/pf.anchors/foo.conf

Add the following, (notice the port number at the end):

rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 3000

Configure port forwarding

$ sudo vim /etc/pf.conf

Add the following,

rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/foo.conf"

Enable port forwarding

$ sudo pfctl -ef /etc/pf.anchors/foo.conf

Troubleshooting

If it doesnt work make sure the following is set:

$ sudo sysctl -a | grep forwarding
net.inet.ip.forwarding: 1
net.inet6.ip6.forwarding: 1

Not set up? Set it like this:

sudo sysctl -w net.inet.ip.forwarding=1
sudo sysctl -w net.inet6.ip6.forwarding=1

Optionally, add it to /etc/sysctl.conf, for the settings to persist.

@ef2k
Copy link
Author

ef2k commented Apr 27, 2015

To quickly, turn on and off:

1. Comment out the /etc/hosts rule.
2. Comment out the /etc/pf.conf rdr-anchor and load anchor lines.
3. If turning it off, done. Otherwise, run sudo pfctl -ef <path to conf> to turn it on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment