Rewrite host header to specific pool member

I’ve done basic host header rewrites rules in the past but I’ve just received an odd request that’s beyond me. I’m scrapping together a solution from multiple posts but haven’t got anything to pass yet.

Our developers want the F5 to receive the URL “ims.services.company.com” and rewrite the host header to reflect the specific pool member, after the node is selected by the LB algorithm. To the point, they want the client to send the GET with “ims.services.company.com” in the host header and the F5, serverside, to send a GET for “ims.services..company.com” in the header.

I’ve tried this with combinations of HTTP_REQUEST, LB_SELECTED, and HTTP_REQUEST_SEND but nothings working.

I’m assuming this can be done?

Thanks.

Hi,

So what do you want to rewrite the Host header to? Is it always the same value? Or do you want to rewrite it to something specific to the selected pool member?

For a static Host header rewrite you can use a rule like this:

when HTTP_REQUEST {

    Set the Host header to www.example.com
   HTTP::header replace Host "www.example.com"
}

To rewrite the Host header to something based on the selected pool member, you can use the HTTP_REQUEST_SEND event. You’ll need to force the evaluation of the code into the clientside context as this event is in the serverside context.

From: http://devcentral.f5.com/wiki/default.aspx/iRules/HTTP_REQUEST_SEND
when HTTP_REQUEST_SEND {

    Need to force the host header replacement and HTTP:: commands into the clientside context
    as the HTTP_REQUEST_SEND event is in the serverside context
   clientside {

       Replace the HTTP host header with the selected server IP and port
      HTTP::header replace Host "[LB::server addr]:[LB::server port]"
   }
}

If you want to map the IP:port to a name, you could use a datagroup and the class command (10.x) or the findclass command (9.x) to look up the corresponding value. If you need to do this and want help, please provide details on the logic you want to implement.

Aaron

Thanks. That’s a really good start. Wireshark (on the serverside) shows the IP address:port in the header “Host: 10.200.32.80\r\n”

I created a data group, “tempneteng01”, including only that address and am attempting to replace the host header by referencing the datagroup. I can’t seem to get the syntax down correctly though.

when HTTP_REQUEST {

if { [HTTP::header host] eq “netengtest.ecollege.com”) and ( [IP::addr “[IP::node_addr]” equals $::tempneteng01] } {

HTTP::header replace Host “netengtest.tempneteng01.ecollege.com”

}

}

The LTM throws me the following:

01070151:3: Rule [test-serverspecific-rewrite] error:

