解决Mac 80端口被占用

在做一些第三方登录开发调试、微信小程序等开发时,必须要用本机的80端口。但是,mac却禁止了普通用户访问1024以下的端口,包括80端口,因为mac会用这些端口来提供文件共享等等很多服务。

解决办法:停掉mac自带的占用80端口的程序(其实就是一个apache),然后再设置端口转发,将80端口的请求转发到8080或9090端口。

具体操作如下:

一、关闭占用80端口的apache:sudo apachectl stop
二、修改/etc/pf.conf,设置端口转发:
sudo vi /etc/pf.confrdr-anchor "com.apple/*" 后添加 rdr on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080,修改后的效果:

#
# Default PF configuration file.
#
# This file contains the main ruleset, which gets automatically loaded
# at startup.  PF will not be automatically enabled, however.  Instead,
# each component which utilizes PF is responsible for enabling and disabling
# PF via -E and -X as documented in pfctl(8).  That will ensure that PF
# is disabled only when the last enable reference is released.
#
# Care must be taken to ensure that the main ruleset does not get flushed,
# as the nested anchors rely on the anchor point defined here. In addition,
# to the anchors loaded by this file, some system services would dynamically
# insert anchors into the main ruleset. These anchors will be added only when
# the system service is used and would removed on termination of the service.
#
# See pf.conf(5) for syntax.
#

#
# com.apple anchor point
#
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
rdr on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 9090
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"

lo0 通过ifconfig 看自己那个设备绑定的是127.0.0.1, lo0是这个网络设备的名字

三、使修改生效,依次执行以下命令:

sudo pfctl -d
sudo pfctl -f /etc/pf.conf  
sudo pfctl -e  

完成,可以测试效果了!

(全文完)

(转载本站文章请注明作者和出处 解决Mac 80端口被占用