我在将负载均衡器(唯一具有外部IP地址的入口点)的8000端口转发到具有内部IP的Web服务器(到端口8000)时遇到了问题。
所以我需要XX.XX.XX.XX:8000 – > YY.YY.YY.YY:8000其中XX.XX.XX.XX是外部IP,YY.YY.YY.YY是内部的。
当通过sshlogin到XX.XX.XX.XX时,可以执行telnet YY.YY.YY.YY 8000,并且连接成功。 这是我启动的iptables命令(在XX.XX.XX.XX上):
iptables -F iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -A PREROUTING -t nat -p tcp --dport 8000 -j DNAT --to YY.YY.YY.YY:8000 iptables -A FORWARD -p tcp -d YY.YY.YY.YY --dport 8000 -j ACCEPT
这里是iptables -L输出:
Chain INPUT (policy ACCEPT 13486 packets, 6361K bytes) num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 122 6968 ACCEPT tcp -- any any anywhere YY.YY.YY.YY tcp dpt:irdmi Chain OUTPUT (policy ACCEPT 14248 packets, 8532K bytes) num pkts bytes target prot opt in out source destination Chain acctboth (0 references) num pkts bytes target prot opt in out source destination
所以规则已经被添加了,并且有一些数据包通过它。 但是,当连接到XX.XX.XX.XX:8000时,我仍然从浏览器中获取连接超时
有人可以指出我的错误? 提前致谢!
PS。 路由-n从负载均衡器输出 – 我把这些规则:
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 173.199.160.147 0.0.0.0 255.255.255.255 UH 0 0 0 eth1 173.199.160.146 0.0.0.0 255.255.255.255 UH 0 0 0 eth1 173.199.160.145 0.0.0.0 255.255.255.255 UH 0 0 0 eth1 173.199.160.144 0.0.0.0 255.255.255.255 UH 0 0 0 eth1 96.30.32.0 0.0.0.0 255.255.255.192 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1 172.16.0.0 0.0.0.0 255.252.0.0 U 0 0 0 eth0 0.0.0.0 96.30.32.1 0.0.0.0 UG 0 0 0 eth1
YY.YY.YY.YY实际上是172.17.4.10。
ifconfig输出,只有两个接口可能影响到这一切:
eth0 Link encap:Ethernet HWaddr 00:25:90:53:1F:A8 inet addr:172.17.4.163 Bcast:172.19.255.255 Mask:255.252.0.0 inet6 addr: fe80::225:90ff:fe53:1fa8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:111646 errors:0 dropped:0 overruns:0 frame:0 TX packets:79057 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:63144265 (60.2 MiB) TX bytes:11600837 (11.0 MiB) Interrupt:217 Memory:fb900000-fb920000 eth1 Link encap:Ethernet HWaddr 00:25:90:53:1F:A9 inet addr:96.30.32.10 Bcast:96.30.32.63 Mask:255.255.255.192 inet6 addr: fe80::225:90ff:fe53:1fa9/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:116136 errors:0 dropped:0 overruns:0 frame:0 TX packets:101365 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:16607792 (15.8 MiB) TX bytes:89471131 (85.3 MiB) Interrupt:233 Memory:fba00000-fba20000
ip_forwarding:
root@load1 [~]# cat /proc/sys/net/ipv4/conf/eth0/forwarding 1 root@load1 [~]# cat /proc/sys/net/ipv4/conf/eth1/forwarding 1 root@load1 [~]# cat /proc/sys/net/ipv4/ip_forward 1
由于路由显然是启用的,另一个可能的和非常stream行的错误configuration将是172.17.4.10上的默认路由而不是172.17.4.163。 在这种情况下,传入的数据包将被正确地DNATed到172.17.4.10,但响应将通过不同的目的地被路由出去,从而得到“错误的”源IP地址。
一般来说,真正了解发生了什么的一个好方法就是运行tcpdump
tcpdump -i eth0 -v -n host 172.17.4.10
并尝试连接。 输出应该是有见识的。
你的iptables NAT规则是错误的。 将其更改为:
iptables -A PREROUTING -t nat -p tcp --dport 8000 -j DNAT --to-destination YY.YY.YY.YY:8000