MAC OS 本質上還是 Unix 系統, Unix 系統大多默認情況下非root用戶是無法使用小於1024的常用端口的.這時候如果你開發中需要在普通用戶下用到80端口, 比如 tomcat, 比如 vitualbox 下構建了一個 http 服務, 若你想直接通過 瀏覽器的 localhost 訪問的話(不用加上莫名其妙的”:端口”的話)你就需要做一些系統端口轉發的工作.
MAC OS 10.10 上 pfctl 就可以做這一件事情, 詳情請參見
1、創建文件 :
sudo vim /etc/pf.anchors/eclipse.tomcat.forwarding
文件內容 :
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080 rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 443 -> 127.0.0.1 port 8443
2、創建文件 :
sudo vim /etc/pf-tomcat.conf
文件內容 :
rdr-anchor "forwarding" load anchor "forwarding"from "/etc/pf.anchors/eclipse.tomcat.forwarding"
3、啟動
sudo pfctl -ef /etc/pf-tomcat.conf
執行結果
root@ymdeMacBook-Air etc# sudo pfctl -ef /etc/pf-tomcat.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 pf enabled
假設你的工程 http://localhost/myDemo.html 這時訪問你的web工程, 你會發現 http://localhost/myDemo.html 這個不需要8080端口即可訪問,加上 http://localhost:8080/myDemo.html 訪問不了。
4、關閉
sudo pfctl -d
執行結果
root@ymdeMacBook-Air etc# sudo pfctl -d No ALTQ support in kernel ALTQ related functions disabled pf disabled
這時你訪問你的web工程, 你會發現 http://localhost/myDemo.html 這個訪問不了,加上 http://localhost:8080/myDemo.html 即可訪問。
或者全部關閉
pfctl -F all -f /etc/pf.conf