Forum Discussion
irule to load custom page via ifile when HTTP::status !200
Good day, DevCentral is an incredible resource but I thought I throw out my specific scenario for the community's opinion.
We're looking to load a "friendly" page via ifile to send to the end-user when the response from a pool member is not 200. I'm not a coder at all but hopefully this is close enough to get my idea across:
when HTTP_RESPONSE {
if { [HTTP::status] != "200"} {
switch [HTTP::status] {
404 { [ifile get 404.html]
}
503 { [ifile get 503.html]
}
default { [ifile get Unexpected.html]
}
}
}
}
I'm looking for a "catch-all" at the bottom to trap any custom errors our web front-end might send.
I'll probably want to add other "good" codes in addition to 200 (like redirect) but this is sort of just proof-of-concept for now.
Thanks for your time!
5 Replies
- nitass
Employee
i understand you need HTTP::respond to send ifile content.
e.g.root@(ve11a)(cfg-sync Changes Pending)(Active)(/Common)(tmos) list ltm virtual bar ltm virtual bar { destination 172.28.20.111:80 ip-protocol tcp mask 255.255.255.255 pool foo profiles { http { } tcp { } } rules { myrule } source 0.0.0.0/0 source-address-translation { type automap } vlans-disabled } root@(ve11a)(cfg-sync Changes Pending)(Active)(/Common)(tmos) list ltm pool foo ltm pool foo { members { 200.200.200.101:80 { address 200.200.200.101 } } } root@(ve11a)(cfg-sync Changes Pending)(Active)(/Common)(tmos) list ltm rule myrule ltm rule myrule { when HTTP_RESPONSE { if { [HTTP::status] != 200 } { HTTP::respond 200 content [ifile get simpletext_ifile] noserver } } } [root@ve11a:Active:Changes Pending] config ssldump -Aed -nni 0.0 port 80 New TCP connection 1: 172.28.19.251(50701) <-> 172.28.20.111(80) 1374884126.3182 (0.0021) C>S --------------------------------------------------------------- GET /fake HTTP/1.1 User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5 Host: 172.28.20.111 Accept: */* --------------------------------------------------------------- New TCP connection 2: 200.200.200.14(50701) <-> 200.200.200.101(80) 1374884126.3201 (0.0018) C>S --------------------------------------------------------------- GET /fake HTTP/1.1 User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5 Host: 172.28.20.111 Accept: */* --------------------------------------------------------------- 1374884126.3231 (0.0030) S>C --------------------------------------------------------------- HTTP/1.1 404 Not Found Date: Sat, 27 Jul 2013 00:12:18 GMT Server: Apache/2.2.3 (CentOS) Content-Length: 280 Content-Type: text/html; charset=iso-8859-1 404 Not Found Not Found The requested URL /fake was not found on this server. Apache/2.2.3 (CentOS) Server at 172.28.20.111 Port 80 --------------------------------------------------------------- 1374884126.3232 (0.0050) S>C --------------------------------------------------------------- HTTP/1.0 200 OK Connection: Keep-Alive Content-Length: 25 this is new simple text. --------------------------------------------------------------- - warren_beaudry_
Nimbostratus
Great thanks.
I'm now on phase 2 of my issues :-)
Loading the image file; I have the png loaded as an ifile but can't seem to link to it from the html
This seems to work (at least the 404 part which I've tested):
when HTTP_RESPONSE {
if { [HTTP::status] != "200"} {
switch [HTTP::status] {
"404" { HTTP::respond 200 content [ifile get "/Common/404.html"] noserver
}
"503" { HTTP::respond 200 content [ifile get "/Common/503.html"] noserver
}
default { HTTP::respond 200 content [ifile get "/Common/unexpected.html"] noserver
}
}
}
}
These html pages all link to a png file (our logo) which isn't loading.
All of the articles I've found related to linking to an image file using ifile is of this type:
when HTTP_REQUEST {
if { [HTTP::uri] eq "/" } {
HTTP::respond 200 content [ifile get testapp_index_txt]
} elseif { [HTTP::uri] eq "/f5b_mini.png" } {
HTTP::respond 200 content [ifile get testapp_f5ball_img]
} else { discard }
}
Can I use HTTP_RESPONSE to evaluate the HTTP::status and then use HTTP_REQUEST to deliver the ifiles in one irule? - warren_beaudry_
Nimbostratus
Great thanks.
I'm now on phase 2 of my issues :-)
Loading the image file; I have the png loaded as an ifile but can't seem to link to it from the html
This seems to work (at least the 404 part which I've tested):
when HTTP_RESPONSE {
if { [HTTP::status] != "200"} {
switch [HTTP::status] {
"404" { HTTP::respond 200 content [ifile get "/Common/404.html"] noserver
}
"503" { HTTP::respond 200 content [ifile get "/Common/503.html"] noserver
}
default { HTTP::respond 200 content [ifile get "/Common/unexpected.html"] noserver
}
}
}
}
These html pages all link to a png file (our logo) which isn't loading.
All of the articles I've found related to linking to an image file using ifile is of this type:
when HTTP_REQUEST {
if { [HTTP::uri] eq "/" } {
HTTP::respond 200 content [ifile get testapp_index_txt]
} elseif { [HTTP::uri] eq "/f5b_mini.png" } {
HTTP::respond 200 content [ifile get testapp_f5ball_img]
} else { discard }
}
Can I use HTTP_RESPONSE to evaluate the HTTP::status and then use HTTP_REQUEST to deliver the ifiles in one irule? - nitass
Employee
These html pages all link to a png file (our logo) which isn't loading. case sensitive (i.e. filename) is not an issue, is it?
by the way, have you ever used http analyzer tool such as httpfox? i think it could be helpful when troubleshooting.
httpfox
https://addons.mozilla.org/en-us/firefox/addon/httpfox/
Can I use HTTP_RESPONSE to evaluate the HTTP::status and then use HTTP_REQUEST to deliver the ifiles in one irule?i understand what you are doing now (i.e. respond html page in HTTP_RESPONSE and image in HTTP_REQUEST) is correct. - warren_beaudry_
Nimbostratus
I ended up using html with the logo embedded so I could do it all in HTTP_RESPONSE.
I had an issue with it working in IE but not FF or Chrome; had to set the Content-Type header for it to work for all:
when HTTP_RESPONSE {
if { [HTTP::status] != "200"} {
switch [HTTP::status] {
"404" { HTTP::respond 200 -version 1.1 content [ifile get "/Common/404.html"] noserver "Content-Type" "text/html; charset=utf-8"
}
"503" { HTTP::respond 200 -version 1.1 content [ifile get "/Common/503.html"] noserver "Content-Type" "text/html; charset=utf-8"
}
default { HTTP::respond 200 -version 1.1 content [ifile get "/Common/unexpected.html"] noserver "Content-Type" "text/html; charset=utf-8"
}
}
}
}
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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