Forum Discussion

Moinul_Rony's avatar
Moinul_Rony
Icon for Altostratus rankAltostratus
Sep 19, 2013

iRule to catch 4XX and 5XX HTTP error codes.

Hi,

Is there any way to catch all 4XX and 5XX http error codes rather then specifying individually ie. 404, 500, 400 etc ?

I am trying to set a friendly error page response to client when the server is sending a 4XX or 5XX HTTP response.

Currently I have the following which creates a generic html. Is there any way F5 can show a customized HTML or JSP page in reposnse?

Appreciate your assistance.. 🙂

when HTTP_REQUEST { 
set hostvar [HTTP::host] 
set urivar [HTTP::uri] 
set delay 4 } 
when HTTP_RESPONSE { 
if { [HTTP::status] == 404 } { 
HTTP::respond 200 content " Unfortunately, your request for ${hostvar}${urivar} casued a error. After 4 seconds, you’ll automatically be redirected to our home page. Sorry for this inconvenience. " "Content-Type" "text/html" } 
if { [HTTP::status] == 500 } { 
HTTP::respond 200 content " Unfortunately, your request for ${hostvar}${urivar} casued a error. After 4 seconds, you’ll automatically be redirected to our home page. Sorry for this inconvenience. " "Content-Type" "text/html" } 
if { [HTTP::status] == 400 } { 
HTTP::respond 200 content " Unfortunately, your request for ${hostvar}${urivar} casued a error. After 4 seconds, you’ll automatically be redirected to our home page. Sorry for this inconvenience. " "Content-Type" "text/html" } 
}

8 Replies

  • Also I would like to add logging in the iRule so we can have a record of the error.
  • Richard__Harlan's avatar
    Richard__Harlan
    Historic F5 Account

    Here is what I have used in the past to have the LTM try all members in the pool on a 4xx or 5xx status code. It could be easy updated to do what you want

     

    when CLIENT_ACCEPTED {
        set retry 0
    }
    when HTTP_REQUEST {
        set http_request [HTTP::request]
    }
    when HTTP_RESPONSE {
        if { ([HTTP::status] starts_with "4") || ([HTTP::status] starts_with "5")} {
            incr retry
            if { $retry <= [active_members [LB::server pool]] } {
                HTTP::retry $http_request
            } else {
                set retry 0
            }   
    } else {
        set retry 0
    }
    
    }
    when LB_SELECTED {
        log local0.info "Run $retry";
        if { ($retry > 0) && ($retry <= [active_members [LB::server pool]])} {
            LB::reselect pool [LB::server pool]
        }
    }
    • Moinul_Rony's avatar
      Moinul_Rony
      Icon for Altostratus rankAltostratus
      Thanks Richard, That really helps. How do you log the original responses using "log local0.info " ?
    • El_Jefe's avatar
      El_Jefe
      Icon for Nimbostratus rankNimbostratus
      In the When HTTP_RESPONSE, just log the original response before the if.
    • Moinul_Rony's avatar
      Moinul_Rony
      Icon for Altostratus rankAltostratus
      Hi, I was able to catch the 4xx and 5xx response and used a page response with redirect, but when some pages have internal 404 for image and css files the page is not able to render properly, how to ignore the internal 404 response and activate the irule only a whole page 404 ? Help really appreciated :) Thanks,..
  • Hello Folks,

    Got a similar one, thought to share.

    when HTTP_REQUEST {
        set hostvar [HTTP::host]
        set urivar [HTTP::uri]
        set ipvar [IP::client_addr] 
        }
        when HTTP_RESPONSE {
                if { [HTTP::status] starts_with "5" } {
                        log local0. "$ipvar requested $hostvar $urivar and received a [HTTP::status] from [IP::server_addr]" }
        }
    
  • Wrote a super simple iRule that seems to work for us (modified from the work above). Thank you for pointing us in the right direction. Hope this helps others too.

    when HTTP_RESPONSE { 
     if { ([HTTP::status] starts_with "4") || ([HTTP::status] starts_with "5")} {
        HTTP::redirect "http://maintenance.host.url/dir/dir/file"
        }
    }