连接到本地主机parsing127.0.0.1,但连接到外部IP

我有一个专用服务器的问题,我不知道如果这是默认的行为,但这是问题:

如果我使用本地主机连接到位于服务器上的服务,则服务会将外部IP作为源IP。

让我来举个例子,我使用netcat在127.0.0.1:4444上进行监听

xxxxxx # nc -vv -l -s 127.0.0.1 -p 4444 listening on [127.0.0.1] 4444 ... 

让我们检查一下是否可以:

 xxxxxx ~ # netstat -atnp | grep 4444 tcp 0 0 127.0.0.1:4444 0.0.0.0:* LISTEN 14038/nc 

好,让我们连接:

 xxxxxx ~ # nc -vv 127.0.0.1 4444 localhost [127.0.0.1] 4444 (?) open 

回到有听力过程的tty,我得到这个:

 connect to [127.0.0.1] from xxxxxx.net [176.31.xxx.xx] 50354 

所以这就是问题所在。 我有一个服务器守护进程,必须监听本地主机,并检查IP是127.0.0.1当客户端连接,但由于某种原因,当我连接到本地主机报告外部IP …

如果我对IPv6做同样的工作,例外情况是…检测连接为本地主机(:: 1)。

一些可能有用的信息:

“本地主机”解决没有问题127.0.0.1

 xxxxxx ~ # ping -c1 localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.086 ms 

我的主机文件没有什么奇怪的,我想…

 xxxxxx ~ # grep -v ^# /etc/hosts 127.0.0.1 localhost localhost.localdomain 176.31.xxx.xx xxxxxx.net ns1.xxxxxx.net ::1 ip6-localhost ip6-loopback feo0::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts 

和ifconfig报告一切正常…

 eth0 Link encap:Ethernet HWaddr e0:69:95:d8:30:a1 inet addr:176.31.xxx.xx Bcast:176.31.108.255 Mask:255.255.255.0 inet6 addr: 2001:41d0:8:xxxx::/64 Scope:Global inet6 addr: 2001:41d0:8:xxxx:x:xx:xx:xx/64 Scope:Global inet6 addr: fe80::e269:95ff:fed8:30a1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:16916 errors:0 dropped:0 overruns:0 frame:0 TX packets:16914 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:8410679 (8.0 MiB) TX bytes:10539881 (10.0 MiB) Interrupt:28 Base address:0xe000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:5570 errors:0 dropped:0 overruns:0 frame:0 TX packets:5570 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:744490 (727.0 KiB) TX bytes:744490 (727.0 KiB) 

解决了

感谢@Cakemox向我介绍了关于iptables一些build议。 我有以下规则导致了意外的行为:

 /sbin/iptables -t nat -A PREROUTING -p tcp --dport 1234 -j DNAT --to-destination xxx.xxx.xxx.xxx:1234 /sbin/iptables -t nat -A POSTROUTING -j MASQUERADE 

我将以适当的方式解决这个redirect问题,以避免将来出现问题。