美文网首页
OSX 10.11 El Capitan的80端口转发

OSX 10.11 El Capitan的80端口转发

作者: 维生素CC | 来源:发表于2016-05-16 12:56 被阅读477次

    升级到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
    

    相关文章

      网友评论

          本文标题:OSX 10.11 El Capitan的80端口转发

          本文链接:https://www.haomeiwen.com/subject/wyjbrttx.html