Learn about Centmin Mod LEMP Stack today
Register Now

Nginx Rate limiting a specific path using Nginx

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by pamamolf, Mar 5, 2020.

  1. pamamolf

    pamamolf Premium Member Premium Member

    3,737
    362
    83
    May 31, 2014
    Ratings:
    +696
    Local Time:
    12:06 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    Hello

    I think there is a topic but i can't find it about rate limiting a specific path using Nginx.

    I enabled rate limiting in general for Nginx main config:
    Code:
    limit_req_zone  $binary_remote_addr zone=abclogin:16m rate=50r/s;
    limit_conn_zone $binary_remote_addr zone=abcconlimit:16m;
    and on the domain:
    Code:
    limit_conn abcconlimit 20;
    limit_req  zone=abclogin burst=80 nodelay;
    Now i would like to enable it at:
    Code:
    https://www.mydomain.com/folderx/
    and
    Code:
    https://www.mydomain.com/folderx
    Can anyone please help?

    Thank you
     
  2. pamamolf

    pamamolf Premium Member Premium Member

    3,737
    362
    83
    May 31, 2014
    Ratings:
    +696
    Local Time:
    12:06 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    It seems that i found two solutions:

    Solution 1:
    Code:
           location /folderx/ {
               limit_req zone=abclogin;
               limit_conn_zone=abcconlimit;
           }
    and
    Code:
           location /folderx {
               limit_req zone=abclogin;
               limit_conn_zone=abcconlimit;
           }
    Solution 2:

    If i want to set a custom limit for that zone something like:

    Code:
           location /folderx/ {
               limit_req zone=abclogin burst=300 nodelay;
               limit_conn_zone=abcconlimit burst=40 nodelay;
           }
    and
    Code:
           location /folderx {
               limit_req zone=abclogin burst=300 nodelay;
               limit_conn_zone=abcconlimit burst=40 nodelay;
           }
    Which one is the correct method or both can work and i should select what i want?

    I would like your recommendations about my values also please.

    Thank you
     
  3. eva2000

    eva2000 Administrator Staff Member

    43,579
    9,884
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +15,264
    Local Time:
    7:06 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    look at location match's optional operators Understanding Nginx Server and Location Block Selection Algorithms | DigitalOcean

     
  4. pamamolf

    pamamolf Premium Member Premium Member

    3,737
    362
    83
    May 31, 2014
    Ratings:
    +696
    Local Time:
    12:06 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    Thanks for your reply !

    It seems that this should be ok for my case:
    Code:
          location /folderx {
              limit_req zone=abclogin;
              limit_conn_zone=abcconlimit;
          }
    as it will cover all paths starting from:
    Code:
    https://www.mydomain.com/folderx
    Are these lines correct/accepted there?
    Code:
            
    limit_req zone=abclogin;
    limit_conn_zone=abcconlimit;
     
  5. eva2000

    eva2000 Administrator Staff Member

    43,579
    9,884
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +15,264
    Local Time:
    7:06 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    you can use Centmin Mod installed siege to load test your site and see