line 2: [parse error: PARSE syntax 78 {syntax error in expression " [HTTP::header host] eq

I think I’m on the right track. Though, using this logic, this iRule could grow extremely large considering we have over 70 possible nodes in some of our pools.

not sure if i understand correctly. pls feel free to revise.

v10.1.0
virtual bar {
   snat automap
   pool foo
   destination 172.28.17.55:http
   ip protocol tcp
   rules myrewrite
   profiles {
      http {}
      tcp {}
   }
}
pool foo {
   members {
      10.10.70.110:http {}
      10.10.70.120:http {}
      10.10.70.130:http {}
   }
}
class myhost {
   {
      "10.10.70.110" { "node1" }
      "10.10.70.120" { "node2" }
      "10.10.70.130" { "node3" }
   }
}
rule myrewrite {
   when HTTP_REQUEST_SEND {
        clientside {
                if {[HTTP::host] equals "172.28.17.55"} {
                        HTTP::header replace Host "[class match -value [LB::server addr] equals myhost]"
                }
        }
}
}
client side:
GET / HTTP/1.1
Host: 172.28.17.55
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

server side:
GET / HTTP/1.1
Host: node3
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

I think I’m really close now. With the following, I get the following error: Line 4 [undefined procedure: class]

when HTTP_REQUEST_SEND {

clientside {

if {[HTTP::host] equals “netengtest.company.com”} {

HTTP::header replace Host “[class match -value [LB::server addr] equals netenghosts]”

}

}

}

“Undefined procedure” seems to be trying to say that the data group doesn’t exist? I did create the data group from the GUI. In bigip.conf, it looks like this:

class netenghosts {

“\“10.200.32.200\” { \“TEMPNETENG01\”}”

}

BTW - I’m running 9.4.7. Initially, I did try editing the bigip.conf file directly using what you suggested (replacing the relevant pieces of course) but that tossed errors as well.

i understand in v9 u’ve to use findclass. the class command is available in v10.

Here’s a 9.x rule for this:

http://devcentral.f5.com/wiki/default.aspx/iRules/rewrite\_host\_header\_to\_server\_name.html

Aaron

Cool.

I tried the findclass command and the F5 returned “bad argument”.

I gave the sample iRule Aaron sent a look over and that looks perfect. I applied it and it’s matching, but I must have my data group misconfigured, because I’m just getting the IP address in the host header serverside. Nitass, the example you sent the other day looks like it was taken out of bigip.conf. I’ve tried to edit my own file using that exact same sytax but it doesn’t take. The way it appears in my bigip.conf file is:

class netenghosts {

“10.200.32.200” “TEMPNETENG01”

}

From the GUI, how would I go about creating a data group using the logic specified in step 1: from the example that Aaron sent. For example, it’s of the type “string”, but what do I put into the box for the string to create the logic 10.10.10.10 equals host1.company.com?

You can add the string datagroup element exactly like this in the GUI:

10.200.32.200 TEMPNETENG01

From the command line you they should show as this:

class netenghosts {

“10.200.32.200 TEMPNETENG01”

}

Aaron

Voila. According to wireshark:

Host: TEMPNETENG01

This is great. Thanks a ton. Though I think it’s worth my time to look into the proxy-pass feature from F5. The production data group I’m going to apply this to will quickly grow out of hand.

Thanks again to the both of you.

Andy

Glad that’s working for you. If doing just this Host header rewrite works, I wouldn’t suggest using the ProxyPass iRule. That rule provides a lot more functionality--and overhead--that you probably don’t need.

Aaron

I am picking up this project from Andy, and have had to reverse-engineer this irule back into our QA environment (runs the same version of code), and of course, it doesnt work there. I created the data class with the same name and syntax, just with the new IP addresses and string to lookup. the debug logs showthis:

Looked up 10.52.4.67, found: .

I will repost what what i have in the QA environment:

when HTTP_REQUEST_SEND {

Log debug messages to /var/log/ltm? 1=yes, 0=no.

set host_debug 1

Need to force the host header replacement and HTTP:: commands into the clientside context

as the HTTP_REQUEST_SEND event is in the serverside context

clientside {

if {$host_debug}{log local0. “[IP::client_addr]:[TCP::client_port]: New [HTTP::method] request to [HTTP::host][HTTP::uri]”}

Look up the selected server IP in the datagroup to get the host header value

set host_header_value [findclass [LB::server addr] netenghosts " "]

if {$host_debug}{log local0. “[IP::client_addr]:[TCP::client_port]: Looked up [LB::server addr], found: $host_header_value.”}

Check if the lookup returned a value

if {$host_header_value ne “”}{

Replace the host header value

HTTP::header replace Host $host_header_value

if {$host_debug}{log local0. “[IP::client_addr]:[TCP::client_port]: Replaced Host header with $host_header_value.”}

}

}

}

class netenghosts {

“10.52.4.67 ims.next004pqa.services.qaprod.company.com”

}

I essentially copied and pasted from the production devices, so it doesnt appear that the irule or data class syntax is bad, yet persists in not finding the lookup value.

TIA

-clay

Hi Clay,

That looks valid. Can you confirm it’s just a space between the IP address and host name in the datagroup?

Aaron

just to be sure, i went back and deleted/retyped those characters, and I get the same message in the ltm log:

Looked up 10.52.4.67, found: .

-clay

Not sure what the problem was, but here is how i fixed it:

1. created a new data group string with a different name

2. pasted in the same parameters.

3. updated the iRule to reference the new data group.

4. watched it work.

PFM

Thanks for keeping an eye on this and being willing to help.

-clay

That’s quite odd. Glad you got it working regardless.

Aaron

Here’s how I pulled it off:

when LB_SELECTED {

    set POOL [LB::server addr]

    log local0.debug "Pool Member is $POOL"

    switch $POOL {

        10.65.16.135 { HTTP::header replace Host "strv-int2.corp.lan" }

        10.65.17.62  { HTTP::header replace Host "strv-int4.corp.lan" }

        default { log local0.debug "Default hit on STRV-rewrite." }

    }

}