Fix rewrite regex to match the start of the URL and not a substring

This commit is contained in:
Manuel de Brito Fontes 2017-02-17 10:38:52 -03:00
parent b5819d8f4d
commit 77221b3555
2 changed files with 8 additions and 5 deletions

View file

@ -237,7 +237,10 @@ func buildLocation(input interface{}) string {
path := location.Path path := location.Path
if len(location.Redirect.Target) > 0 && location.Redirect.Target != 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 return path

View file

@ -45,12 +45,12 @@ var (
rewrite /(.*) /jenkins/$1 break; rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
`, false}, `, false},
"redirect /something to /": {"/something", "/", "~* /something", ` "redirect /something to /": {"/something", "/", "~* ^/something", `
rewrite /something/(.*) /$1 break; rewrite /something/(.*) /$1 break;
rewrite /something / break; rewrite /something / break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
`, false}, `, 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; rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
`, false}, `, false},
@ -60,14 +60,14 @@ var (
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/jenkins/">' r; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/jenkins/">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/jenkins/">' r; subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/jenkins/">' r;
`, true}, `, true},
"redirect /something to / and rewrite": {"/something", "/", "~* /something", ` "redirect /something to / and rewrite": {"/something", "/", "~* ^/something", `
rewrite /something/(.*) /$1 break; rewrite /something/(.*) /$1 break;
rewrite /something / break; rewrite /something / break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/">' r; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/">' r; subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/">' r;
`, true}, `, 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; rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/not-root/">' r; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/not-root/">' r;