Skip to content

Commit 9e93396

Browse files
committed
Replace server redirects Lua impl with NJS impl
1 parent 43fe741 commit 9e93396

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function srv_redirect(req) {
2+
const redirectTo = req.variables.tmp_redirect_to;
3+
4+
const requestUri = req.variables.request_uri.replace(/\/$/, '');
5+
6+
const useForwardedHeaders = req.variables.forwarded_headers
7+
const xForwardedProto = req.variables.http_x_forwarded_proto;
8+
const xForwardedPort = req.variables.http_x_forwarded_port;
9+
10+
const redirectScheme = useForwardedHeaders && xForwardedProto ? xForwardedProto : req.variables.scheme;
11+
const redirectPort = useForwardedHeaders && xForwardedPort ? xForwardedPort : req.variables.server_port;
12+
13+
return `${redirectScheme}://${redirectTo}:${redirectPort}${requestUri}`;
14+
}
15+
16+
export default { srv_redirect };

rootfs/etc/nginx/template/nginx.tmpl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ http {
7474

7575
init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua;
7676

77+
js_import /etc/nginx/js/nginx/ngx_srv_redirect.js;
78+
79+
js_set $njs_srv_redirect ngx_srv_redirect.srv_redirect;
80+
7781
{{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}}
7882
{{/* we use the value of the real IP for the geo_ip module */}}
7983
{{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }}
@@ -572,9 +576,10 @@ http {
572576
}
573577
{{ end }}
574578

575-
set_by_lua_file $redirect_to /etc/nginx/lua/nginx/ngx_srv_redirect.lua {{ $redirect.To }};
579+
set $tmp_redirect_to '{{ $redirect.To }}';
580+
set $tmp_forwarded_headers '{{ $cfg.UseForwardedHeaders }}';
576581

577-
return {{ $all.Cfg.HTTPRedirectCode }} $redirect_to;
582+
return {{ $all.Cfg.HTTPRedirectCode }} $njs_srv_redirect;
578583
}
579584
## end server {{ $redirect.From }}
580585
{{ end }}

test/e2e/annotations/fromtowwwredirect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var _ = framework.DescribeAnnotation("from-to-www-redirect", func() {
4949
f.WaitForNginxConfiguration(
5050
func(cfg string) bool {
5151
return strings.Contains(cfg, `server_name www.fromtowwwredirect.bar.com;`) &&
52-
strings.Contains(cfg, `return 308 $redirect_to;`)
52+
strings.Contains(cfg, `return 308 $njs_srv_redirect;`)
5353
})
5454

5555
ginkgo.By("sending request to www.fromtowwwredirect.bar.com")
@@ -88,7 +88,7 @@ var _ = framework.DescribeAnnotation("from-to-www-redirect", func() {
8888
f.WaitForNginxServer(toHost,
8989
func(server string) bool {
9090
return strings.Contains(server, fmt.Sprintf(`server_name %v;`, toHost)) &&
91-
strings.Contains(server, `return 308 $redirect_to;`)
91+
strings.Contains(server, `return 308 $njs_srv_redirect;`)
9292
})
9393

9494
ginkgo.By("sending request to www should redirect to domain")

0 commit comments

Comments
 (0)