升级到OSX 10.11 El Capitan版本后,在本地需要开启80端口转发到9000端口以测试web程序可以这样做:
使用pfctl
实现该需求
- 创建anchor文件
/etc/pf.anchors/<CUSTOM NAME>
:
rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 9000
rdr pass on lo0 inet proto tcp from any to any port 443 -> 127.0.0.1 port 9001
- 创建
pfctl
配置文件/etc/pf-<CUSTOM NAME>.conf
:
rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/<CUSTOM NAME>"
- 应用规则,开启转发:
sudo pfctl -ef /etc/pf-<CUSTOM NAME>.conf
如果想取消端口转发,执行下列命令:
sudo pfctl -df /etc/pf-<CUSTOM NAME>.conf
设置为系统开启自动开启该功能
- 创建文件
/Library/LaunchDaemons/com.apple.pfctl-<CUSTOM NAME>.plist
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.apple.pfctl-<CUSTOM NAME></string>
<key>Program</key>
<string>/sbin/pfctl</string>
<key>ProgramArguments</key>
<array>
<string>pfctl</string>
<string>-e</string>
<string>-f</string>
<string>/etc/pf-<CUSTOM NAME>.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
- 运行下列命令使得之后开启自动运行
sudo launchctl load -w /Library/LaunchDaemons/com.apple.pfctl-<CUSTOM NAME>.plist