Mac上设置端口转发的方法

使用IDEA开发时,会经常需要在本地启动tomcat服务器,并且要使用80端口。使用1024以下的端口需要root权限,这样就比较麻烦了。可以通过设置转发规则,实现这个目的。

首先添加规则,让本机所有的80端口的请求都走到127.0.0.1:8080

$ 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 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
pf enabled
Token : 11729729078737411621

设置完成之后,可以看看这条规则是否在起作用。

$ sudo pfctl -s nat                                                                            
No ALTQ support in kernel
ALTQ related functions disabled
rdr pass inet proto tcp from any to any port = 80 -> 127.0.0.1 port 8080

接着要去/etc/hosts里面添加规则

127.0.0.2 a.b.c.com

打开一个Web服务器测试一把。

$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...

可以看到a.b.c.com:80->127.0.0.2:80->127.0.0.1:8080起作用了。

Paste_Image.png

执行下面这条命令可以删除临时添加的规则。

$ sudo pfctl -F all -f /etc/pf.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
rules cleared
nat cleared
dummynet cleared
0 tables deleted.
0 states cleared
source tracking entries cleared
pf: statistics cleared
pf: interface flags reset
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 名词延伸 通俗的说,域名就相当于一个家庭的门牌号码,别人通过这个号码可以很容易的找到你。如果把IP地址比作一间房子...
    杨大虾阅读 20,665评论 2 56
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,317评论 6 13
  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,452评论 0 36
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,010评论 6 342