方式一:通过防火墙
1. 查询端口映射情况
netsh interface portproxy show v4tov4
2. 查询某一个IP的所有端口映射情况
netsh interface portproxy show v4tov4 | find "[IP]"
例:
netsh interface portproxy show v4tov4 | find "192.168.1.1"
3. 增加一个端口映射
netsh interface portproxy add v4tov4 listenaddress=[外网IP/本地任意IP] listenport=[外网端口] connectaddress=[内网/远程IP] connectport=[内网端口]
例:
listenaddress=0.0.0.0 为本地IP,四个零代表本地任意IP
listenport=443 本地端口
connectaddress=43.156.232.70 需要转发的远程IP
connectport=443 远程IP
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=443 connectaddress=43.156.232.70 connectport=443
使用netsh
命令在Windows上将222端口转发到3333端口:
netsh interface portproxy add v4tov4 listenaddress=本机IP地址 listenport=222 connectaddress=本机IP地址 connectport=3333
netsh interface portproxy add v4tov4 listenaddress=172.19.166.10 listenport=53389 connectaddress=172.19.166.10 connectport=3389
4. 删除一个端口映射
netsh interface portproxy delete v4tov4 listenaddress=[外网IP] listenport=[外网端口]
例:
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=443

实验:将部署在内网的服务映射到外网
实验环境
- VMWare Workstation Pro
- 5台最小化安装的Windows虚拟机
如果Server4
为Windows,替换一下相应的命令即可
Windows的IP信息如下
网卡 | IP地址 | 子网掩码 | 默认网关 | 备注 |
---|---|---|---|---|
Ethernet0 | 192.168.50.105 | 255.255.255.0 | - | 内网网卡 |
Ethernet1 | 172.16.2.105 | 255.255.255.0 | - | 外网网卡 |
配置并查看端口映射情况
netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8081 connectaddress=192.168.50.11 connectport=8080
netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8082 connectaddress=192.168.50.12 connectport=8080
netsh interface portproxy show v4tov4
检查效果
在client
节点上
curl http://172.16.2.105:8081/index.html
curl http://172.16.2.105:8082/index.html
curl http://172.16.2.11:8080/index.html
方式二:路由和远程访问
注意:仅支持内网服务器的端口映射