使用IDEA开发时,会经常需要在本地启动tomcat服务器,并且要使用80端口。使用1024以下的端口需要root权限,这样就比较麻烦了。可以通过设置转发规则,实现这个目的。
首先添加规则,让本机所有的80端口的请求都走到127.0.0.1:8080
。
$ echo "rdr pass proto tcp from any to any port {80} -> 127.0.0.1 port 8080" | sudo pfctl -Ef -
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
pf enabled
Token : 11729729078737411621
设置完成之后,可以看看这条规则是否在起作用。
$ sudo pfctl -s nat
No ALTQ support in kernel
ALTQ related functions disabled
rdr pass inet proto tcp from any to any port = 80 -> 127.0.0.1 port 8080
接着要去/etc/hosts里面添加规则
127.0.0.2 a.b.c.com
打开一个Web服务器测试一把。
$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
可以看到a.b.c.com:80->127.0.0.2:80->127.0.0.1:8080
起作用了。
执行下面这条命令可以删除临时添加的规则。
$ sudo pfctl -F all -f /etc/pf.conf
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
rules cleared
nat cleared
dummynet cleared
0 tables deleted.
0 states cleared
source tracking entries cleared
pf: statistics cleared
pf: interface flags reset