Forum Discussion

Thiyagu's avatar
Thiyagu
Icon for Cirrus rankCirrus
Jan 24, 2023

Need help with an iRule to match multiple URI and redirect the http request to a pool

Hello Friends,

Hope you all are doing good. I'm working on an iRule to match the multiple URI and route the request to a specific pool.

Please find below the iRule. Could you please review and let me know is there anything needs to be added or removed or is there any other best possible ways to achive this?

when HTTP_REQUEST {
switch -glob [string tolower [HTTP::uri]] {
"/next/x1/*" -
"/next/x2/*" -
"/next/x3/*" -
pool POOL_NEXT_X
}
{
"/next/y1/*" -
"/next/y2/*"
pool POOL_NEXT_Y
}
{
"/next/a1/*"-
pool POOL_NEXT_A
}
{
"/next/b1/*"
pool POOL_NEXT_B
}
{
"/next/c1/*"
pool POOL_NEXT_C
}

}

 

  • Thiyagu I found a few curled brace errors and some - in the incorrect location but those have been corrected. I added in a variable to utilize instead of reading directly from the HTTP header when matching on the switch statement. The last bit is I used this style guide that another user created if you would like to give it a once over when you have a moment.

    https://community.f5.com/t5/technical-articles/irules-style-guide/ta-p/305921

    Here is the iRule that should function the way you described.

    when CLIENT_ACCEPTED priority 500 {
    
        set DEFAULT_POOL [LB::server pool]
    
    }
    
    when HTTP_REQUEST priority 500 {
    
        set URI [string tolower [HTTP::uri]]
    
        switch --glob ${URI} {
            "/next/x1/*" -
            "/next/x2/*" -
            "/next/x3/*" {
                pool POOL_NEXT_X
            }
            "/next/y1/*" -
            "/next/y2/*" {
                pool POOL_NEXT_Y
            }
            "/next/a1/*" {
                pool POOL_NEXT_A
            }
            "/next/b1/*" {
                pool POOL_NEXT_B
            }
            "/next/c1/*" {
                pool POOL_NEXT_C
            }
            default {
                pool ${DEFAULT_POOL}
            }
        }
    
    }