我有一个静态资产约为1.5mb和drop的角形网站,它大约400 kb,我有nginx作为我的when服务器和API服务器的反向代理,当我使用Apache基准测试工具测试nginx时,如果我测试https站点比http (https比http(https慢10倍)& cpu利用率和内存一点都不高(cpu 30%的内存只有1mb!),我发现性能会大幅下降!
我已经搜索了几个小时,尝试了所有可能的增强,但都没有起作用,就我所读到的https在现代web服务器上的速度不会太慢(http大约1500 req/sec & https对于Nginx是46 req/秒),这主要来自Nginx https非常高的连接时间,但我不知道如何解决这个问题。
有人能建议如何改进这一点吗?
(令我惊讶的是,Apache在这两种情况下的性能都要好得多,但如果我将并发连接设置为200以上,则不会做出响应)&这不是nginx对apache,我只是在说明我的情况。
Important注:
我不是在比较不是这个站点重点的2台web服务器,但是如果nginx中的https比Apache慢10倍的话,它们的性能通常是<#>so。我觉得我的Nginx配置有问题&我想修复它。
所有测试都在我的windows机器i7 & 16 gb内存上进行。
仅限Nginx http:
C:\Apache24\bin>ab -n 5000 -c 200 http://localhost:8100/abc/index.html?param=abc
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Server Software: nginx/1.15.4
Server Hostname: localhost
Server Port: 8100
Document Path: /abc/index.html?param=abc
Document Length: 1099 bytes
Concurrency Level: 200
Time taken for tests: 3.246 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 6665000 bytes
HTML transferred: 5495000 bytes
Requests per second: 1540.32 [#/sec] (mean)
Time per request: 129.843 [ms] (mean)
Time per request: 0.649 [ms] (mean, across all concurrent requests)
Transfer rate: 2005.12 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.3 0 16
Processing: 31 87 12.8 94 124
Waiting: 0 87 13.7 94 124
Total: 31 87 12.8 94 124
Percentage of the requests served within a certain time (ms)
50% 94
66% 94
75% 94
80% 94
90% 99
95% 109
98% 109
99% 113
100% 124 (longest request)Nginx (启用http2 )
C:\Apache24\bin>abs -n 5000 -c 200 https://localhost:8200/abc/index.html?param=abc
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Server Software: nginx/1.15.4
Server Hostname: localhost
Server Port: 8200
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
TLS Server Name: localhost
Document Path: /abc/index.html?param=abc
Document Length: 1099 bytes
Concurrency Level: 200
Time taken for tests: 108.985 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 6780000 bytes
HTML transferred: 5495000 bytes
Requests per second: 45.88 [#/sec] (mean)
Time per request: 4359.386 [ms] (mean)
Time per request: 21.797 [ms] (mean, across all concurrent requests)
Transfer rate: 60.75 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 16 4201 506.8 4251 4755
Processing: 0 32 12.6 31 88
Waiting: 0 32 12.6 31 88
Total: 62 4232 506.9 4283 4800
Percentage of the requests served within a certain time (ms)
50% 4283
66% 4342
75% 4413
80% 4439
90% 4484
95% 4547
98% 4694
99% 4727
100% 4800 (longest request)与Apache http相比(这里的CPU利用率在90 %到100%之间)
C:\Apache24\bin>ab -n 5000 -c 200 http://localhost:6200/abc/index.html?param=abc
Server Software: Apache/2.4.33
Server Hostname: localhost
Server Port: 6200
Document Path: /abc/index.html?param=abc
Document Length: 1099 bytes
Concurrency Level: 200
Time taken for tests: 1.781 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 6810000 bytes
HTML transferred: 5495000 bytes
Requests per second: 2806.99 [#/sec] (mean)
Time per request: 71.251 [ms] (mean)
Time per request: 0.356 [ms] (mean, across all concurrent requests)
Transfer rate: 3733.51 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.6 0 16
Processing: 16 69 16.0 63 125
Waiting: 0 57 16.0 63 125
Total: 16 69 16.0 63 125
Percentage of the requests served within a certain time (ms)
50% 63
66% 78
75% 78
80% 78
90% 94
95% 94
98% 94
99% 109
100% 125 (longest request)Apache如下所示(http 1.1) &请注意,nginx中的HTTP1.1并没有提高性能:
C:\Apache24\bin>abs -n 5000 -c 200 https://localhost:7200/abc/index.html?param=abc
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Server Software: Apache/2.4.33
Server Hostname: localhost
Server Port: 7200
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
TLS Server Name: localhost
Document Path: /abc/index.html?param=abc
Document Length: 1099 bytes
Concurrency Level: 200
Time taken for tests: 8.747 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 6810000 bytes
HTML transferred: 5495000 bytes
Requests per second: 571.60 [#/sec] (mean)
Time per request: 349.894 [ms] (mean)
Time per request: 1.749 [ms] (mean, across all concurrent requests)
Transfer rate: 760.27 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 198 42.7 188 391
Processing: 62 145 39.1 140 385
Waiting: 0 76 28.3 78 250
Total: 62 343 63.0 331 615
Percentage of the requests served within a certain time (ms)
50% 331
66% 369
75% 380
80% 389
90% 422
95% 465
98% 500
99% 536
100% 615 (longest request)我的nginx配置:
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8100;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8200 ssl http2;
server_name localhost;
ssl_certificate C:/nginx-1.13.12/conf/server.crt;
ssl_certificate_key C:/nginx-1.13.12/conf/server.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
gzip on;
gzip_comp_level 1;
gzip_vary on;
gzip_types
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_static on;
location /ipo_reits/ {
root html;
index index.html index.htm;
## here we redirect to the homepage in case of nginx 404
try_files $uri $uri/ /ipo_reits/index.html;
# error_page 404 =301 /;
}
location /api/ {
proxy_pass https://localhost:7001/;
}
}
}<#>更新
我已经按照kworr & nginx的建议在Linux上进行了测试,在https中性能要好得多。
Nginx http: 17000 req/sec和https大约1200 req/s,apache在https 1150 req/sec和http 13000 req/sec之间具有类似的性能比。
为什么两台web服务器中的http和https之间有这么大的差异?
Http比https快12倍,尽管许多引用都确保了差异可以忽略不计?
发布于 2018-10-06 20:51:57
对于这个简单的测试用例,HTTPS将比HTTP慢得多。
HTTPS意味着两件事:
现在第2点是可以忽略不计的,这些天与正确的设置和大多数交通。大量的流量(例如,如果你主要发送的是像Netflix或YouTube这样的视频)可能会注意到不同之处,但大多数网站不会真的注意到,除非是在非常过时的硬件上,或者设置得非常糟糕。
然而,第一点是有趣的。您正在使用的TLSv1.2需要2次完整的往返行程,以便在TCP连接所需的1.5次往返和实际HTTP请求的1次往返的基础上建立HTTPS连接。TLSv1.3将此简化为一次往返(如果重新建立连接的话,甚至为零),这也是人们对它感到兴奋的原因之一,现在它开始在实现中可用。
因此,就目前而言,我预计HTTPS的速度至少是HTTP的两倍--完全忽略加密处理本身的成本,假设网页的下载量很小,因为您正在比较4.5次往返和2.5次往返。
然而,计算确实要付出代价。HTTPS使用基于站点提供的TLS证书的公钥非对称加密。这是必要的,因为你没有预先商定的密钥,你可以使用在世界上的每一个网站(特别是那些由尚未创建的!)所以你不能使用更快的对称加密。非对称加密是众所周知的缓慢,因此,为了帮助抵消,这只用于建立一个对称密钥,然后用于其余的加密。然而,这意味着初始握手将再次慢得多,因为它正在执行昂贵的公钥加密和解密。
老实说,我从来没有用ab度量过这种差异,但与使用HTTP相比,由于这些原因,速度慢了10-13倍,我并不感到完全惊讶,因为HTTP只需要建立TCP连接并将页面发回。
那么,为什么会有使用HTTPS的网站呢?为什么大多数人会说性能影响并不大呢?!!那是因为很少有网站只加载一个页面。一个网站通常是由无数的资源(平均75)组成的,每个域名的第一个都会受到握手延迟的影响。
因此,您的ab测试并不代表真实的单词(除非您有一个非常简单的网站)。相反,您应该使用wget -p或类似的方式下载网页和依赖项。然后时间,只有那时,你才能看到什么是不同的。我仍然期望有一个不同,但不像你看到的那样简单的ab测试。
然而,即使是这样,也不具有代表性,因为下载只是其中的一部分。如果下载你的网站需要1秒的时间,而所有的JavaScript都需要15秒的时间,那么就给tracking...etc做广告。那么,即使将这1秒的下载时间增加一倍,也不会产生实质性的影响。因此,要成为一个真正准确的测试,您应该测量下载和渲染,以获得一个真正的成本。像万维网这样的工具可以在这方面有所帮助。
您还应该确保您的对HTTPS设置进行了优化.使用更快的密码,确保启用了TLS恢复(因此任何后续连接都不会受到很大的性能损失)和其他设置。
最终,我们将进入HTTPS世界,这是有充分理由的,虽然对最初的设置有性能影响,但是也有其他的性能好处,比如Brotli压缩和HTTP/2,它们只能在HTTPS上使用(因为思想和实际的原因)。在性能方面,这些都会把HTTP留在尘土中通常会显示这一点(比如我的一个),通常是基于人工测试,而这些测试并不真正代表大多数网站,所以大多数网站的性能都不会得到很大的改善!
https://serverfault.com/questions/933627
复制相似问题