【问题标题】:Nginx https very high connect time and is much slower (32 times) than Nginx http & 12 times slower than Apache httpsNginx https 连接时间非常长,比 Nginx http 慢得多(32 倍),比 Apache https 慢 12 倍
【发布时间】:2019-03-07 11:21:05
【问题描述】:

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

我一直在寻找几个小时并尝试了所有可能的增强功能,但都没有奏效,据我所知,现代 Web 服务器上的 https 不会那么慢(http 大约 1500 req/sec 和 https 是 46 req/sec nginx),这主要来自 Nginx https 非常高的连接时间,但我不知道如何解决这个问题。

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

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

重要提示:

我没有比较不是本网站重点的 2 个 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/;
        }
    }
}

【问题讨论】:

    标签: apache nginx https webserver angular6


    【解决方案1】:

    避免使用旧密码套件 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 指南足够安全,并且在此测试中,很明显 Nginx 和 Apache正在使用相同的密码套件:TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
    【解决方案2】:

    我希望这对其他人有所帮助,似乎与windows上的nginx问题有关,我错误地认为nginx在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)
    

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 2014-10-10
      • 2017-07-23
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      相关资源
      最近更新 更多