您的位置:首页 > 其它

解决mac无法使用80端口问题,亲测可用

2018-05-11 12:26 106 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/samuelandkevin/article/details/80279773

前言:

在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
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 443 -> 127.0.0.1 port 8443

2.创建pf-tomcat.conf文件

sudo vim /etc/pf-tomcat.conf

在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

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,如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: