Forum Discussion
raytoles_75680
Nimbostratus
Dec 03, 2009Header Insert of "http" or "https"
We're going to implement a virtual server to handle HTTP and HTTPS requests. The developers need us to insert the protocol into the header. We're still new to irules and I constantly fear performance issues because of irules. So I'm submitting what I have for some input.
when HTTP_REQUEST {
if {[TCP::local_port] equals 80}{
HTTP::header insert X-Forwarded-Proto "http"
log local0. "HTTP request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]"
} elseif {[TCP::local_port] equals 443}{
HTTP::header insert X-Forwarded-Proto "https"
log local0. "HTTPS request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]"
}
}
- The_Bhattman
Nimbostratus
Looks good from what I can tell. The only thing I would suggest is to check for the existence of the header. This way you are not always inserting for returning connections that already have it.when HTTP_REQUEST { if {([TCP::local_port] ==80) and !( [HTTP::header "X-Forwarded-Proto"] eq "http") }{ HTTP::header insert X-Forwarded-Proto "http" log local0. "HTTP request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]" } elseif {([TCP::local_port] ==443) and !( [HTTP::header "X-Forwarded-Proto"] eq "https") } { HTTP::header insert X-Forwarded-Proto "https" log local0. "HTTPS request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]" } }
- hoolio
Cirrostratus
A client wouldn't include a response header in a subsequent request, so the only time that header would exist is if the client injected it. You might actually want to remove any prior instance to ensure a malicious client couldn't forge the header.when CLIENT_ACCEPTED { Check the requested port switch [TCP::local_port] { 80 { set proto http } 443 { set proto https } default { Drop the request drop } } } when HTTP_REQUEST { Replace the X-Forwarded-Proto header if it exists If it does not exist, a new instance will be inserted HTTP::header replace X-Forwarded-Proto $proto }
- raytoles_75680
Nimbostratus
Aaron, - raytoles_75680
Nimbostratus
Everything is good but Safari is doing something weird. It's appending the url with :443 when it's an http request, looks like this http://newsite.apa.org:443. Might be because the application is doing some switching between http and https. All the rest of the browsers are working w/o any problems. - hoolio
Cirrostratus
You could add the XForwardedProto logic to the single VIP iRule by setting the proto variable to https in this section: - raytoles_75680
Nimbostratus
Thank YOU kind sirs! I added the following to http_response and it resolved our Safari issue.when HTTP_RESPONSE { if { [HTTP::header Location] starts_with "http://newsite.apa.org:443"} { log local0. "Location [HTTP::header Location]" HTTP::header replace Location "http://newsite.apa.org" } }
- hoolio
Cirrostratus
It would be more efficient to only check for the Location header on redirects. Also, if you want to preserve the URI in the redirect, you should use string map:when HTTP_RESPONSE { if { [HTTP::is_redirect] and [HTTP::header Location] starts_with "http://newsite.apa.org:443"} { log local0. "Location [HTTP::header Location]" HTTP::header replace Location "[string map "http://newsite.apa.org:443 http://newsite.apa.org" [HTTP::header Location]]" } }
- raytoles_75680
Nimbostratus
This exactly what I did once the developers returned with an issue involving the need to preserve the URI. - Nick_T_68319
Nimbostratus
I did something similar, except with the HTTP profile... i have one profile for non-ssl VS and one with ssl VS. Then each profile does a header insert that is basically SSL true or false, and the application reads this header. - Puli
Nimbostratus
I tried the above code, but for some reason, the Location is outputed as empty in the log.
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects