Linux TCP连接建立非常慢

Linux TCP连接建立非常慢

我不确定哪里出了问题,但我的服务器上的每个传出网络连接都非常慢(在下面的代码片段中称为 dikkenek)。似乎问题出在连接建立上。

bicou@dikkenek:~$ time ping -c 1 free.fr
PING free.fr (212.27.48.10) 56(84) bytes of data.
64 bytes from www.free.fr (212.27.48.10): icmp_seq=1 ttl=58 time=12.1 ms

--- free.fr ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 12.185/12.185/12.185/0.000 ms

real    0m10.025s
user    0m0.000s
sys     0m0.000s

如您所见,连接非常好 (到我的 ISP 主页需要 12 毫秒),但是运行 ping 却花了 10 秒钟。

直接 ping IP 要快得多:

bicou@dikkenek:~$ time ping -c 1 212.27.48.10
PING 212.27.48.10 (212.27.48.10) 56(84) bytes of data.
64 bytes from 212.27.48.10: icmp_seq=1 ttl=58 time=12.0 ms

--- 212.27.48.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 12.045/12.045/12.045/0.000 ms

real    0m0.013s
user    0m0.000s
sys     0m0.000s

因此,我认为我遇到了名称解析问题,但它也很快:

bicou@dikkenek:~$ time host free.fr
free.fr has address 212.27.48.10
free.fr has IPv6 address 2a01:e0c:1::1
free.fr mail is handled by 20 mx2.free.fr.
free.fr mail is handled by 10 mx1.free.fr.

real    0m3.022s
user    0m0.008s
sys     0m0.000s

好的,它花了 3 秒,但这并不比一台快速的机器花费的时间长,见下文。

作为比较,以下是我的笔记本电脑(Mac OS X)上运行的相同命令:

<9> Mon May 11 23:05:42 ~ $ time ping -c 1 free.fr
PING free.fr (212.27.48.10): 56 data bytes
64 bytes from 212.27.48.10: icmp_seq=0 ttl=58 time=15.194 ms

--- free.fr ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 15.194/15.194/15.194/0.000 ms

real    0m0.026s
user    0m0.001s
sys     0m0.003s
<10> Mon May 11 23:05:48 ~ $ time ping -c 1 212.27.48.10
PING 212.27.48.10 (212.27.48.10): 56 data bytes
64 bytes from 212.27.48.10: icmp_seq=0 ttl=58 time=44.084 ms

--- 212.27.48.10 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 44.084/44.084/44.084/0.000 ms

real    0m0.060s
user    0m0.001s
sys     0m0.003s
<11> Mon May 11 23:06:09 ~ $ time host free.fr
free.fr has address 212.27.48.10
free.fr has IPv6 address 2a01:e0c:1::1
free.fr mail is handled by 20 mx2.free.fr.
free.fr mail is handled by 10 mx1.free.fr.

real    0m3.059s
user    0m0.006s
sys     0m0.006s

ping 速度快了很多。

简单的 HTTP 连接也非常慢:

bicou@dikkenek:~$ time curl -sI 'free.fr'
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 11 May 2015 21:10:47 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://portail.free.fr/


real    0m5.548s
user    0m0.008s
sys     0m0.000s

我的笔记本电脑:

<14> Mon May 11 23:08:11 ~ $ time curl -sI 'free.fr'
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 11 May 2015 21:12:04 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://portail.free.fr/


real    0m0.043s
user    0m0.005s
sys     0m0.004s

两者之间的 iperf (双向) 都非常好:

bicou@dikkenek:~$ iperf -c 192.168.0.13 -r
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
------------------------------------------------------------
Client connecting to 192.168.0.13, TCP port 5001
TCP window size: 85.0 KByte (default)
------------------------------------------------------------
[  5] local 192.168.0.10 port 55416 connected with 192.168.0.13 port 5001
[ ID] Interval       Transfer     Bandwidth
[  5]  0.0-10.2 sec  8.50 MBytes  7.01 Mbits/sec
[  4] local 192.168.0.10 port 5001 connected with 192.168.0.13 port 55140
[  4]  0.0-10.4 sec  16.4 MBytes  13.3 Mbits/sec

由于笔记本电脑仅通过 WiFi 连接,因此吞吐量不是很好。服务器使用 1Gbps 以太网电缆连接到路由器。

iperf 与外界的通信正如广告所说的那样(ISP 说 1.1Mbps UL / 31.5Mbps DL):

bicou@dikkenek:~$ iperf -c iperf.testdebit.info -r
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
------------------------------------------------------------
Client connecting to iperf.testdebit.info, TCP port 5001
TCP window size: 85.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.0.10 port 37395 connected with 89.84.127.54 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-11.0 sec  1.88 MBytes  1.43 Mbits/sec
[  5] local 192.168.0.10 port 5001 connected with 89.84.127.54 port 33129
[  5]  0.0-10.2 sec  30.4 MBytes  24.9 Mbits/sec

我主要担心的是 http 速度太慢,它几乎总是晚 5.5 秒。

服务器运行 Debian 8 jessie,笔记本电脑运行 Mac OS X.10 Yosemite。

我可以使用什么工具来解决 Debian 速度缓慢的问题?


编辑:正如 Otheus 指出的那样,这可能是 DNS 问题。以下测试可以证实这一点:

bicou@dikkenek:~$ time curl -sIH 'Host: free.fr' 212.27.48.10
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 11 May 2015 21:57:19 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Location: http://portail.free.fr/


real    0m0.034s
user    0m0.004s
sys     0m0.000s

现在我该如何加快速度呢?

答案1

您的 DNS 配置混乱。请检查/etc/resolv.conf并确保仅列出了有效的服务器。

相关内容