Forum Discussion
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.
16 Replies
- hoolio
Cirrostratus
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 - astokes_6920
Nimbostratus
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. - nitass
Employee
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 - astokes_6920
Nimbostratus
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. - nitass
Employee
i understand in v9 u've to use findclass. the class command is available in v10. - hoolio
Cirrostratus
Here's a 9.x rule for this:
http://devcentral.f5.com/wiki/default.aspx/iRules/rewrite_host_header_to_server_name.html
Aaron - astokes_6920
Nimbostratus
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? - hoolio
Cirrostratus
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 - astokes_6920
Nimbostratus
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 - hoolio
Cirrostratus
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
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com