From 77221b3555d50a1e442c5d24259cf662e3e4a34d Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Fri, 17 Feb 2017 10:38:52 -0300 Subject: [PATCH] Fix rewrite regex to match the start of the URL and not a substring --- controllers/nginx/pkg/template/template.go | 5 ++++- controllers/nginx/pkg/template/template_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/controllers/nginx/pkg/template/template.go b/controllers/nginx/pkg/template/template.go index cc21dccacd..5afaf9a7f8 100644 --- a/controllers/nginx/pkg/template/template.go +++ b/controllers/nginx/pkg/template/template.go @@ -237,7 +237,10 @@ func buildLocation(input interface{}) string { path := location.Path if len(location.Redirect.Target) > 0 && location.Redirect.Target != path { - return fmt.Sprintf("~* %s", path) + if path == "/" { + return fmt.Sprintf("~* %s", path) + } + return fmt.Sprintf("~* ^%s", path) } return path diff --git a/controllers/nginx/pkg/template/template_test.go b/controllers/nginx/pkg/template/template_test.go index fde02e3ef6..244232306f 100644 --- a/controllers/nginx/pkg/template/template_test.go +++ b/controllers/nginx/pkg/template/template_test.go @@ -45,12 +45,12 @@ var ( rewrite /(.*) /jenkins/$1 break; proxy_pass http://upstream-name; `, false}, - "redirect /something to /": {"/something", "/", "~* /something", ` + "redirect /something to /": {"/something", "/", "~* ^/something", ` rewrite /something/(.*) /$1 break; rewrite /something / break; proxy_pass http://upstream-name; `, false}, - "redirect /something-complex to /not-root": {"/something-complex", "/not-root", "~* /something-complex", ` + "redirect /something-complex to /not-root": {"/something-complex", "/not-root", "~* ^/something-complex", ` rewrite /something-complex/(.*) /not-root/$1 break; proxy_pass http://upstream-name; `, false}, @@ -60,14 +60,14 @@ var ( subs_filter '' '' r; subs_filter '' '' r; `, true}, - "redirect /something to / and rewrite": {"/something", "/", "~* /something", ` + "redirect /something to / and rewrite": {"/something", "/", "~* ^/something", ` rewrite /something/(.*) /$1 break; rewrite /something / break; proxy_pass http://upstream-name; subs_filter '' '' r; subs_filter '' '' r; `, true}, - "redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", "~* /something-complex", ` + "redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", "~* ^/something-complex", ` rewrite /something-complex/(.*) /not-root/$1 break; proxy_pass http://upstream-name; subs_filter '' '' r;