为什么 VPS 上的 TLS 握手需要*永远*(20 秒)?

为什么 VPS 上的 TLS 握手需要*永远*(20 秒)?

我有一个服务器,通常运行良好,但在尝试使用 SSL 连接时会卡住 20 秒(SSH 或 HTTPS 都显示相同的模式。)

我尝试了各种不使用 SSL 的连接,例如 telnet:

telnet server-name 80

输入了 GET 命令

GET http://server-name/
Host: server-name
Accept: text/html, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

并且答复是100%即时的。

但是,当我尝试通过浏览器中的 HTTPS 连接到服务器,或者使用 SSH 连接到同一台服务器时,它会等待大约 20 秒才连接。

对于 SSH,它将工作得很好(即不再慢了)。对于 HTTPS,每次必须再次连接时它都会很慢。但是,没有关闭的连接将继续快速运行。

我添加了一张 Firebug 中显示的 Net 信息的截图。我们可以看到,每次尝试新连接时,需要 20 秒。使用 htop 查看服务器使用情况时,CPU 为 0%,当发生某些事情时,1 分钟使用情况报告中的 CPU 使用率会达到惊人的 0.01%。因此,服务器总体上没有受到任何限制(即,此时它是一台测试服务器,我们还没有收到其他服务器的访问。)

在此处输入图片描述

所以我的问题是:什么原因可能导致这种放缓?

我以为可能是 OpenSSL 试图使用 /dev/random,但我以前从未听说过这样的问题。随机设备在该 VPS 上根本没有输出太多内容。但是,/dev/urandom 工作得很好。我可以在几秒钟内获得 1Mb 的随机数据。我可以研究什么来解决这个问题?


/etc/resolv.conf看起来像 Google DNS。应该很快……

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 8.8.4.4

请注意,Apache2 设置本身并不直接相关,因为它也会发生在 SSH 上...


我也尝试过ssh -vvv。连接是即时的。

OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /home/alexis/.ssh/config
debug1: /home/alexis/.ssh/config line 202: Applying options for do-nia2match
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Hostname has changed; re-reading configuration
debug1: Reading configuration data /home/alexis/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to 178.62.213.172 [178.62.213.172] port 22.
debug1: Connection established.
...
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/alexis/.ssh/do-nia2match_rsa, explicit

SUPER LONG PAUSE HAPPENS HERE (~20s)

debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred publickey,keyboard-interactive
debug3: authmethod_lookup publickey
...

我不太确定为什么在发送密钥时 SSH 服务器会挂起 20 秒......

答案1

我发现(很久以前——见评论)我阻止了除 127.0.0.1 地址之外的所有 127.0.0.0 地址。

Debian 和 Ubuntu 在您的/etc/hosts127.0.1.1 中定义了一个包含您的域名的条目。您应该在文件开头看到类似以下内容/etc/hosts

127.0.1.1     hostname.example.com hostname

我不得不检查我的防火墙,实际上我决定打开所有 127.xxx,因为它们都是私有网络的一部分,而且这些都是安全 IP。以下是此类规则的一个示例:

-A INPUT -i lo -s 127.0.0.0/8 -j ACCEPT

表示/80.0.0部分可以是任意数量。

相关内容