Forum Discussion

JWhitesPro_1928's avatar
JWhitesPro_1928
Icon for Cirrostratus rankCirrostratus
Oct 03, 2016

Best way to rewrite and choose different pool at same time

What would be the best method to achieve the following:

 

If incoming url starts with "/test" replace /test with just "/" and choose pool "test"

 

I've done some of my other pool selection with LTM policies but I'm concerned that I can't make the policy replace just the "/test" part...I don't want it to do something like replace "/test/api/call1" with "/" and then the app running on node test breaks because it's expecting the request to be "/api/call1"

 

11 Replies

  • Assign one iRule to do the pool selection, then apply ProxyPass to handle the rewrite.

     

  • when HTTP_REQUEST {
        if { [HTTP::path] equals "/test" } {
            HTTP::path "/"
            pool test
        }
    }
    

    This will match exactly (and only) /test and nothing else.

    • JWhitesPro_1928's avatar
      JWhitesPro_1928
      Icon for Cirrostratus rankCirrostratus

      I will test this but will it effectively just remove the /test so that if a user goes to:

       

      myserver.com/test/documents/clinton.pdf that it would rewrite it to just /documents/clinton.pdf because that is what the backend pool server would be expecting (it wouldn't have a path for /test)

       

    • Josh_Jacobson_4's avatar
      Josh_Jacobson_4
      Icon for Altostratus rankAltostratus

      You can use [HTTP::uri] to preserve any GET vars (if you don't care about GET vars [HTTP::path] is fine too of course). Here's one way to strip the first segment of the path and send the rest on. I broke it out into two statements in case that's easier to read, and there's a one-liner version of it at the end too.

      when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/test/" } {
           This will find the position of the second slash (starts searching at position 1; first slash is position 0)
          set slashpos [string first / [HTTP::uri] 1]
           Substring from the position of the second slash through the end of the URI
          set newuri [substr [HTTP::uri] $slashpos]
           Here's the one-liner to accomplish the same thing (it was just easier to comment as two statements):
          set newuri [substr [HTTP::uri] [string first / [HTTP::uri] 1]]
          log local0. "New URI: $newuri"
          HTTP::uri $newuri
          pool test
      }
      }
      
    • Josh_Jacobson_4's avatar
      Josh_Jacobson_4
      Icon for Altostratus rankAltostratus

      oops, looking at Vernon's answer below I should have put in a test for no second slash - sorry if my answer does more harm than good!

       

      -Josh

       

  • Vernon_97235's avatar
    Vernon_97235
    Historic F5 Account
    when HTTP_REQUEST {
        if { [HTTP::path] equals "/test" } {
            HTTP::path "/"
            pool test
        }
    }
    

    This will match exactly (and only) /test and nothing else.

    • JWhitesPro_1928's avatar
      JWhitesPro_1928
      Icon for Cirrostratus rankCirrostratus

      I will test this but will it effectively just remove the /test so that if a user goes to:

       

      myserver.com/test/documents/clinton.pdf that it would rewrite it to just /documents/clinton.pdf because that is what the backend pool server would be expecting (it wouldn't have a path for /test)

       

    • Josh_Jacobson_4's avatar
      Josh_Jacobson_4
      Icon for Altostratus rankAltostratus

      You can use [HTTP::uri] to preserve any GET vars (if you don't care about GET vars [HTTP::path] is fine too of course). Here's one way to strip the first segment of the path and send the rest on. I broke it out into two statements in case that's easier to read, and there's a one-liner version of it at the end too.

      when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/test/" } {
           This will find the position of the second slash (starts searching at position 1; first slash is position 0)
          set slashpos [string first / [HTTP::uri] 1]
           Substring from the position of the second slash through the end of the URI
          set newuri [substr [HTTP::uri] $slashpos]
           Here's the one-liner to accomplish the same thing (it was just easier to comment as two statements):
          set newuri [substr [HTTP::uri] [string first / [HTTP::uri] 1]]
          log local0. "New URI: $newuri"
          HTTP::uri $newuri
          pool test
      }
      }
      
    • Josh_Jacobson_4's avatar
      Josh_Jacobson_4
      Icon for Altostratus rankAltostratus

      oops, looking at Vernon's answer below I should have put in a test for no second slash - sorry if my answer does more harm than good!

       

      -Josh

       

  • Ah, I see. You want to remove /test for an Request Path that is /test or begins with /test/, correct? If so:

    when HTTP_REQUEST {
        switch -glob [HTTP::path] {
            "/test" {
                HTTP::path "/"
                pool test
            }
            
            "/test/*" {
                HTTP::path "[substr [HTTP::path] 5]"
                pool test
            }
        }
    }
    
  • HTTP::path
    does preserve any Request Target parameters. That is, in order to retain the query parameters, the use of a reconstructed
    HTTP::uri
    is not needed.
    HTTP::path
    affects only the path portion of the Request Target. As such, if I submit this request:

    GET /test/abc/def.html?this=that&here=there HTTP/1.1
    ...
    

    my rule above would transform the Request Target to:

    GET /abc/def.html?this=that&here=there HTTP/1.1
    ...