From 86221823d8f10ddb59ec81a0196f357e066f8aa9 Mon Sep 17 00:00:00 2001 From: Elizabeth Martin Campos Date: Tue, 26 Nov 2024 00:14:41 +0100 Subject: [PATCH] Replace server redirects Lua impl with NJS impl --- rootfs/etc/nginx/js/nginx/ngx_srv_redirect.js | 16 ++++++++++++++++ rootfs/etc/nginx/template/nginx.tmpl | 7 +++++-- test/e2e/annotations/fromtowwwredirect.go | 4 ++-- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 rootfs/etc/nginx/js/nginx/ngx_srv_redirect.js diff --git a/rootfs/etc/nginx/js/nginx/ngx_srv_redirect.js b/rootfs/etc/nginx/js/nginx/ngx_srv_redirect.js new file mode 100644 index 0000000000..da1942bc8e --- /dev/null +++ b/rootfs/etc/nginx/js/nginx/ngx_srv_redirect.js @@ -0,0 +1,16 @@ +function srv_redirect(req) { + const redirectTo = req.variables.tmp_redirect_to; + + const requestUri = req.variables.request_uri.replace(/\/$/, ''); + + const useForwardedHeaders = req.variables.forwarded_headers + const xForwardedProto = req.variables.http_x_forwarded_proto; + const xForwardedPort = req.variables.http_x_forwarded_port; + + const redirectScheme = useForwardedHeaders && xForwardedProto ? xForwardedProto : req.variables.scheme; + const redirectPort = useForwardedHeaders && xForwardedPort ? xForwardedPort : req.variables.server_port; + + return `${redirectScheme}://${redirectTo}:${redirectPort}${requestUri}`; +} + +export default { srv_redirect }; \ No newline at end of file diff --git a/rootfs/etc/nginx/template/nginx.tmpl b/rootfs/etc/nginx/template/nginx.tmpl index 9717bfc657..91387530da 100644 --- a/rootfs/etc/nginx/template/nginx.tmpl +++ b/rootfs/etc/nginx/template/nginx.tmpl @@ -75,8 +75,10 @@ http { init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua; js_import /etc/nginx/js/nginx/ngx_conf_rewrite_auth.js; + js_import /etc/nginx/js/nginx/ngx_srv_redirect.js; js_set $njs_cache_key ngx_conf_rewrite_auth.cache_key; + js_set $njs_srv_redirect ngx_srv_redirect.srv_redirect; {{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}} {{/* we use the value of the real IP for the geo_ip module */}} @@ -576,9 +578,10 @@ http { } {{ end }} - set_by_lua_file $redirect_to /etc/nginx/lua/nginx/ngx_srv_redirect.lua {{ $redirect.To }}; + set $tmp_redirect_to '{{ $redirect.To }}'; + set $tmp_forwarded_headers '{{ $cfg.UseForwardedHeaders }}'; - return {{ $all.Cfg.HTTPRedirectCode }} $redirect_to; + return {{ $all.Cfg.HTTPRedirectCode }} $njs_srv_redirect; } ## end server {{ $redirect.From }} {{ end }} diff --git a/test/e2e/annotations/fromtowwwredirect.go b/test/e2e/annotations/fromtowwwredirect.go index a3fb3b9b5d..5d4e7af0ef 100644 --- a/test/e2e/annotations/fromtowwwredirect.go +++ b/test/e2e/annotations/fromtowwwredirect.go @@ -49,7 +49,7 @@ var _ = framework.DescribeAnnotation("from-to-www-redirect", func() { f.WaitForNginxConfiguration( func(cfg string) bool { return strings.Contains(cfg, `server_name www.fromtowwwredirect.bar.com;`) && - strings.Contains(cfg, `return 308 $redirect_to;`) + strings.Contains(cfg, `return 308 $njs_srv_redirect;`) }) ginkgo.By("sending request to www.fromtowwwredirect.bar.com") @@ -88,7 +88,7 @@ var _ = framework.DescribeAnnotation("from-to-www-redirect", func() { f.WaitForNginxServer(toHost, func(server string) bool { return strings.Contains(server, fmt.Sprintf(`server_name %v;`, toHost)) && - strings.Contains(server, `return 308 $redirect_to;`) + strings.Contains(server, `return 308 $njs_srv_redirect;`) }) ginkgo.By("sending request to www should redirect to domain")