Forum Discussion

kashanihra's avatar
kashanihra
Icon for Nimbostratus rankNimbostratus
Jun 17, 2024

how set connection rate limit on specific uri with iRule ?

hello 

I use this iRule 

when RULE_INIT {
#Allow 3 Requests every 1 Second
set static::maxRate 5
set static::windowSecs 1
}
when HTTP_REQUEST {
   if {[HTTP::host] equals "host.com" and [string tolower [HTTP::uri]] contains "/favicon.ico"}
   {
       reject
   }
    elseif {[HTTP::host] equals "host.com" and [string tolower [HTTP::uri]] contains "/openapi/v0/identity/register" } {
    set uri_limitervar [HTTP::uri]
    set get_count [table key -count -subtable $uri_limitervar]
    if { $get_count < $static::maxRate } {
        incr get_count 1
         table set -subtable $uri_limitervar $get_count $uri_limitervar indefinite $static::windowSecs
         } else {

                HTTP::respond 503 content "<html><h2>You have exceeded the maximum number of requests per minute allowed... Try again later.</h2></html>"

                #Drop the Connection afterwards
                drop
        }
       }
    elseif {[HTTP::host] equals "host.com" and [string tolower [HTTP::uri]] contains "/openapi" } {
    HTTP::header replace "Host" "apim3.sdb247.com"
    HTTP::header insert X-Forwarded-For [IP::remote_addr]
    HTTP::header insert X-Geo-Country-Code [whereis [IP::client_addr] country ]
    HTTP::header insert X-Geo-Country-Area [whereis [IP::client_addr]  state  ]
#   SSL::disable serverside
    pool Prd-NGNX_443
   }
}

but after 10 or 5 minute all connection for this uri will drop.
and now what is the problem?

No RepliesBe the first to reply