mac系统nginx监听80端口无效

330 阅读1分钟

问题:mac系统nginx无法监听80端口

原因:mac系统不能监听小于1024的端口

解决问题

一、设置端口转发

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  
//这个配置是说80端口转发到8080  【比如:本地启动服务8080端口,浏览器访问:http://127.0.0.1】

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
    //出现以上代码表示转发端口启动成功
关闭端口转发功能 运行命令:sudo pfctl -d

启动成功之后,nginx就可以监听listen 8080;端口,浏览器访问127.0.0.1:80端口了