Skip to content

Commit

Permalink
Replace server redirects Lua impl with NJS impl
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabeth-dev committed Nov 25, 2024
1 parent fb4dde4 commit cf12a71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions rootfs/etc/nginx/js/nginx/ngx_srv_redirect.js
Original file line number Diff line number Diff line change
@@ -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 };
7 changes: 5 additions & 2 deletions rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 */}}
Expand Down Expand Up @@ -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 }}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/annotations/fromtowwwredirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit cf12a71

Please sign in to comment.