Search code examples
nginxopenresty

Why does rate limit not working when using with return directive in Nginx?


I had following nginx configuration:

worker_processes 1;
error_log logs/error.log;
events {
   worker_connections 1024;
}
http {
   limit_req_zone $request_uri zone=by_uri_6000:10m rate=6000r/s;

   server {
      listen 80;
      server_name "openresty.com";

      location = /limit-6000 {
         limit_req zone=by_uri_6000 nodelay;
         return 200 "ok"
      }
   }
}

But it is not working at all when testing using wrk:

wrk -c100 -t10 -d5s http://localhost/limit-6000
Running 5s test @ http://localhost/
  10 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     5.57ms    1.65ms  23.47ms   81.17%
    Req/Sec     1.81k   495.91     9.27k    99.60%
  90400 requests in 5.10s, 17.93MB read
Requests/sec:  17713.29
Transfer/sec:      3.51MB

But, if I change my configuration to something like below (I was using openresty in fact):

      location = /limit-6000 {
         limit_req zone=by_uri_6000 nodelay;
         default_type 'application/json';
         content_by_lua_block {
             ngx.say('{"code": 0, "msg": "成功"}')
         }

it works then, what could be the reason, I didn't see any explanation in official document.


Solution

  • nginx return directive works on rewrite phase, it immediately returns the result.

    I believe ngx_http_limit_req_module works on access phase. So with return directive ngx_http_limit_req_module doesn't have any chances to be in game.