Forum Discussion

big_nerd_9769's avatar
big_nerd_9769
Icon for Nimbostratus rankNimbostratus
Nov 27, 2012

iRule to force usage of Content Distribution [Modify HTML Out]

Hello,

 

 

I am still very very new at iRules, so thank you in advance for any help you can give me.

 

I am attemping to put an iRule in place to force the usage of our content distribution network. We have an iRule that was developed for us that searches for a specific url (www.site.com) and as long as the http path ends with jpg, jpeg, png, gif, (used in a datagroup) etc it will redirect to cdn.site.com.

 

I can see in the logs that the rule seems to be firing however I do not know enough about the browsers available on the market to know if all browsers will follow a redirect from an image URL.

 

Is it possible to modify the html output? or would that be too exaustive on the device itself?

 

I am not sure I can post the iRule code as it was developed during the implementation of the F5 itself, so I am not sure about licensing, etc.

 

 

Thanks again in advance for any help...

 

8 Replies

  • OK, just to clarify a few things before I respond in full;

     

     

    1) Presently, you are redirecting based on request URI yes?

     

    2) You'd rather rewrite responses to avoid the redirect yes?

     

    3) Is there a direct correlation between the request URI and the CDN URI? So would www.site.com/images/test.jpg be located at cdn.site.com/images/test.jpg? If not, this is going to be a very long iRule =]
  • Hello! Thank you for the swift response...

     

     

    1) Presently, you are redirecting based on request URI yes?

     

     

    Yes - if the request URI ends with an image extension, it will redirect

     

     

    2) You'd rather rewrite responses to avoid the redirect yes?

     

     

    Yes - unless I can find some confirmation all browsers (including mobile browsers) will follow redirect requests for images.. I have not been able to confirm nor deny this

     

     

    3) Is there a direct correlation between the request URI and the CDN URI? So would www.site.com/images/test.jpg be located at cdn.site.com/images/test.jpg? If not, this is going to be a very long iRule =]

     

     

    Yes, it is literally www.site.com/blah.jpg == cdn.site.com/blah.jpg, so on and so forth.

     

     

    The CDN provider uses a cname (cdn.site.com) and just correlates that to an origin source which has images in the exact same path.
  • Is it possible to modify the html output? or would that be too exaustive on the device itself?i think it is possible. i do not know whether it is too exhaustive on the device or not. anyway, the following code is just a simple example.

     

     

    e.g.

     

     

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          myhttp {}
          stream {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b pool foo list
    pool foo {
       members 200.200.200.101:80 {}
    }
    [root@ve10:Active] config  b profile myhttp list
    profile http myhttp {
       defaults from http
       header erase "Accept-Encoding"
    }
    
     original content
    
    [root@ve10:Active] config  curl -i http://200.200.200.101
    HTTP/1.1 200 OK
    Date: Tue, 27 Nov 2012 15:47:46 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Sat, 27 Oct 2012 03:22:35 GMT
    ETag: "4183f3-59-f28f94c0"
    Accept-Ranges: bytes
    Content-Length: 89
    Content-Type: text/html; charset=UTF-8
    
    ...snipped...
    This is 101 host.
    
    ...snipped...
    
     modified content
    
    [root@ve10:Active] config  curl -i http://172.28.19.79
    HTTP/1.1 200 OK
    Date: Tue, 27 Nov 2012 15:47:54 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Sat, 27 Oct 2012 03:22:35 GMT
    ETag: "4183f3-59-f28f94c0"
    Accept-Ranges: bytes
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    
    ...snipped...
    This is 101 host.
    
    ...snipped...
    
  • thanks Steve. 🙂

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      STREAM::disable
    }
    when HTTP_RESPONSE {
      if { [HTTP::header value Content-Type] contains "text" }{
        STREAM::expression {&".*?\.gif&&}
        STREAM::enable
      }
    }
    when STREAM_MATCHED {
      STREAM::replace [string map {\" \"http://cdn.site.com/} [STREAM::match]]
    }
    }
    
  • Just out of interest Nitass why do you delete the HTTP Accept-Encoding header? Doesn't the iRule event execute before compression takes place?
  • The forum code munged the stream expression. It should be:

     

     

    STREAM::expression {@".*?\.gif@@}

     

     

    Also make sure that the config loads after updating the iRule as the unbalanced double quote might throw off the parser.

     

     

    Steve, you remove the Accept-Encoding header on requests to the pool to prevent the server from sending compressed response content. Compressed content isn't decompressed by TMM before applying the stream filter.

     

     

    Aaron