nginx 开启连接数限制

阿里云默认已经有安全防护,不需要自己再定义。

 

首先查看,配置文件,可以看出,这两个模块,已经内置 ,不需要另外安装。

./configure --help | grep http_limit_

  --without-http_limit_conn_module   disable ngx_http_limit_conn_module

  --without-http_limit_req_module    disable ngx_http_limit_req_module

 

在conf文件中配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

upstream report{
server 127.0.0.1:8080;

check interval=3000 rise=2 fall=5 timeout=2000 type=http;
check_http_expect_alive http_2xx http_3xx;
}
#限制请求
limit_req_zone $binary_remote_addr  zone=one:20m rate=5r/s;
##按ip配置一个连接 zone
limit_conn_zone $binary_remote_addr zone=perip_conn:10m;
##按server配置一个连接 zone
limit_conn_zone $server_name zone=perserver_conn:100m;



    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
 #请求限流排队通过 burst默认是0,若是5,太小,导致服务器报503错误,css,js加载不全
limit_req zone=one burst=25;
 #连接数限制,每个IP并发请求为2
limit_conn perip_conn 2;
#服务所限制的连接数(即限制了该server并发连接数量)
limit_conn perserver_conn 1000;
#连接限速
limit_rate 100k;
proxy_pass      http://report;
#
        }

       
      location /status {
      check_status;
       }
    }
}

 

posted @ 2021-05-18 09:44  琴声清幽  阅读(698)  评论(0编辑  收藏  举报