mac無法使用80端口問題


前言:
在mac os中,非root用戶是無法使用小於1024的常用端口的。如果開發中需要用到80端口, 就要設置端口轉發。

hosts文件介紹
(1)hosts文件是將域名和IP地址建立映射關系的系統文件,用戶可以自定義常用域名跟IP,當在瀏覽器上輸入網址時,系統會優先從hosts文件找到相應的IP地址,打開相應的網頁。
(2)hosts文件的作用是域名解析,構建映射關系,屏蔽垃圾網站。
(3)為什么要修改hosts文件?
在微信公眾號開發時候,需要獲取用戶的微信信息,如果在微信開發者工具的地址欄填寫localhost就獲取不了,所以必須要填寫審核通過的微信公眾號域名。在這里,假設我的微信公眾號域名為:https://test.cn 。本地啟動了java項目,當你很高興地在在微信開發者工具輸入https://test.cn 時候,會注意到點擊網頁任意一個按鈕,都不會觸發到本地java項目的任何一個接口。原因好簡單,因為默認hosts文件中,域名localhost 映射的是 127.0.0.1 ip地址。因此,需要把localhost 映射為https://test.cn。

修改hosts文件的步驟
(1)打開Terminal,輸入以下指令:

cd /private/etc
sudo vim hosts
打開后,默認的hosts文件長這樣:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 samuel.local # added by Apache Friends XAMPP
(2)把localhost 映射為https://test.cn (改成你要映射的域名)后 ,修改后是張這樣的:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
#127.0.0.1 localhost
127.0.0.1 test.cn
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 samuel.local # added by Apache Friends XAMPP
保存退出。

端口轉發
域名映射完成后,需要做的是端口轉發,也就是將監聽的80端口轉發到8080。
1.創建idea.tomcat.forwarding文件:

sudo vim /etc/pf.anchors/idea.tomcat.forwarding
在idea.tomcat.forwarding添加以下命令:

rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080

2.創建pf-tomcat.conf文件

sudo vim /etc/pf-tomcat.conf
1
在pf-tomcat.conf添加以下命令:

rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/idea.tomcat.forwarding"

3.啟動端口轉發功能

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
pfctl: pf already enabled
1
2
3
4
5
6
7
4.關閉端口轉發功能

sudo pfctl -d
或者關閉全部

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

注意事項:
重啟mac,需要手動重啟端口轉發命令

sudo pfctl -ef /etc/pf-tomcat.conf

java項目配置
在由spring boot搭建的java開發項目中,用到的開發工具是IDEA,操作系統mac OS 10.13.4。因為微信公眾號的開發需要,端口號要設置為80,
之前已經設置好端口轉發,80轉發到8080,所以在本地調試的時候,項目的服務器端口設置為8080,

轉自:https://blog.csdn.net/samuelandkevin/article/details/80279773


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM