Learn about Centmin Mod LEMP Stack today
Register Now

Nginx Nginx Limit_req based on HTTP method?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by rdan, Apr 7, 2022.

  1. rdan

    rdan Well-Known Member

    5,446
    1,408
    113
    May 25, 2014
    Ratings:
    +2,201
    Local Time:
    5:36 AM
    Mainline
    10.2
    Is it possible to have limit request based on HTTP_Method?


    Like for POST
    For GET and HEAD
    I tried putting it inside IF, nginx produce this error:
     
  2. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    7:36 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    If it's just varying the burst value, you could setup a nginx map based on method used and assign a variable value to the method then use the variable in place of the burst value
     
  3. rdan

    rdan Well-Known Member

    5,446
    1,408
    113
    May 25, 2014
    Ratings:
    +2,201
    Local Time:
    5:36 AM
    Mainline
    10.2
    Also tried that earlier today, but got this error:
    Seems burst doesn't accept a variable.
     
  4. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    7:36 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    If that doesn't work try changing the variable to the zone name and then setup several limit_req directives referencing different zones. The limit_req directives which don't match and assigned empty zone name will not trigger AFAIK Module ngx_http_limit_req_module
     
  5. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    7:36 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    And if that doesn't work, I know turning the limit_req_zone's key (i.e.
    $binary_remote_addr) Module ngx_http_limit_req_module into a variable should work as I use that method for conditional requests via nginx maps. An empty limit_req_zone's key will disable the directive
     
  6. rdan

    rdan Well-Known Member

    5,446
    1,408
    113
    May 25, 2014
    Ratings:
    +2,201
    Local Time:
    5:36 AM
    Mainline
    10.2
    Trying it now :)
    Thanks Eva.
     
  7. rdan

    rdan Well-Known Member

    5,446
    1,408
    113
    May 25, 2014
    Ratings:
    +2,201
    Local Time:
    5:36 AM
    Mainline
    10.2
    I have this now created, not yet tested:
    Code:
    map $request_method $limitreqmethod {
    default $binary_remote_addr;
    GET     "";
    }
    
    limit_req_zone $limitreqmethod zone=reqperipmethod:128m rate=5r/s;
    
    limit_req zone=reqperipmethod burst=5 nodelay;
    
     
  8. rdan

    rdan Well-Known Member

    5,446
    1,408
    113
    May 25, 2014
    Ratings:
    +2,201
    Local Time:
    5:36 AM
    Mainline
    10.2
    Works great now :)
    Thanks again Eva.
     
  9. eva2000

    eva2000 Administrator Staff Member

    54,647
    12,230
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,799
    Local Time:
    7:36 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Great to hear :)