OSX 10.11 El Capitan的80端口转发

升级到OSX 10.11 El Capitan版本后,在本地需要开启80端口转发到9000端口以测试web程序可以这样做:

使用pfctl实现该需求

  1. 创建anchor文件 /etc/pf.anchors/<CUSTOM NAME>:
rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 9000
rdr pass on lo0 inet proto tcp from any to any port 443 -> 127.0.0.1 port 9001
  1. 创建pfctl配置文件 /etc/pf-<CUSTOM NAME>.conf:
rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/<CUSTOM NAME>"
  1. 应用规则,开启转发:
sudo pfctl -ef /etc/pf-<CUSTOM NAME>.conf

如果想取消端口转发,执行下列命令:

sudo pfctl -df /etc/pf-<CUSTOM NAME>.conf

设置为系统开启自动开启该功能

  1. 创建文件 /Library/LaunchDaemons/com.apple.pfctl-<CUSTOM NAME>.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
     <key>Label</key>
     <string>com.apple.pfctl-<CUSTOM NAME></string>
     <key>Program</key>
     <string>/sbin/pfctl</string>
     <key>ProgramArguments</key>
     <array>
          <string>pfctl</string>
          <string>-e</string>
          <string>-f</string>
          <string>/etc/pf-<CUSTOM NAME>.conf</string>
     </array>
     <key>RunAtLoad</key>
     <true/>
     <key>KeepAlive</key>
     <false/>
</dict>
</plist>
  1. 运行下列命令使得之后开启自动运行
sudo launchctl load -w /Library/LaunchDaemons/com.apple.pfctl-<CUSTOM NAME>.plist
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简介 本文将介绍在局域网内,如何监听受害者流量并通过 SSLstrip 攻击获取敏感信息,分为如下两步: 中间人攻...
    码可波罗阅读 25,786评论 3 41
  • 在web开发过程中,如果是前后端分离进行开发,前端资源发布到服务器后,访问前端页面,前端页面请求服务端资源,在这种...
    上课睡觉觉阅读 1,813评论 0 51
  • mac与linux一样,1024以下的端口为特权端口,只有root用户才有权监听。 因此要使用80端口要么使用ro...
    十二月的水瓶座阅读 1,221评论 0 0
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,934评论 0 0
  • 晨兴理荒秽,带月荷锄归。 ——陶渊明《归园田居》 如果有一种生活方式叫做不食人间烟火,那么它一定...
    希圣阅读 503评论 0 3