Nginx https 连接时间非常长,比 Nginx http 慢得多(32 倍),比 Apache https 慢 12 倍

Nginx https very high connect time and is much slower (32 times) than Nginx http & 12 times slower than Apache https

我有一个 Angular 网站,其静态资产约为 1.5 mb,gzip 压缩后约为 400 kb,当我测试时,我将 nginx 作为我的网络服务器和 API 服务器的反向代理nginx 与 Apache 基准测试工具,如果我测试 https 站点与 http 相比,我发现性能大幅下降(https 慢 10 倍)并且 cpu 利用率和内存根本不高(cpu 30% 的内存只有 1 MB!!)

我已经搜索了几个小时并尝试了所有可能的改进,但 none 有效,据我所知,https 在现代网络服务器上不会慢很多(http 大约 1500 req/sec & https 是 46 req/sec 对于 nginx),这主要是由于 Nginx https 非常高的连接时间,但我不知道如何解决这个问题。

有人可以建议如何改进吗?

(同样令我惊讶的是,Apache 在这两种情况下都表现得更好,但如果我将并发连接设置为超过 200,则不会响应)这不是 nginx vs apache 我只是在陈述我的情况。

重要提示:

我不是在比较这两个不是本网站重点的 Web 服务器,但通常它们具有相当的性能 所以如果 nginx 中的 https 比 Apache 慢 10 倍我感觉我的 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 https(启用 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 https 如下 (http 1.1) & 请注意 nginx 中的 http 1.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/;
        }
    }
}

避免使用旧密码套件 HTTP/2 有大量旧的和不安全的密码黑名单,因此我们必须避免使用它们。密码套件是一组密码算法,描述了传输数据应该如何加密。

我们将使用非常流行的密码集,其安全性已获得 CloudFlare 等互联网巨头的认可。它不允许使用 MD5 加密(自 1996 年以来就被认为是不安全的,但尽管如此,它的使用至今仍在广泛使用)。

打开以下配置文件:

sudo nano /etc/nginx/nginx.conf
Add this line after ssl_prefer_server_ciphers on;.
/etc/nginx/nginx.conf
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;

保存文件,退出文本编辑器。

再次检查配置是否存在语法错误:

sudo nginx -t

我希望这对其他人有帮助,似乎与 nginx on windows issue 有关,我错误地认为 nginx on windows & linux 的性能是相似但显然不是。

我在同一台机器上 Linux 上用 nginx 再次尝试了基准测试并获得了出色的性能,如下所示

ab -n 5000 -c 200 https://localhost:8200/abc/index?param=abc
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>

Finished 5000 requests

Server Software:        nginx/1.10.3
Server Hostname:        localhost
Server Port:            8200
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256

Document Path:          /abc/index?param=abc
Document Length:        1099 bytes

Concurrency Level:      200
Time taken for tests:   4.179 seconds
Complete requests:      5000
Failed requests:        0
Total transferred:      6825000 bytes
HTML transferred:       5495000 bytes
Requests per second:    1196.37 [#/sec] (mean)
Time per request:       167.173 [ms] (mean)
Time per request:       0.836 [ms] (mean, across all concurrent requests)
Transfer rate:          1594.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       15  141 185.3    106    1322
Processing:     1   22  13.1     20      82
Waiting:        1   14   9.5     13      81
Total:         24  163 185.7    128    1351

Percentage of the requests served within a certain time (ms)
  50%    128
  66%    142
  75%    148
  80%    155
  90%    208
  95%    260
  98%   1100
  99%   1164
 100%   1351 (longest request)

对于持续更高的负载和并发,性能仍然相同:

ab -n 25000 -c 1000 https://localhost:8200/abc/index?param=abc
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>

Benchmarking localhost (be patient)
Completed 2500 requests
....
Completed 25000 requests
Finished 25000 requests


Server Software:        nginx/1.10.3
Server Hostname:        localhost
Server Port:            8200
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256

Document Path:          /abc/index?param=abc
Document Length:        1099 bytes

Concurrency Level:      1000
Time taken for tests:   20.149 seconds
Complete requests:      25000
Failed requests:        0
Total transferred:      34125000 bytes
HTML transferred:       27475000 bytes
Requests per second:    1240.76 [#/sec] (mean)
Time per request:       805.960 [ms] (mean)
Time per request:       0.806 [ms] (mean, across all concurrent requests)
Transfer rate:          1653.94 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        7  687 711.8    492    7694
Processing:     2   89  50.1     81     516
Waiting:        0   57  48.9     41     509
Total:         15  776 723.4    600    7756

Percentage of the requests served within a certain time (ms)
  50%    600
  66%    812
  75%   1095
  80%   1186
  90%   1397
  95%   1631
  98%   3183
  99%   3442
 100%   7756 (longest request)