I want to set dynamic cookie value(from rand function) while doing re-direction. First i tried set-cookie
option like following:
http-request redirect code 307 location %[capture.req.uri] set-cookie test=%[rand(20)]
but this gives me:
HTTP/1.1 307 Temporary Redirect
Cache-Control: no-cache
Content-length: 0
Location: /test
Set-Cookie: test=%[rand(20)]; path=/;
after reading haproxy source code i found a workaround, adding Set-Cookie
header after location with \r\n
works perfectly.
http-request redirect code 307 location %[capture.req.uri]\r\nSet-Cookie:\ test=%[rand(20)]
output:
HTTP/1.1 307 Temporary Redirect
Cache-Control: no-cache
Content-length: 0
Location: /test
Set-Cookie: test=16
I want to know whether this is a good practice?.
1 Like
I need this as well.
At first I thought I could use a http-request add-header
command to do this, but that doesnât work. After finding this: https://www.mail-archive.com/haproxy@formilux.org/msg25061.html I think your hack is the only way to do it. Although I definitely think it should have a way to add headers using log-format syntax.
happy
December 26, 2018, 4:46am
3
Lua way
Cfg:
set your cookie and location headers
âŚ
use_backend redirectWithSetCookie
âŚ
backend redirectWithSetCookie
http-request use-service lua.redirectWithSetCookie
Lua:
core.register_service(âredirectWithSetCookieâ, âhttpâ, function(applet)
applet:set_status(301)
applet:add_header(âset-cookieâ, applet.headers[âset-cookieâ][0])
applet:add_header(âlocationâ, applet.headers[âlocationâ][0])
applet:start_response()
applet:send(ââ)
end)
1 Like
Based on @happy âs idea I was able to do it easier in HA-Proxy v2.0.13 with Lua 5.3.3:
File redirect.lua :
core.register_service("RedirectAllHeaders", "http", function(applet)
applet:set_status(302)
applet:start_response()
applet:send("")
end)
HAProxy config:
global
lua-load /path/to/redirect.lua
backend bk_redir
http-request use-service lua.RedirectAllHeaders
http-response add-header Set-Cookie redir=1;\ path=/
http-response add-header X-LB-Backend bk_redir
http-response add-header Location http://nowherefast.com/
frontend fr_http_https
bind 11.22.33.44:80
default_backend bk_redir
3 Likes