老师,limit_req可以起到限制的作用,但是limit_conn好像没有起到作用,可以帮忙看下不?

来源:2-26 Nginx的请求限制_请求限制配置语法

慕移动9175522

2020-04-13

我的nginx配置是这样的:

limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;

server {
    listen      80;
    server_name  www.fdl.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /opt/app/code/pro1;
        index  index.html index.htm;

        limit_conn conn_zone 1;
        #limit_req zone=req_zone;
        #limit_req zone=req_zone burst=3 nodelay;

}

然后我的结果是这样的:

$ ./ab.exe -n 500 -c 100 http://www.fdl.com/
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.fdl.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests


Server Software:        nginx/1.16.1
Server Hostname:        www.fdl.com
Server Port:            80

Document Path:          /
Document Length:        572 bytes

Concurrency Level:      100
Time taken for tests:   0.141 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      402500 bytes
HTML transferred:       286000 bytes
Requests per second:    3546.10 [#/sec] (mean)
Time per request:       28.200 [ms] (mean)
Time per request:       0.282 [ms] (mean, across all concurrent requests)
Transfer rate:          2787.71 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:    16   23   3.1     25      29
Waiting:       11   22   3.8     25      28
Total:         17   23   3.1     25      29

Percentage of the requests served within a certain time (ms)
  50%     25
  66%     26
  75%     26
  80%     26
  90%     26
  95%     27
  98%     28
  99%     29
 100%     29 (longest request)
Finished 500 requests

写回答

2回答

慕移动9175522

提问者

2020-04-14

试了好几次,发现即使请求时间超过了1s,limit_conn还是没有起到作用;
稍微超过一些并发数或者请求数,就会报错:

这是并发数稍微大一点的情况:

[root@localhost conf.d]# ab -n 10000 -c 1500 http://www.fdl.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.fdl.com (be patient)
socket: Too many open files (24)


///////////////////////////////////////////////////////////////////

这是可以正常压测的情况:

[root@localhost conf.d]# ab -n 10000 -c 1000 http://www.fdl.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.fdl.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.16.1
Server Hostname:        www.fdl.com
Server Port:            80

Document Path:          /
Document Length:        463 bytes

Concurrency Level:      1000
Time taken for tests:   2.693 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      6960000 bytes
HTML transferred:       4630000 bytes
Requests per second:    3712.70 [#/sec] (mean)
Time per request:       269.346 [ms] (mean)
Time per request:       0.269 [ms] (mean, across all concurrent requests)
Transfer rate:          2523.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  122 274.2     48    1080
Processing:     9   75  31.8     72     313
Waiting:        1   55  26.5     52     270
Total:         31  197 285.0    119    1356

Percentage of the requests served within a certain time (ms)
  50%    119
  66%    155
  75%    167
  80%    174
  90%    209
  95%   1153
  98%   1173
  99%   1187
 100%   1356 (longest request)

0
0

Jeson

2020-04-14

//img1.sycdn.imooc.com/szimg/5e95138f09d1ef0c07720044.jpg

一次的并发请求都没有超过1s中。

建议:

加大并发、总请求数目。

或者增加网络延迟方式、增加页面响应延迟的方式作测试。

0
0

Nginx入门到实践-Nginx中间件应用+搭建Webserver架构

中间件、负载均衡、应用层安全防护、动静分离、Nginx+LUA开发等

4183 学习 · 908 问题

查看课程