Forum Discussion

Mel_153429's avatar
Mel_153429
Icon for Nimbostratus rankNimbostratus
Dec 17, 2015

Need help creating a irule to reverse proxy .

I need some help creating an irule to reverse proxy to an external website. I would like to keep the original uri when it proxy to the external site. I have workup the start of a simple irule not sure if I am on the right track or not. I am new to irule and any help would be greatly appreciated.

 

when HTTP_REQUEST { HTTP::uri [string tolower [HTTP::uri]] if { [HTTP::uri] starts_with "/blog/" } { pool test-reverse-proxy } }

 

8 Replies

  • Hi Mel,

    an iRule to content-switch just a certain sub directory would look like this...

    when HTTP_REQUEST { 
        if { [string tolower [HTTP::uri]] starts_with "/blog/" } then { 
            pool test-reverse-proxy 
        }
    }
    

    Does the Blog site has any special requirements, that you need to cover with iRules? (e.g. HOSTNAME rewrite, etc.)

    BTW: Keep in mind that this rule would forward the URI "/blog/" but not "/blog".

    Cheers, Kai

  • If I read this correctly, when the customer sends http://www.lightsource.com/blog, you want the backend server to see what is essentially http://lightsource.wpengine.com. The be pedantic, you want the Host: header to change from www.lightsource.com to lightsource.wpengine.com and the request-uri to change from /blog to /. You do not want to perform an HTTP redirect because you want this change to be transparent to the user-agent.

    You can solve this on 11.4 or higher using a Local Traffic Policy:

    Here is an example:

    ltm policy lightsource-rewrite {
        controls { forwarding }
        requires { http }
        rules {
            rule01 {
                actions {
                    0 {
                        http-host
                        replace
                        value lightsource.wpengine.com
                    }
                    1 {
                        http-uri
                        replace
                        path /
                    }
                }
                conditions {
                    0 {
                        http-host
                        host
                        values { www.lightsource.com }
                    }
                    1 {
                        http-uri
                        path
                        values { /blog }
                    }
                }
                ordinal 1
            }
        }
        strategy first-match
    }
    

    Alternatively, you can do the same in an iRule:

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] eq "www.lightsource.com" and [HTTP::path] eq "/blog" } {
            HTTP::replace Host lightsource.wpengine.com
            HTTP::path "/"
        }
    }
    
    • Mel_153429's avatar
      Mel_153429
      Icon for Nimbostratus rankNimbostratus
      Vernon, Thanks for the responses! I just to make sure we are on the same page. When the end user go to www.lightsource.com/blog they are proxy over to lightsource.wpengine.com but the web address in their browser remain www.lightsource.com/blog and all action is transparent to the user. Is that what you saying as well? Thanks
  • Vernon_97235's avatar
    Vernon_97235
    Historic F5 Account

    If I read this correctly, when the customer sends http://www.lightsource.com/blog, you want the backend server to see what is essentially http://lightsource.wpengine.com. The be pedantic, you want the Host: header to change from www.lightsource.com to lightsource.wpengine.com and the request-uri to change from /blog to /. You do not want to perform an HTTP redirect because you want this change to be transparent to the user-agent.

    You can solve this on 11.4 or higher using a Local Traffic Policy:

    Here is an example:

    ltm policy lightsource-rewrite {
        controls { forwarding }
        requires { http }
        rules {
            rule01 {
                actions {
                    0 {
                        http-host
                        replace
                        value lightsource.wpengine.com
                    }
                    1 {
                        http-uri
                        replace
                        path /
                    }
                }
                conditions {
                    0 {
                        http-host
                        host
                        values { www.lightsource.com }
                    }
                    1 {
                        http-uri
                        path
                        values { /blog }
                    }
                }
                ordinal 1
            }
        }
        strategy first-match
    }
    

    Alternatively, you can do the same in an iRule:

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] eq "www.lightsource.com" and [HTTP::path] eq "/blog" } {
            HTTP::replace Host lightsource.wpengine.com
            HTTP::path "/"
        }
    }
    
    • Mel_153429's avatar
      Mel_153429
      Icon for Nimbostratus rankNimbostratus
      Vernon, Thanks for the responses! I just to make sure we are on the same page. When the end user go to www.lightsource.com/blog they are proxy over to lightsource.wpengine.com but the web address in their browser remain www.lightsource.com/blog and all action is transparent to the user. Is that what you saying as well? Thanks
  • Hi Vernon,

    I would add a few things to remind, when it comes to content switching scenarios, where the hostname and/or path needs to be changed.

    1.) /blog/ would most likely contain nested ressources and may also reference to other sub directories. So it will be most likely the case that a

    starts_with
    operator have to be used, to make the entire path accessible.

    2.) If the blog website is build on absolute pathes (e.g.

    Click me
    ), then the site would nevertheless flip after the initial page is loaded. To fix this behavior, you have to change the delivered content using
    [STREAM]
    to reflect the changed host-header-value.

    3.) If the blog website is build on root pathes that are different (e.g.

    ),
    then
    [STREAM]
    needs to replace those links too.

    4.) If the site is build on relative pathes (e.g.

    ), it will work most of the time, but only if you proxy to the path where the relativity starts.

    @Mel: The best thing you could do, is to ask your provider to accept your desired HOSTNAME and provide every ressources below the path of /blog. In this case you won't need to dig into the website content and correct certain things...

    Cheers, Kai

  • Hi Mel,

    in my opinion the iRule below would be the last "easy" implementation method.

    If this iRules is not working out, then you SHOULD ask your provider to accept the hostname "www.lightsource.com" for the blog website and ask them to store every related content underneath of the path "/blog" without any exeptions.

    If the provider can't do / is willing to do this, then you have to dig into the web site content and start to translate included URLs, HTTP-Redirects, etc. But this would then require some deeper HTTP/HTML knowledge and some time to figure out what needs to be changed...

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] eq "www.lightsource.com" and [HTTP::path] starts_with "/blog" } {
            HTTP::replace Host lightsource.wpengine.com
            pool test-reverse-proxy 
        }
    }
    

    Cheers, Kai