Skip to content

Commit e5ac148

Browse files
elizabeth-devGacko
authored andcommitted
NGINX: Migrate server redirects to NJS.
1 parent 1408b74 commit e5ac148

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# setup custom paths that do not require root access
1313
pid {{ .PID }};
1414

15+
load_module /etc/nginx/modules/ngx_http_js_module.so;
16+
1517
{{ if $cfg.UseGeoIP2 }}
1618
load_module /etc/nginx/modules/ngx_http_geoip2_module.so;
1719
{{ end }}
@@ -74,6 +76,10 @@ http {
7476

7577
init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua;
7678

79+
js_import /etc/nginx/js/nginx/ngx_srv_redirect.js;
80+
81+
js_set $njs_srv_redirect ngx_srv_redirect.srv_redirect;
82+
7783
{{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}}
7884
{{/* we use the value of the real IP for the geo_ip module */}}
7985
{{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }}
@@ -572,9 +578,10 @@ http {
572578
}
573579
{{ end }}
574580

575-
set_by_lua_file $redirect_to /etc/nginx/lua/nginx/ngx_srv_redirect.lua {{ $redirect.To }};
581+
set $tmp_redirect_to '{{ $redirect.To }}';
582+
set $tmp_forwarded_headers '{{ $cfg.UseForwardedHeaders }}';
576583

577-
return {{ $all.Cfg.HTTPRedirectCode }} $redirect_to;
584+
return {{ $all.Cfg.HTTPRedirectCode }} $njs_srv_redirect;
578585
}
579586
## end server {{ $redirect.From }}
580587
{{ 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy