2

I'm using 503 HTTP Status and a coming soon page for maintenance mode.

Is there any way to get HAproxy serving server-side generated 503 page instead of the default blank/unavailable page?

I'm using Openshift + HAproxy + Cloudflare + PHP.

Thanks in advance.

Haproxy config (some comments removed):

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check
PhGeek
  • 23
  • 6

2 Answers2

0

Yes. Use the errorfile directive, for example via:

errorfile 503 /etc/haproxy/errors/503.http

The syntax is as follows:

errorfile <code> <file>

Quoting the docs:

<code> is the HTTP status code. Currently, HAProxy is capable of
       generating codes 200, 400, 403, 405, 408, 429, 500, 502, 503, and
       504.

<file> designates a file containing the full HTTP response. It is
       recommended to follow the common practice of appending ".http" to
       the filename so that people do not confuse the response with HTML
       error pages, and to use absolute paths, since files are read
       before any chroot is performed.

For example, the content of the file could be:

HTTP/1.1 503 Service Unavailable\r\n
Cache-Control: no-cache\r\n
Connection: close\r\n
Content-Type: text/html\r\n

<html> 
<head>
    <title>Service unavailable</title>
</head> 
<body>
    <h1>Service unavailable</h1>
</body> 

gxx
  • 5,483
  • 2
  • 21
  • 42
  • Ain't what I'm looking for. I want to serve original files generated by my application.Maybe by disabling HAproxy for serving error files, if it's possible. – PhGeek Jul 20 '16 at 22:00
  • @PhillippePhCaetano Sorry, it seems I got your question wrong. Could you please show a `HAProxy` log line generated by a request with a `503` response? – gxx Jul 20 '16 at 22:25
  • [ALERT] 201/012437 (192371) : proxy 'express' has no server available! [WARNING] 201/141904 (192371) : Server express/local-gear is UP, reason: Layer7 check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", check duration: 307ms. 1 active and 0 backup servers online. 0 sessions requeued, 0 total in queue. [WARNING] 201/142518 (192371) : Server express/local-gear is DOWN, reason: Layer7 wrong status, code: 503, info: "HTTP status check returned code <3C>503<3E>", check duration: 87ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue – PhGeek Jul 20 '16 at 23:03
  • @PhCaetano Additionally, please put your config inside your question. – gxx Jul 20 '16 at 23:21
  • @PhCaetano Also, I've asked for a logged request (made by a client, which should see your custom error page), not a check made by `HAProxy`. – gxx Jul 20 '16 at 23:23
  • @gh_ Do you mean php.log? It doesn't show much info. - - - [20/Jul/2016:15:21:00 -0400] "GET / HTTP/1.0" 503 17160 "-" "-" – PhGeek Jul 21 '16 at 00:24
  • Users should see the homepage with 503 but I had to deactivate `header()` function in order to work. As said the page shows a _coming soon_ message and a form for collecting emails. – PhGeek Jul 21 '16 at 00:28
0

Is there any way to get HAproxy serving server-side generated 503 page instead of the default blank/unavailable page?

This isn't the actual question you should be asking. HAProxy always uses the server's response. The error file is only used for errors generated internally by HAProxy.

If you check your web server logs, you'll find that these specific requests are not actually being sent to the web server.

This is happening because HAProxy thinks your server is down... because it's getting 503 from the server in response to the health check.

If the server were returning 502 or 400 or any error code on the health checks, you'd still receive the 503 from HAProxg because the server is, officially, down.

WARNING] 201/142518 (192371) : Server express/local-gear is DOWN, reason: Layer7 wrong status, code: 503, info: "HTTP status check returned code <3C>503<3E>", check duration: 87ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue

You have apparently redacted the line option httpchk from the configuration you posted, but it has to be there, or HAProxy would only do a layer 4 check, and this wouldn't be happening.

The simplest solution is to remove that line from the backend or default config.

Michael - sqlbot
  • 21,988
  • 1
  • 57
  • 81