search

Home  >  Q&A  >  body text

nginx - Using ngx_http_limit_conn_module according to the tutorial, the number of concurrent connections of the virtual host cannot be limited

I follow http://www.jb51.net/article/7... to configure the nginx.conf file to limit the number of concurrent connections to the virtual host. The following is the main content in nginx.conf.

http {

limit_conn_zone $server_name zone=perserver:10m;
include       mime.types;
default_type  application/octet-stream;

sendfile        on;
keepalive_timeout  65;

server {
    listen       54321;
    server_name  localhost;
    limit_conn perserver 100;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        root   html;
        index  index.html index.htm;
    }        
    location /status {
        stub_status on;
        access_log off;
    }
}
}


After the configuration is completed, reload the configuration file and use apache's ab tool to perform a stress test on 192.168.3.161:54321/index.html. The stress test code is as follows:
/usr/bin/ab -c 2000 -n 100000 http://192.168.3.161:54321/in...

对网站加压时,利用浏览器访问192.168.3.161:54321/status观察并发连接数,发现Active connections依然大于100,如下图所示。

Where is the problem with my configuration?

ringa_leeringa_lee2826 days ago788

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 17:16:08

    Changed my thinking.
    The limit code written before is: limit_conn_zone $server_name zone=perserver:10m;
    Using nginx’s built-in $server_name variable, it cannot successfully limit the number of concurrent connections.
    I changed to: limit_conn_zone $server_port zone=perserver:10m;
    Successfully limited the number of concurrent connections on a certain port

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 17:16:08

    You can use

    limit_conn_zone $binary_remote_addr zone=perserver:10M; 
    The form of

    $binary_remote_addr
    

    means using the user’s IP address, using $binary_remote_addr as the Key, and the IP address has a restrictive meaning. The $server_name you wrote above is not suitable as a criterion for judging the number of user connections

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 17:16:08

    Because this module is executed in the PREACCESS stage, it must have occurred in the http request processing stage after the tcp connection is established. The Active connections value above should refer to the number of concurrent tcp connections. Even if the number of concurrency is exceeded and 503 is returned, this is based on a successful TCP connection.

    reply
    0
  • Cancelreply