Forum Discussion

frisco76_104868's avatar
frisco76_104868
Icon for Nimbostratus rankNimbostratus
Oct 29, 2009

hostname rewrite

Hi all,

I want to throw a message out on the forum..this might not require an iRule (if we can do it without an iRule, that would be preferred method).

I need to be able to rewrite the hostname of incoming and outgoing request for a single pool. (I have LTM 1600, v 9.4.5) Basically, it looks like this:

For a single pool, any incoming HTTP Request that have http://www.mycompany.com/* ===rewrite the hostname to ====> http://blogs.mycompany.com/* (the server must see blogs.mycompany.com and the client will continue to see www.mycompany.com -- I'm not doing a redirect)

The outgoing (the return) request (HTTP Response), need to rewrite the hostname back to the original name (http://blogs.mycompany.com/* ====rewrite the hostname to =====> http://www.mycompany.com/* (the LTM will intercept and rewrite the host header back to www for the client).

If I can this in a HTTP profile, that would be awesome. Would the iRule be as simple as this:

 
 HTTP_REQUEST{ 
  HTTP::host "blogs.mycompany.com" 
 } 
 HTTP_RESPONSE{ 
  HTTP::host "www.mycompany.com" 
 } 
 

I saw some Reverse Proxy examples but it looked like overkill for what I want to do. Will the iRule I show above work? (Or can I set it up in the F5 HTTP profile?)

Thanks in advance...

-frisco76: cool:

5 Replies

  • There isn't a host header in responses. The app would potentially include absolute references to the hostname in HTTP headers and the payload.

     

     

    If the ProxyPass iRule is overkill for your scenario, you might be able to take a stripped down version and adapt it for your requirements. This Codeshare example is for rewriting the URI, but you could adapt it for external/internal hostnames in your scenario.

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/Uri_Version_Mapping.html

     

     

    Here is a version that assumes the application does not include absolute references to the "internal" hostname in response content:

     

     

    iRule to change host headers

     

    http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&tpage=1&view=topic&postid=19917

     

     

    Aaron
  • HI hoolio, I wrote up the iRule I think that should work (I have not tested this yet...I'm hoping to test this early next week).

    In a nutshell, on HTTP_REQUEST..I'm going to rewrite the HTTP host header from aaa.company.com to bbb.company.com and on the HTTP_REQUEST, if HTTP header "Location" exists, I'm going to rewrite Location value from bbb.company.com to aaa.company.com (Is that a valid task to rewrite the Location? Do I need to rewrite the hostname anywhere else?). Again, I don't know if this is going to work or not...

     
      when RULE_INIT {        
     set ::external_hostname "aaa.company.com"         
     set ::internal_hostname "bbb.company.net"    
      Enable debug logging to /var/log/ltm.  1 = yes, 0 = no 
     set ::hostname_rewrite_debug 0 
     }    
      
      
     when HTTP_REQUEST {                
     if {$::hostname_rewrite_debug}{log local0. "Original URL: [HTTP::host][HTTP::uri]"} 
      
     HTTP::header replace "Host" $::internal_hostname        
      
     if {$::hostname_rewrite_debug}{log local0. "New URL: [HTTP::host][HTTP::uri]"} 
     }    
      
      
     when HTTP_RESPONSE {         
     if { [HTTP::header value "Location"] starts_with "$::internal_hostname" }{ 
      
     if {$::hostname_rewrite_debug}{log local0. "Inside of HTTP::header location, orig HTTP location equals: [HTTP::header location]"} 
      
     set ::location [HTTP::header location] 
      
     HTTP::header replace location $::external_hostname 
      
     if {$::hostname_rewrite_debug}{log local0. "Inside of HTTP::header location, new HTTP location equals: [HTTP::header location]"} 
     } 
     }   
     
  • Hi Frisco,

    A few notes:

    - This line won't log the updated host header value as the HTTP::host value is cached within the same event and event priority. If you want to log the Host header update, you can add a second HTTP_REQUEST event after the first:

     
     when HTTP_REQUEST priority 501 { 
        if {$::hostname_rewrite_debug}{log local0. "New URL: [HTTP::host][HTTP::uri]"}  
     } 
     

    - The Location header value is normally an absolute URL like http://www.example.com/path/to/file.ext. So you would want to check for a Location header value which starts with http://$::internal_hostname or https://$::internal_hostname. Also, when rewriting the Location header value, you would want to do an in place replacement of $::internal_hostname for $::external_hostname--not just replace the Location header value with $::external_hostname. You can do this using:

    HTTP::header replace Location [string map -nocase "$::internal_hostname $::external_hostname" [HTTP::header Location]]

    The internal hostname might be included in links or other references in the response content. If that's the case, you would want to use a stream profile and STREAM::expression based iRule to rewrite the response payload. The wiki page for STREAM::expression has some examples for this.

    Aaron
  • Interesting use case. Was there ever a solution that you don't mind sharing?

     

    Thanks.