Fix redirection script

This commit is contained in:
Ricardo Katz 2024-08-31 12:18:52 -05:00
parent 9b923c8961
commit 3b12461265
3 changed files with 9 additions and 11 deletions

View file

@ -3,26 +3,25 @@ local redirect_to = ngx.arg[1]
local luaconfig = ngx.shared.luaconfig local luaconfig = ngx.shared.luaconfig
local use_forwarded_headers = luaconfig:get("use_forwarded_headers") local use_forwarded_headers = luaconfig:get("use_forwarded_headers")
local listen_https_ports = luaconfig:get("listen_https_ports")
if string.sub(request_uri, -1) == "/" then if string.sub(request_uri, -1) == "/" then
request_uri = string.sub(request_uri, 1, -2) request_uri = string.sub(request_uri, 1, -2)
end end
local redirectScheme local redirectScheme = ngx.var.scheme
local redirectPort = ngx.var.server_port
if use_forwarded_headers then if use_forwarded_headers then
if not ngx.var.http_x_forwarded_proto then if ngx.var.http_x_forwarded_proto then
redirectScheme = ngx.var.scheme
else
redirectScheme = ngx.var.http_x_forwarded_proto redirectScheme = ngx.var.http_x_forwarded_proto
end end
else if ngx.var.http_x_forwarded_port then
redirectScheme = ngx.var.scheme redirectPort = ngx.var.http_x_forwarded_port
end
end end
return string.format("%s://%s:%s%s", redirectScheme, return string.format("%s://%s:%s%s", redirectScheme,
redirect_to, listen_https_ports, request_uri) redirect_to, redirectPort, request_uri)

View file

@ -8,7 +8,6 @@ local configfile = cjson.decode(content)
local luaconfig = ngx.shared.luaconfig local luaconfig = ngx.shared.luaconfig
luaconfig:set("enablemetrics", configfile.enable_metrics) luaconfig:set("enablemetrics", configfile.enable_metrics)
luaconfig:set("listen_https_ports", configfile.listen_ports.https)
luaconfig:set("use_forwarded_headers", configfile.use_forwarded_headers) luaconfig:set("use_forwarded_headers", configfile.use_forwarded_headers)
-- init modules -- init modules
local ok, res local ok, res

View file

@ -58,7 +58,7 @@ var _ = framework.DescribeAnnotation("from-to-www-redirect", func() {
WithHeader("Host", fmt.Sprintf("%s.%s", "www", host)). WithHeader("Host", fmt.Sprintf("%s.%s", "www", host)).
Expect(). Expect().
Status(http.StatusPermanentRedirect). Status(http.StatusPermanentRedirect).
Header("Location").Equal("http://fromtowwwredirect.bar.com/foo") Header("Location").Equal("http://fromtowwwredirect.bar.com:80/foo")
}) })
ginkgo.It("should redirect from www HTTPS to HTTPS", func() { ginkgo.It("should redirect from www HTTPS to HTTPS", func() {
@ -101,7 +101,7 @@ var _ = framework.DescribeAnnotation("from-to-www-redirect", func() {
WithHeader("Host", toHost). WithHeader("Host", toHost).
Expect(). Expect().
Status(http.StatusPermanentRedirect). Status(http.StatusPermanentRedirect).
Header("Location").Equal(fmt.Sprintf("https://%v", fromHost)) Header("Location").Equal(fmt.Sprintf("https://%v:443", fromHost))
ginkgo.By("sending request to domain should not redirect to www") ginkgo.By("sending request to domain should not redirect to www")
f.HTTPTestClientWithTLSConfig(&tls.Config{ f.HTTPTestClientWithTLSConfig(&tls.Config{