【发布时间】:2019-07-22 22:09:03
【问题描述】:
这是我的计划,
我正在 Haproxy 中设置一些错误页面,我想在负载平衡器级别控制错误页面。
我有多个后端(不同的域),我想为每个后端(域)返回一个不同的错误页面。
错误页面的文件位于每个域的不同目录中的 haproxy 内。
我正在根据 http-request 主机标头处理 http-response,通过主机标头我可以选择要服务的错误页面。
我能够实现这样的目标,但是当我为每个域提供并行请求时,haproxy 开始混淆域之间的响应。
我共享 lua 和 haproxy 配置
global
debug
log /dev/log local0 debug
lua-load /root/error-page.lua
defaults
log global
mode http
retries 3
backlog 10000
maxconn 10000
timeout connect 3s
timeout client 30s
timeout server 180s
timeout tunnel 120s
timeout http-keep-alive 1s
timeout http-request 15s
timeout queue 30s
timeout tarpit 60s
option redispatch
option http-server-close
option dontlognull
option contstats
option forceclose
errorfile 404 /root/errors/404.http
errorfile 500 /root/errors/5xx.http
frontend http
bind *:80
acl error status ge 400
http-response lua.error-page if error
http-request lua.error-page
use_backend web
backend web
server web-1 10.93.3.41:1500 check
function file_check(file_name)
local file_found=io.open(file_name, "r")
if file_found==nil then
check = "file not found" .. file_name
else
check = "file found" .. file_name
local error_file2 = io.open(file_name, "r")
local file = error_file2:read("a*")
error_file2:close()
return file
end
end
function http_request_header(txn)
host = txn.sf:req_fhdr("host")
end
function http_response_header(txn)
local error_code = txn.sf:status()
local domain = "/root/errors/by-domains/" .. host .. "/error-" .. error_code .. ".html"
if file_check(domain) ~= nil then
error_page = file_check(domain)
error_path = domain
end
if error_page ~= nil then
txn:Debug("lua.error-page: rewrite error page: " .. error_path )
txn.res:set("")
txn.res:send(error_page)
txn.done(txn)
else
error_path = "back-end"
txn:Debug("lua.error-page: rewrite error page: " .. error_path )
end
end
core.register_action("error-page", { "http-res" }, http_response_header )
core.register_action("error-page", { "http-req" }, http_request_header )
【问题讨论】:
-
您使用哪个版本的 haproxy?最新版本中修复了一些错误,修复了很多问题。