Openshift + HAproxy 503 自定义页面

Openshift + HAproxy 503 自定义页面

我正在使用 503 HTTP 状态和即将推出的页面进行维护模式。

有什么方法可以让 HAproxy 服务器端生成 503 页面而不是默认的空白/不可用页面?

我正在使用 Openshift + HAproxy + Cloudflare + PHP。

提前致谢。

Haproxy 配置(删除了一些评论):

#---------------------------------------------------------------------
# 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

答案1

有什么方法可以让 HAproxy 服务器端生成 503 页面而不是默认的空白/不可用页面?

这不是你应该问的真正问题。HAProxy 始终使用服务器的响应。错误文件仅用于生成的错误内部由 HAProxy 提供。

如果您检查 Web 服务器日志,您会发现这些特定请求实际上并未被发送到 Web 服务器。

发生这种情况是因为 HAProxy 认为您的服务器已关闭......因为它在响应健康检查时从服务器获取了 503。

如果服务器在健康检查中返回 502 或 400 或任何错误代码,您仍然会收到来自 HAProxg 的 503,因为服务器已正式关闭。

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

你显然已经删除了这一行option httpchk,但它必须存在,否则 HAProxy 只会执行第 4 层检查,而这种情况不会发生。

最简单的解决方案是从后端或默认配置中删除该行。

答案2

是。使用错误文件指令,例如通过:

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

语法如下:

errorfile <code> <file>

引用文档:

<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.

例如,该文件的内容可能是:

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> 

相关内容