我正在尝试将HAProxy设置为处理504错误时的静态JSON文件。为了进行测试,我们将配置文件设置为10秒后超时,并使用errorfile选项:
defaults
log global
mode http
retries 3
timeout client 10s
timeout connect 10s
timeout server 10s
option tcplog
balance roundrobin
frontend https
maxconn 2000
bind 0.0.0.0:9000
errorfile 504 /home/user1/test/error.json
acl employee-api-service path_reg /employee/api.*
use_backend servers-employee-api if employee-api-service
backend servers-employee-api
server www.server.com 127.0.0.1:8000实际上,我试图在超时时提供JSON而不是HTML,这样后端服务就可以优雅地失败。但是,在测试中,我们不能得到任何东西,HTML或JSON。在查看响应时,它只是说它失败了,没有状态代码。我的errorfile设置正确吗?HAProxy 1.5支持这一点吗?
发布于 2020-05-01 08:01:07
根据错误文件的文档
<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 ,但您只想提供JSON。
文件还说:
For better HTTP compliance, it is
recommended that all header lines end with CR-LF and not LF alone.例如,示例配置,
errorfile 503 /etc/haproxy/errorfiles/503sorry.http演示错误文件的.http扩展的常见做法。
您可以找到一些默认错误文件的示例这里。
样本(504.http):
HTTP/1.0 504 Gateway Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>504 Gateway Time-out</h1>
The server didn't respond in time.
</body></html>因此,在您的场景中,504.http应该是这样的:
HTTP/1.0 504 Gateway Time-out
Cache-Control: no-cache
Connection: close
Content-Type: application/json
{
"message": "Gateway Timeout"
}此外,您还需要按照文档中的描述,将文件大小保持在限制范围内,即BUFSIZE (8或16 KB)。
可能有一些错误日志没有为您的JSON文件提供服务。您可能需要再次彻底查看HAProxy的日志。只是想确定一下。
https://stackoverflow.com/questions/61485269
复制相似问题