Historically, I used
ipfw from the command line to do port forwarding on my Mac. Unfortunately, as of Yosemite OS X 10.10
ipfw has been removed. (In all fairness,
ipfw has been deprecated for some time but I continued using it because it was way easier than
pfctl on the command line).
Command Line pfctl
pfctl seems to prefer configuration files to command line parameters, which I find frustrating. Fortunately, we can use command line pipes to work around this limitation.
Forward Port 80 and 443 with Mac pfctl Port Forwarding
You can copy and paste the following onto the command line.
echo " rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080 rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 8443 " | sudo pfctl -ef -
Remove Port Forwarding
sudo pfctl -F all -f /etc/pf.conf
Display Your Current Port Forwarding Rules
sudo pfctl -s nat
Vagrant Port Forwarding with pfctl
As a WordPress developer, I use VVV for my virtual environment. By default, VVV does not expose the guest environment to other computers on your network. I address the general setup in Vagrant VVV Setup for WordPress Development. The port forwarding in Vagrant is accomplished using Vagrant triggers and these lines in my Customfile (or in your Vagrantfile if you’re not using VVV).
config.trigger.after [:up, :reload, :provision], :stdout => true do system('echo " rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080 rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 8443 " | sudo pfctl -ef - >/dev/null 2>&1; echo "Add Port Forwarding (80 => 8080)\nAdd Port Forwarding (443 => 8443)"') end
Feel free to view and download my full VVV Custom file for port forwarding.