美文网首页
[Nginx]nginx反向代理集群配置

[Nginx]nginx反向代理集群配置

作者: w_w_wei | 来源:发表于2019-04-09 09:06 被阅读0次

    在程序固定之后, 不对程序优化,使用固有配置的设备,
    提高并发的有效手段是添加硬件配置。

    创建一个网站并测试QPS

    先使用docker创建一个php网站。

    1. 创建目录 ~/Program/test
    2. 添加文件 index.php
    <?php
          phpinfo();
    
    1. 创建容器

    docker run -d -v ~/Program/test:/webwww/public --name web1 -p 8081:80 ssslocal/nginx-php7

    1. 访问


    2. ab工具进行测试

    $ ab -c 100 -n 10000 http://localhost:8081/                                 [23:08:11]
    This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
    ......
    Server Software:        nginx/1.10.3
    Server Hostname:        localhost
    Server Port:            8081
    
    Document Path:          /
    Document Length:        77452 bytes
    
    Concurrency Level:      100
    Time taken for tests:   30.652 seconds
    Complete requests:      10000
    Failed requests:        1052
       (Connect: 2, Receive: 0, Length: 1050, Exceptions: 0)
    Non-2xx responses:      6
    Total transferred:      775965086 bytes
    HTML transferred:       774055260 bytes
    Requests per second:    326.24 [#/sec] (mean)
    Time per request:       306.520 [ms] (mean)
    Time per request:       3.065 [ms] (mean, across all concurrent requests)
    Transfer rate:          24721.97 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    1  29.0      0    1097
    Processing:     5  304  25.8    301     561
    Waiting:        5  303  25.7    300     560
    Total:          5  305  40.1    301    1490
    ......
    

    可以看到QPS326

    组建一个nginx反向代理集群

    1. 使用容器创建第二个网站

    docker run -d --name web2 -v ~/Program/test:/webwww/public
    -p 8082:80 ssslocal/nginx-php7

    1. 添加集群配置文件
      • 先获取配置
      $ docker run --name tmp-nginx-container -d nginx
      $ docker cp tmp-nginx-container:/etc/nginx/nginx.conf ~/Program/test/nginx.conf
      $ docker rm -f tmp-nginx-container
      
      修改配置 nginx.conf 本次测试无需修改
      添加配置, ~/Program/test/default.conf
      upstream hello{
          server web1:80 weight=1;
          server web2:80 weight=1;
      }
      server {
          listen  80;
          server_name  www.if404.com;
          access_log /var/log/nginx/access.log main;
          error_log /var/log/nginx/error.log error;
          location / {
                 proxy_set_header  Host  $http_host;
                  proxy_set_header  X-Real-IP  $remote_addr;
                  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_pass  http://hello;
          }
      }
      
    2. 启动一个集群nginx容器

    docker run -d --name web -v ~/Program/test/default.conf:/etc/nginx/conf.d/default.conf --link web1:web1 --link web2:web2 -p 8083:80 nginx

    验证

    $ ab -c 100 -n 10000 http://localhost:8083/                                  [0:10:32]
    This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
    ......
    Server Software:        nginx/1.15.9
    Server Hostname:        localhost
    Server Port:            8083
    
    Document Path:          /
    Document Length:        77705 bytes
    
    Concurrency Level:      100
    Time taken for tests:   29.604 seconds
    Complete requests:      10000
    Failed requests:        9098
       (Connect: 0, Receive: 0, Length: 9098, Exceptions: 0)
    Non-2xx responses:      5
    Total transferred:      778581151 bytes
    HTML transferred:       776671296 bytes
    Requests per second:    337.80 [#/sec] (mean)
    Time per request:       296.035 [ms] (mean)
    Time per request:       2.960 [ms] (mean, across all concurrent requests)
    Transfer rate:          25683.86 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.4      0       6
    Processing:     9  295 105.7    302     567
    Waiting:        9  292 105.5    300     563
    Total:          9  295 105.6    303     567
    ......
    

    可以看到QPS337

    相关文章

      网友评论

          本文标题:[Nginx]nginx反向代理集群配置

          本文链接:https://www.haomeiwen.com/subject/rnimiqtx.html