MAC系统下使用1024以下端口

原因:

    在公司别的业务线前端资源不够了,让我顶上 :)。再就是他们的前端项目开的端口是80端口,然后mac系统想要使用1024以下的端口需要root权限(Linux系统也是)。但是root权限咱也不会开,咱也不敢问啊。所以就想了一解决办法---端口转发!
    既然我在80端口没有办法跑起来项目,那我就把代码里面的开发端口改成8080,然后在系统上做一下端口转发,将80端口的数据转发到8080。

将开发时的监听端口改成8080

步骤:

一、创建 anchor 文件

1.回到根目录:$ cd ~ 

2.$ pf=yes

3.$ cd /etc/pf.anchors/ 

4.管理员权限创建http文件:$ sudo vi http 

5.在vim/vi中创建如下内容

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

6.检测配置文件:$ sudo pfctl -vnf /etc/pf.anchors/http 

正常会打出配置信息 


      二、添加到主配置文件

1.修改pf的主配置文件/etc/pf.conf, 来引入这个转发规则 

在rdr-anchor “com.apple/*”下,添加如下 anchor 声明:rdr-anchor “http-forwarding” 

再在load anchor “com.apple” from “/etc/pf.anchors/com.apple”下, 添加 anchor 引入:load anchor “http-forwarding” from “/etc/pf.anchors/http” 

2.导入并允许运行 pf文件:$ sudo pfctl -ef /etc/pf.conf 

3.设置 pf 自启动

 $ sudo pfctl -e 

另外:关闭pf 命令:$ sudo pfctl -d 

注意⚠️!!另一种端口转发的方法,简单而暴力!!

$ echo"rdr pass proto tcp from any to any port {80} -> 127.0.0.1 port 8080"| sudo pfctl -Ef -pfctl: Use of -f option, could resultinflushing of rulespresentinthe main ruleset added by the system at startup.See /etc/pf.confforfurther details.

查看是否已经开启

$ sudo pfctl -s nat

关闭命令

$ sudo pfctl -F all -f /etc/pf.conf pfctl: Use of -f option, could resultinflushing of rulespresentinthe main ruleset added by the system at startup.See /etc/pf.confforfurther details.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容