我正在尝试设置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支持这个吗?
根据文档中的 错误文件:
<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的日志。你可能想再仔细看看HAProxy的日志。只是为了确保。