Azure APIM Rate limiting for IP address

Amoghavarsh Patil 40 Reputation points
2024-04-30T09:45:37.92+00:00

Hi All,

I am trying to set rate limit based on IPAddress i found the below one but this is not working in my case since i have App Gateway and APIM reciving App Gateway backend Ip as IPAddress is their anyway i can get IPAddress from App Gateway and set rate limiting?

I checked about X-Forwarded-For even after trying that also IpAddress remain same in APIM.

And also i tried to restrict in App Gayeway rate limit but it shows 403 forribeden instead of 429 rate limit exceed.

 

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,791 questions
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 21,966 Reputation points
    2024-04-30T12:48:13.55+00:00

    @Amoghavarsh Patil Thanks for reaching out. Please verify the X-forwarded header for example, the "X-Forwarded-For" header might look like this: X-Forwarded-For: 203.0.113.1, 198.51.100.2, 192.0.2.3

    If you see the above pattern you can try adding the below policy 

        <inbound>
            <base />
            <!-- Save the X-Forwarded-For value -->
            <set-variable name="originalXForwardedForValue" value="@{
                string ipAddress = context.Request.Headers.GetValueOrDefault("x-forwarded-for","");
                    if (!string.IsNullOrEmpty(ipAddress))
                    {
                        string[] tokens = ipAddress.Split(',');
                        ipAddress = tokens[0];
                    }
                    return ipAddress;
                    }" />
            <rate-limit-by-key calls="5" renewal-period="60"
            counter-key="@((string)context.Variables["originalXForwardedForValue"])"
            increment-condition="@(context.Response.StatusCode == 200)"
            remaining-calls-variable-name="remainingCallsPerIP" />
        </inbound>
    
    

    this policy is to apply rate limiting based on the IP address of the client that initiated the request in this case the ipaddress would be 203.0.113.1

    please try and let me know if works.

    Please accept as Yes if the answer provided is useful, so that you can help others in the community looking for remediation for similar issues.

    0 comments No comments

0 additional answers

Sort by: Most helpful