From bbeb9a766cdae4bc97cd3bfd39d0ccd490fe4840 Mon Sep 17 00:00:00 2001 From: ohmystack Date: Mon, 13 Mar 2017 19:15:09 +0800 Subject: [PATCH] Fix add-base-url The "base" tag is used for completing a relative link in current page by browser, so it should stay the same with the base url of current page. --- controllers/nginx/pkg/template/template.go | 19 +++++++----- .../nginx/pkg/template/template_test.go | 30 ++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/controllers/nginx/pkg/template/template.go b/controllers/nginx/pkg/template/template.go index 8262a58735..578dd62744 100644 --- a/controllers/nginx/pkg/template/template.go +++ b/controllers/nginx/pkg/template/template.go @@ -205,7 +205,13 @@ func buildLocation(input interface{}) string { if path == "/" { return fmt.Sprintf("~* %s", path) } - return fmt.Sprintf("~* ^%s", path) + // baseuri regex will parse basename from the given location + baseuri := `(?.*)` + if !strings.HasSuffix(path, slash) { + // Not treat the slash after "location path" as a part of baseuri + baseuri = fmt.Sprintf(`\/?%s`, baseuri) + } + return fmt.Sprintf(`~* ^%s%s`, path, baseuri) } return path @@ -273,13 +279,10 @@ func buildProxyPass(b interface{}, loc interface{}) string { if len(location.Redirect.Target) > 0 { abu := "" if location.Redirect.AddBaseURL { - bPath := location.Redirect.Target - if !strings.HasSuffix(bPath, slash) { - bPath = fmt.Sprintf("%s/", bPath) - } - - abu = fmt.Sprintf(`subs_filter '' '' r; - subs_filter '' '' r; + // path has a slash suffix, so that it can be connected with baseuri directly + bPath := fmt.Sprintf("%s%s", path, "$baseuri") + abu = fmt.Sprintf(`subs_filter '' '' r; + subs_filter '' '' r; `, bPath, bPath) } diff --git a/controllers/nginx/pkg/template/template_test.go b/controllers/nginx/pkg/template/template_test.go index 20e860c728..c6cbfbc147 100644 --- a/controllers/nginx/pkg/template/template_test.go +++ b/controllers/nginx/pkg/template/template_test.go @@ -45,33 +45,43 @@ 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 /end-with-slash/ to /not-root": {"/end-with-slash/", "/not-root", "~* ^/end-with-slash/(?.*)", ` + rewrite /end-with-slash/(.*) /not-root/$1 break; + proxy_pass http://upstream-name; + `, false}, + "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}, "redirect / to /jenkins and rewrite": {"/", "/jenkins", "~* /", ` rewrite /(.*) /jenkins/$1 break; proxy_pass http://upstream-name; - subs_filter '' '' r; - subs_filter '' '' r; + 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; + subs_filter '' '' r; + subs_filter '' '' r; + `, true}, + "redirect /end-with-slash/ to /not-root and rewrite": {"/end-with-slash/", "/not-root", `~* ^/end-with-slash/(?.*)`, ` + rewrite /end-with-slash/(.*) /not-root/$1 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; - subs_filter '' '' r; + subs_filter '' '' r; + subs_filter '' '' r; `, true}, } )