mac设置端口号转发到默认端口80、443

1、需要这种设置的场景

我们一些开发的时候需要将一些网站的请求拦截在本地服务下。
下文将以 test.com 为例

2、设置host
open /private/etc/hosts

添加如下内容

127.0.0.1           test.com

打开之后需要复制到桌面,改动之后再覆盖掉原来的host文件。
通过设置host可以做到将对应域名的请求到你自己的本地服务

设置成功我们就可以使用 比如 http://test.com:8080/index.html 来访问本地服务

3、设置端口转发

设置host只能将对应请求拦截在本地服务,但是请求时需要加本地服务的端口号比如
:8080
http://test.com:8080/index.html
但是我们不想加它。【手动狗头】
我们想直接访问
http://test.com/index.html
于是我们需要将我们本地服务的80端口转发为我们的本地服务端口(https默认端口为443 )

一些常用的默认端口
HTTP(超文本传输协议):80/8080/3128/8081/9080 TCP

HTTPS(安全超文本传输协议):443 TCP

FTP(文件传输协议):21 TCP

SSH(安全外壳协议):22 TCP

Telnet(远程终端协议):23 TCP

SMTP(简单邮件传输协议):25 TCP

POP3(邮局协议版本3):110 TCP

DNS(域名解析协议):53 UDP

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 

这个配置是说80端口转发到8080
443端口转发到8443
8080 、8443 这两个不是死的,你的本地服务什么端口就写什么,
也不一定要都配置,
比如如果是https
只要配置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

一顿操作之后我们终于可以直接
http://test.com/index.html

4.关闭端口转发功能

sudo pfctl -d 

或者关闭全部

pfctl -F all -f /etc/pf.conf

注意事项:
1、重启mac,需要手动重启端口转发命令

sudo pfctl -ef /etc/pf-tomcat.conf

2、一定要注意你开启的服务是否是https 服务,如果是https服务要转发到443才能成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值