Nginx控制並發連接數


  ngx_http_limit_conn_module這個模塊用於限制每個定義的key值的連接數,特別是單IP的連接數。

  不是所有的連接數都會被計數。一個符合計數要求的連接是整個請求頭已經被讀取的連接。

  控制nginx並發連接數量參數如下說明:

  limit_conn_zone參數:

  參數語法:limit_conn_zone key zone=name:szie;

  上下文:http 標簽塊

  用於設置共享區域內存,key可以是字符串、nginx自帶變量或者前兩個組合,如$binary_remove_addr、server_name為內存區域的名稱,name為內存區域的名稱,size為內存區域的大小。

  

  limit_conn參數:

  參數語法:limit_conn zone number;

  上下文:http、server、location 標簽塊

  用於指定key設置最大連接數,當超過最大連接數時,服務器會返回503錯誤。

 

  限制單IP並發連接數

  nginx配置文件如下:(紅色標記為添加或者修改內容)

  

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    #limit_req_zone $binary_remove_addr zone=one:10m rate=1r/s;
    #limit_req  zone=one burst=5;
    limit_conn_zone $binary_remove_addr zone=addr:10m; # 添加limit_conn_zone參數
    client_header_timeout 60;
    client_boby_timeout  15;
    send_timeout    60;
    client_max_boby_size  8m;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  虛擬主機配置文件,添加limit_conn參數:

[root@Nginx www_date]# cat brian.conf 
    server {
        listen       80;
        server_name  www.brian.com;
        location / {
            root   html/brian;
            index  index.html index.htm;
            limit_conn addr 1; # 設置單IP連接數為1 
        auth_basic    "brian training";
        auth_basic_user_file  /opt/nginx/conf/htpasswd;
            
        }
    location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
        access_log off;
    }
        access_log logs/brian.log main gzip buffer=128k flush=5s; 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

  測試:(使用ab測試工具進行測試)

ab -c 1 -n 10 http://127.0.0.1/            # 測試命令

  測試過程中查看nginx的訪問日志 ,這時候你會發現當並發為1時,返回值都是200 ,訪問正常

  修改測試方式:

ab -c 2 -n 10 http://127.0.0.1/

  測試過程中查看nginx的訪問日志 ,這時候你會發現當並發為2時,狀態碼200和503間隔是1:1 說明nginx已經做了並發連接的限制,對超時連接做出了503響應

以上功能可以用於服務器下載,限制每次訪問下載目錄的連接數

  限制虛擬主機總連接數:

  不僅可以限制單IP的並發連接數,還可以限制虛擬主機的總連接數,甚至可以對兩個同時限制

  nginx配置文件如下:(紅色標記為增加或者修改內容)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    #limit_req_zone $binary_remove_addr zone=one:10m rate=1r/s;
    #limit_req  zone=one burst=5;
 limit_conn_zone $binary_remove_addr zone=addr:10m;            # 添加下面紅色標記的兩項
    limit_conn_zone $server_name zone=perserver:10m;
    client_header_timeout 60;
    client_boby_timeout  15;
    send_timeout    60;
    client_max_boby_size  8m;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  虛擬主機配置文件:(紅色標記為增加或者修改內容)

[root@Nginx www_date]# cat brian.conf 
    server {
        listen       80;
        server_name  www.brian.com;
        location / {
            root   html/brian;
            index  index.html index.htm;
            #limit_conn addr 1;
           limit_conn perserver 2; # 添加此項
        auth_basic    "brian training";
        auth_basic_user_file  /opt/nginx/conf/htpasswd;
            
        }
    location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
        access_log off;
    }
        access_log logs/brian.log main gzip buffer=128k flush=5s; 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

  測試:(使用ab測試工具進行測試)

ab -c 5 -n 1000 http://127.0.0.1/    # 測試命令(並發連接數為5 訪問1000次)

  在nginx的統計日志中200和503的狀態碼出現的次數接近是2:1 說明配置生效成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM