Mac 使用 80 端口

来源:互联网 发布:linux下的下载工具 编辑:程序博客网 时间:2024/05/18 12:31

Mac 使用 80 端口

更新日志:
20170807 更新mac使用80端口的错误问题。

  • Mac 使用 80 端口
    • 一修改etcpfconf
    • 二依次执行以下命令
      • 注意如果有apache等服务器占用了80端口则需要将其停掉方能成功
    • 问题一getsockopt connection refused

  • [ ] 方案一:
    Mac OS X 因为要绑定80端口需要ROOT权限, 但是如果用root权限启动eclipse或tomcat又会造成, 启动创建的各类文件是root的,普通用户无法删除,放弃

  • [x] 方案二:
    通过pfctl做网络层的端口转发, 让连接到本机80端口的请求,都转发到8080端口;采纳

注意, Mac OS 会使用80端口做网络文件共享,要先关闭掉。

一、修改/etc/pf.conf

先对pf.conf进行备份:

sudo cp /etc/pf.conf /etc/pf.conf.normal.bak

之后在该文件中以下行:

sudo vim /etc/pf.conf

rdr-anchor "com.apple/*"
后面添加一行配置,如下:

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

注意:lo0 通过ifconfig 看自己那个设备绑定的是127.0.0.1, lo0是这个网络设备的名字,一般大家都是lo0。如图:

二、依次执行以下命令:

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

注意:如果有apache等服务器占用了80端口,则需要将其停掉方能成功!

如果出现

No ALTQ support in kernelALTQ related functions disabledpfctl: pf not enabled

忽略即可。



问题一:getsockopt: connection refused

2017/08/07 18:47:52 [E] [proxy.go:332] [sell] connect to local service [127.0.0.1:80] error: dial tcp 127.0.0.1:80: getsockopt: connection refused

上面我们配置pfctl转发到8080端口,当时有效,但因为重启电脑后,pfctl的配置又变回之前了,需要配置在开机启动的配置文件中。
注:开机启动需要编辑文件 /System/Library/LaunchDaemons/com.apple.pfctl.plist

<string>pfctl</string><string>-e</string><string>-f</string><string>/etc/pf.conf</string>

10.11以上系统因为增强了安全模式,导致/System/Library/LaunchDaemons/com.apple.pfctl.plist修改失败,请重启至安全模式在进行操作。

参考自:
https://toutiao.io/posts/d7ljnp/preview
http://blog.csdn.net/thc1987/article/details/53606603
http://blog.csdn.net/ilovesmj/article/details/51549119
http://www.cnblogs.com/fullstack-yang/p/6223960.html