Skip to content

Instantly share code, notes, and snippets.

@DawTaylor
Created November 17, 2017 11:19
Show Gist options
  • Save DawTaylor/c4919ec9b1f1639b55ef4a9d8bb38433 to your computer and use it in GitHub Desktop.
Save DawTaylor/c4919ec9b1f1639b55ef4a9d8bb38433 to your computer and use it in GitHub Desktop.
Redirect traffic from port 80 to any port on Mac OS

Redirect traffic from port 80/443 to any port on Mac OS

This allows you to access your application on port 80 without root access on Mac OS.

Non-SSL traffic

  1. First, define wich ports you want to redirect to
NON_SSL_PORT=8080
  1. Then, enable the redirecting rule
echo "\
rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port $NON_SSL_PORT\
" | sudo pfctl -ef -

SSL traffic

  1. First, define wich port you want to redirect to
SSL_PORT=8443
  1. Then, enable the redirection rule
echo "\
rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port $SSL_PORT\
" | sudo pfctl -ef -

To list current rules

sudo pfctl -s nat

To remove all rules

sudo pfctl -F all -f /etc/pf.conf

Taken from https://salferrarello.com/mac-pfctl-port-forwarding/

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