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

时间:2018-10-02 10:27:04

标签: apache nginx https webserver angular6

我有一个Angular网站,其静态资产约为1.5 mb,压缩后约为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,我只是在说明我的情况。

重要说明:

我没有比较这两个Web服务器,但这不是该站点的重点,但是它们通常具有可比的性能 ,因此,如果nginx中的https比Apache慢10倍,我会觉得这是错误的我的Nginx配置,我想修复它。

所有测试都在我的Windows机器i7和16 GB RAM上进行。

仅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/;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我希望这会对其他人有所帮助,似乎与Windows问题上的nginx有关,我错误地认为Windows和linux上nginx的性能相似,但显然不是。

我在同一台计算机上的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)

答案 1 :(得分:-1)

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

我们将使用一种非常流行的密码集,该密码集的安全性已得到CloudFlare等Internet巨头的认可。它不允许使用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