Forum Discussion

mb_shankr_22532's avatar
mb_shankr_22532
Icon for Nimbostratus rankNimbostratus
Oct 04, 2015

need help to create an irule to append an uri and redirect with conditions...

Hi all, need ur help pls. I have an URL: abc.test.com, pointing to http vip with mapped pool-1111. I also have another pool-2222 created with same members but different ports. pool-2222 is not mapped to any VIP. Am trying to create an irule so that when users try abc.test.com, it shud be rewritten as abc.test.com/test1 and forwarded to mapped pool-1111. Also additionally, only when users try abc.test.com/test2, it shud send to pool-2222. All connections are http only. Could someone pls guide me here... any help is greatly appreciated.. thanks..

 

10 Replies

  • Hello,

     

    If you do not have other HTTP hosts besides 'abc.test.com' serviced via the same Virtual Server, we can make it a bit slimmer. If you have any questions and are uncertain why the code is exactly as it is, let me know and I'll explain

     

    when HTTP_REQUEST {
    
      switch [HTTP::host][string tolower [HTTP::path]] {
        "abc.test.com/" {
          HTTP::path "/test1"
          pool pool-1111
        }
        "abc.test.com/test1" -
        "abc.test.com/test1/" {
          pool pool-1111
        }
        "abc.test.com/test2" -
        "abc.test.com/test2/" {
          pool pool-2222
        } default {
          pool MyDefaultPool
        }
      }
    
    }
    • mb_shankr_22532's avatar
      mb_shankr_22532
      Icon for Nimbostratus rankNimbostratus
      I do not have other HTTP hosts besides 'abc.test.com' serviced via the same Virtual Server
  • Hello,

     

    If you do not have other HTTP hosts besides 'abc.test.com' serviced via the same Virtual Server, we can make it a bit slimmer. If you have any questions and are uncertain why the code is exactly as it is, let me know and I'll explain

     

    when HTTP_REQUEST {
    
      switch [HTTP::host][string tolower [HTTP::path]] {
        "abc.test.com/" {
          HTTP::path "/test1"
          pool pool-1111
        }
        "abc.test.com/test1" -
        "abc.test.com/test1/" {
          pool pool-1111
        }
        "abc.test.com/test2" -
        "abc.test.com/test2/" {
          pool pool-2222
        } default {
          pool MyDefaultPool
        }
      }
    
    }
    • mb_shankr_22532's avatar
      mb_shankr_22532
      Icon for Nimbostratus rankNimbostratus
      I do not have other HTTP hosts besides 'abc.test.com' serviced via the same Virtual Server
  • Hi Rap, thank u soo much for your answer, but unfortunately it didnt work :( I get the same error tat I got before while trying to access abc.test.com/test2. the test1 uri works jus fine, but when I specifically try test2, it gives HTTP status 404 error.

     

    HTTP Status 404 - /test2

     

    type Status report

     

    message /test2

     

    description The requested resource is not available.

     

  • I tried ur suggestion upside down and now am getting error while trying to access both: abc.test.com and abc.test.com/test2. Below is what I tried:

     

    First I changed the default pool mapped to VS from pool-1111 to pool-2222. Then modified the irule as:

     

    when HTTP_REQUEST {

     

    switch [HTTP::host][string tolower [HTTP::path]] { "abc.test.com/test2" { pool pool-2222 } "abc.test.com/" - "abc.test.com/" { HTTP::path "/test1" pool pool-1111 } "abc.test.com/test1" - "abc.test.com/test1/" { pool pool-1111 } default { pool MyDefaultPool } }

     

    }

     

    • mb_shankr_22532's avatar
      mb_shankr_22532
      Icon for Nimbostratus rankNimbostratus
      Ignoring my above modified irule and considering only the irule suggested by you, I assume that basically I get the error when trying to redirect to a remote pool... am sure the solution is pretty much close but dunno what is it hiding behind!!.. someone, pls help me out here..
  • Hello,

    If you issue a command

    curl -I PoolMemberIP:PoolMemberPort/test2
    from F5, you will see that the 404 error comes from the Pool Member as the page /test2 was not found. Try the same with /test1 page, and you will see the difference. Therefore, the HTTP 404 error you're mentioning is not related to F5 or this iRule. Make sure the related pages exist in the backend servers (your pool members).

    Now in regards to making the iRule slimmer. We can use the user-entered HTTP path as our only conditional trigger since "abc.test.com" is your only host serviced from this particular Virtual Server.

    when HTTP_REQUEST {
    
      switch [string tolower [HTTP::path]] {
        "/" {
          HTTP::path "/test1"
          pool pool-1111
        }
        "/test1" -
        "/test1/" {
          pool pool-1111
        }
        "/test2" -
        "/test2/" {
          pool pool-2222
        } default {
          pool MyDefaultPool
        }
      }
    
    }
    
  • Hi,

    If you want to allow /test1, you can redirect / to /test1/ instead of rewriting path.

    • if you rewrite path and the content of the response contains links like img/logo.php, the browser will request /img/logo.php.
    • if you redirect to /test1/ and the content of the response contains links like img/logo.php, the browser will request /test1/img/logo.php

    You can also store the virtual server default pool in a variable to use it in the "default" statement.

    when CLIENT_ACCEPTED { 
        set default_pool [LB::server pool]
    }
    
    when HTTP_REQUEST {
    
      switch [string tolower [HTTP::path]] {
        "/" {
          HTTP::redirect /
        }
        "/test1" -
        "/test1/" {
          pool pool-1111
        }
        "/test2" -
        "/test2/" {
          pool pool-2222
        } default {
          pool $default_pool
        }
      }
    }
    
  • Hey guys, thank u soo much for your efforts. really appreciate it. U guys do a great job. I was finally able to fix it, but using HTTP class profiles (even though I felt I cud hav done it with iRules). I'll explain how. Rap was correct, the first iRule was perfect, except the fact that /test2 was not available on the server. so basically, the issue was solved right then, but not yet, there was a twist. the requirement was that when users try abc.test.com/test2, it shud go to pool-2222 re-written as abc.test.com, and nothing else. So I tried modifying the iRule as below, but it didn work.

    when HTTP_REQUEST {
      switch [string tolower [HTTP::path]] {
        "/" {
          HTTP::path "/test1"
          pool pool-1111
        }
        "/test1" -
        "/test1/" {
          pool pool-1111
        }
        "/test2" -
        "/test2/" {
          [string map {"/test2" ""} [HTTP::path]]
          pool pool-2222
        } default {
          pool pool-1111
        }
      }
    }
    

    Am sure there's a loop hole in the above rule but dunno what is that. And again it didn work for /test2 but this time it was returning webpage not available. pls suggest and lemme know what was wrong in the above rule.

    Eventually I applied the concept to HTTP Class Profiles and created two profiles: 1 which matched only URI path "/" and redirected to http://[HTTP::host]/test1 2 which matched only URI path "/test2" and sent to pool-2222, re-writing the URL as http://[HTTP::host] I mapped the above 2 class profiles to Virtual server and it works. But thr's still an issue. it works only sporadically. 😞