nginx 对请求速率进行限流

本文详细介绍了Nginx的限流配置方法,包括如何设置每秒处理的请求数量和令牌桶缓存大小,以及如何通过配置无等待和等待方式来处理突发流量。并通过ab工具进行测试,展示了不同配置下限流的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境描述:

操作系统:windows 10 64 位家庭版

nginx: 1.14.2

ab: This is ApacheBench, Version 2.3 <$Revision: 655654 $>

nginx 安装路径:E:\soft3\HTTP\nginx-1.14.2

 

     location /limit {
            limit_req zone=perip burst=3;
            root   E:/soft3/HTTP/nginx-1.14.2/html/;
            index  index.html index.htm;
        }

页面路径:

 

一、niginx 配置限流


#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;

    #limit_conn_zone $binary_remote_addr zone=perip:10m;
    #limit_conn_zone $server_name zone=perip:10m;
    #limit_conn_log_level error;
    #limit_conn_status 503;
    
    limit_req_zone $binary_remote_addr zone=perip:10m rate=2r/s;
    limit_conn_log_level error;
    limit_conn_status 503;

    
    upstream user {
       server 127.0.0.1:8080;
    }
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        location /limit {
            limit_req zone=perip burst=3;
            root   E:/soft3/HTTP/nginx-1.14.2/html/;
            index  index.html index.htm;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }
        
         location /user/getToken {
           proxy_pass http://user;
        }
        
        location /user {
           proxy_pass http://user;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

二、ab 测试

E:\soft3\HTTP>ab -n 5 -c 5  http://127.0.0.1/limit/1.html

由于限流区域没有配置为延迟模式(没有配置nodelay ),ab 模拟的并发数超过限制的并发数 ab 会被阻塞,等待获得令牌。

   location /limit {
            limit_req zone=perip burst=3;
            root   E:/soft3/HTTP/nginx-1.14.2/html/;
            index  index.html index.htm;
        }

 

查看 NGINX 访问日志:

 

 

三、配置为无订单模式

location /limit {
            limit_req zone=perip burst=3 nodelay;
            root   E:/soft3/HTTP/nginx-1.14.2/html/;
            index  index.html index.htm;
        }

其他地方的配置和前面一样,只在标红部分加了 nodelay 。

 

ab 测试 ab -n 5 -c 5  http://127.0.0.1/limit/1.html

这次的测试结果与前面不同,ab 不会出现阻塞。nginx 允许突发流量(突发流量等于桶的容量,桶容量为3)。前面4个访问正常(ngx_http_limit_req_module不够精确所以多了一个访问成功),后面6个比限流返回503。

 

 

 

结论:
配置每秒处理2个请求,令牌桶缓存10
1. 使用无等待方式限流,令牌桶中必须缓存有令牌,如果令牌桶中一个令牌也没有,虽然
   配置每秒处理2个请求,但实际会所有的请求都被限流(拒绝)。如果令牌桶中已经缓存
   10个令牌,这个时候突发10个请求,10个请求都会成功。

2. 使用无等待方式限流,如果令牌桶中只有一个令牌,虽然 配置每秒处理2个请求,但实际
   只有一个请求会成功,其他请求回被拒绝。

3. 使用等待方式限流,如果令牌桶中只有一个令牌,虽然 配置每秒处理2个请求,但实际
   只有一个请求会成功,2到10个请求会等待获得令牌,获得令牌后会请求会处理成功。
   超过10个的其他请求都会拒绝。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值