Pass headers to custom error backend

This commit is contained in:
Manuel de Brito Fontes 2017-02-14 14:24:25 -03:00
parent aa02b7e085
commit d534d7f80e
2 changed files with 10 additions and 9 deletions

View file

@ -8,17 +8,17 @@ local get_upstreams = upstream.get_upstreams
local random = math.random local random = math.random
local us = get_upstreams() local us = get_upstreams()
function openURL(status) function openURL(headers, status)
local httpc = http.new() local httpc = http.new()
headers["X-Code"] = status or "404"
headers["X-Format"] = headers["Accept"] or "html",
local random_backend = get_destination() local random_backend = get_destination()
local res, err = httpc:request_uri(random_backend, { local res, err = httpc:request_uri(random_backend, {
path = "/", path = "/",
method = "GET", method = "GET",
headers = { headers = headers,
["X-Code"] = status or "404",
["X-Format"] = ngx.var.httpAccept or "html",
}
}) })
if not res then if not res then
@ -26,8 +26,9 @@ function openURL(status)
ngx.exit(500) ngx.exit(500)
end end
if ngx.var.http_cookie then for k,v in pairs(res.headers) do
ngx.header["Cookie"] = ngx.var.http_cookie ngx.log(ngx.DEBUG, k, ": ", v)
ngx.header[k] = v
end end
ngx.status = tonumber(status) ngx.status = tonumber(status)

View file

@ -407,7 +407,7 @@ http {
location / { location / {
{{ if .CustomErrors }} {{ if .CustomErrors }}
content_by_lua_block { content_by_lua_block {
openURL(503) openURL(ngx.req.get_headers(), 503)
} }
{{ else }} {{ else }}
return 503; return 503;
@ -477,7 +477,7 @@ stream {
location @custom_{{ $errCode }} { location @custom_{{ $errCode }} {
internal; internal;
content_by_lua_block { content_by_lua_block {
openURL({{ $errCode }}) openURL(ngx.req.get_headers(), {{ $errCode }})
} }
} }
{{ end }} {{ end }}