1.打开IP转发功能:cho "1">/proc/sys/net/ipv4/ip_forward
2.通过以下命令可以查到返回值为1,表示打开成功
cat /proc/sys/net/ipv4/ip_forward
3.删除iptables所有规则
iptables -F
iptables -X
iptables -Z
4.vim /etc/sysconfig/iptables 以下为iptables的配置
(注意:172.XX.XXX.XXX为内网IP,非公网IP,58.XXX.XX.XX为转发目标IP)
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [3:244]
:OUTPUT ACCEPT [3:244]
-A PREROUTING -d 172.XX.XXX.XXX -p tcp -m tcp --dport 80 -j DNAT --to-destination 58.XXX.XX.XX:80
-A PREROUTING -d 172.XX.XXX.XXX -p tcp -m tcp --dport 8080 -j DNAT --to-destination 58.XXX.XX.XX:8080
-A POSTROUTING -d 58.XXX.XX.XX -p tcp -j SNAT --to-source 172.XX.XXX.XXX
COMMIT
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
#-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
#-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
-A FORWARD -d 58.XXX.XX.XX -p tcp --dport 80 -j ACCEPT
-A FORWARD -s 58.XXX.XX.XX -p tcp --sport 80 -j ACCEPT
-A FORWARD -d 58.XXX.XX.XX -p tcp --dport 8080 -j ACCEPT
-A FORWARD -s 58.XXX.XX.XX -p tcp --sport 8080 -j ACCEPT
COMMIT
5.:wq保存,然后再service iptables restart
就可以了
“`