Haproxy lua http-response http-request buffer

以下是我的计划,

我在Haproxy中设立了一些错误页面,我希望在负载平衡器级别上控制错误页面。

我有多个后端(不同的域),我希望为每个后端(域)返回不同的错误页面。

错误页面的文件位于haproxy中,每个域有不同的目录。

我基于http-request主机头进行http-response处理,使用主机头可以选择要服务的错误页面。

我能够实现这样的目标,但当我为每个域发起并行请求时,haproxy开始在域之间混淆响应。

我分享lua和haproxy配置


global
  debug
  log /dev/log local0 debug
  lua-load /root/error-page.lua
ndefaults
  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 )
点赞
用户9228425
用户9228425

经过调试问题和阅读LUA文档,我发现我使用方法"txn.sf:req_fhdr("host")"从HTTP请求中提取的参数"header host"可能是一个单一值字符串,该字符串在HTTP响应之间是共享的。

2019-07-22 14:09:03