3

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.

Wayne Hong
  • 33
  • 2

1 Answers1

5

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.

Alexander Altshuler
  • 2,930
  • 1
  • 17
  • 27
  • 1
    so the rewrite phase is prior to access phase, right? Is there any document explain those phase workflow? – Wayne Hong Apr 14 '21 at 03:09
  • The best I know is http://openresty.org/download/agentzh-nginx-tutorials-en.html Take a look at Nginx Directive Execution Order – Alexander Altshuler Apr 14 '21 at 06:35
  • Also https://cloud.githubusercontent.com/assets/2137369/15272097/77d1c09e-1a37-11e6-97ef-d9767035fc3e.png maybe usefull, but it is more about Lua module, but would give you great big picture – Alexander Altshuler Apr 14 '21 at 06:38