This commit is contained in:
Andreas Kohn 2017-06-08 11:46:11 +00:00 committed by GitHub
commit cf697cc42d
2 changed files with 69 additions and 78 deletions

View file

@ -170,28 +170,14 @@ func buildResolvers(a interface{}) string {
} }
// buildLocation produces the location string, if the ingress has redirects // buildLocation produces the location string, if the ingress has redirects
// (specified through the ingress.kubernetes.io/rewrite-to annotation) // (specified through the ingress.kubernetes.io/rewrite-target annotation)
func buildLocation(input interface{}) string { func buildLocation(input interface{}) string {
location, ok := input.(*ingress.Location) location, ok := input.(*ingress.Location)
if !ok { if !ok {
return slash return slash
} }
path := location.Path return location.Path
if len(location.Redirect.Target) > 0 && location.Redirect.Target != path {
if path == slash {
return fmt.Sprintf("~* %s", path)
}
// baseuri regex will parse basename from the given location
baseuri := `(?<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
} }
func buildAuthLocation(input interface{}) string { func buildAuthLocation(input interface{}) string {
@ -240,7 +226,7 @@ func buildLogFormatUpstream(input interface{}) string {
} }
// buildProxyPass produces the proxy pass string, if the ingress has redirects // buildProxyPass produces the proxy pass string, if the ingress has redirects
// (specified through the ingress.kubernetes.io/rewrite-to annotation) // (specified through the ingress.kubernetes.io/rewrite-target annotation)
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will // If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will
// add a base tag in the head of the response from the service // add a base tag in the head of the response from the service
func buildProxyPass(b interface{}, loc interface{}) string { func buildProxyPass(b interface{}, loc interface{}) string {
@ -274,29 +260,31 @@ func buildProxyPass(b interface{}, loc interface{}) string {
} }
if len(location.Redirect.Target) > 0 { if len(location.Redirect.Target) > 0 {
abu := "" rewrite := ""
if location.Redirect.AddBaseURL {
// 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 '<head(.*)>' '<head$1><base href="$scheme://$http_host%v">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host%v">' r;
`, bPath, bPath)
}
if location.Redirect.Target == slash { if location.Redirect.Target == slash {
// special case redirect to / // special case redirect to /
// ie /something to / // ie /something to /
return fmt.Sprintf(` rewrite = fmt.Sprintf(`
rewrite %s(.*) /$1 break; rewrite %s(.*) /$1 break;
rewrite %s / break; rewrite %s / break;
proxy_pass %s://%s; proxy_pass %s://%s;`, path, location.Path, proto, location.Backend)
%v`, path, location.Path, proto, location.Backend, abu) } else {
rewrite = fmt.Sprintf(`
rewrite %s(.*) %s/$1 break;
proxy_pass %s://%s;`, path, location.Redirect.Target, proto, location.Backend)
} }
return fmt.Sprintf(` if location.Redirect.AddBaseURL {
rewrite %s(.*) %s/$1 break; // path has a slash suffix, so that it can be connected with baseuri directly
proxy_pass %s://%s; return fmt.Sprintf(`
%v`, path, location.Redirect.Target, proto, location.Backend, abu) location ~* %s(?<baseuri>.*) {
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host%s$baseuri">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host%s$baseuri">' r;
%v
}`, path, path, path, rewrite)
}
return rewrite
} }
// default proxy_pass // default proxy_pass

View file

@ -42,49 +42,52 @@ var (
AddBaseURL bool AddBaseURL bool
}{ }{
"invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false}, "invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false},
"redirect / to /jenkins": {"/", "/jenkins", "~* /", "redirect / to /jenkins": {"/", "/jenkins", "/", `
` rewrite /(.*) /jenkins/$1 break;
rewrite /(.*) /jenkins/$1 break; proxy_pass http://upstream-name;`, false},
proxy_pass http://upstream-name; "redirect /something to /": {"/something", "/", "/something", `
`, false}, rewrite /something/(.*) /$1 break;
"redirect /something to /": {"/something", "/", `~* ^/something\/?(?<baseuri>.*)`, ` rewrite /something / break;
rewrite /something/(.*) /$1 break; proxy_pass http://upstream-name;`, false},
rewrite /something / break; "redirect /end-with-slash/ to /not-root": {"/end-with-slash/", "/not-root", "/end-with-slash/", `
proxy_pass http://upstream-name; rewrite /end-with-slash/(.*) /not-root/$1 break;
`, false}, proxy_pass http://upstream-name;`, false},
"redirect /end-with-slash/ to /not-root": {"/end-with-slash/", "/not-root", "~* ^/end-with-slash/(?<baseuri>.*)", ` "redirect /something-complex to /not-root": {"/something-complex", "/not-root", "/something-complex", `
rewrite /end-with-slash/(.*) /not-root/$1 break; rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;`, false},
`, false}, "redirect / to /jenkins and rewrite": {"/", "/jenkins", "/", `
"redirect /something-complex to /not-root": {"/something-complex", "/not-root", `~* ^/something-complex\/?(?<baseuri>.*)`, ` location ~* /(?<baseuri>.*) {
rewrite /something-complex/(.*) /not-root/$1 break; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/$baseuri">' r;
proxy_pass http://upstream-name; subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/$baseuri">' r;
`, false},
"redirect / to /jenkins and rewrite": {"/", "/jenkins", "~* /", ` rewrite /(.*) /jenkins/$1 break;
rewrite /(.*) /jenkins/$1 break; proxy_pass http://upstream-name;
proxy_pass http://upstream-name; }`, true},
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/$baseuri">' r; "redirect /something to / and rewrite": {"/something", "/", "/something", `
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/$baseuri">' r; location ~* /something/(?<baseuri>.*) {
`, true}, subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/something/$baseuri">' r;
"redirect /something to / and rewrite": {"/something", "/", `~* ^/something\/?(?<baseuri>.*)`, ` subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/something/$baseuri">' r;
rewrite /something/(.*) /$1 break;
rewrite /something / break; rewrite /something/(.*) /$1 break;
proxy_pass http://upstream-name; rewrite /something / break;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/something/$baseuri">' r; proxy_pass http://upstream-name;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/something/$baseuri">' r; }`, true},
`, true}, "redirect /end-with-slash/ to /not-root and rewrite": {"/end-with-slash/", "/not-root", "/end-with-slash/", `
"redirect /end-with-slash/ to /not-root and rewrite": {"/end-with-slash/", "/not-root", `~* ^/end-with-slash/(?<baseuri>.*)`, ` location ~* /end-with-slash/(?<baseuri>.*) {
rewrite /end-with-slash/(.*) /not-root/$1 break; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/end-with-slash/$baseuri">' r;
proxy_pass http://upstream-name; subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/end-with-slash/$baseuri">' r;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/end-with-slash/$baseuri">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/end-with-slash/$baseuri">' r; rewrite /end-with-slash/(.*) /not-root/$1 break;
`, true}, proxy_pass http://upstream-name;
"redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", `~* ^/something-complex\/?(?<baseuri>.*)`, ` }`, true},
rewrite /something-complex/(.*) /not-root/$1 break; "redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", "/something-complex", `
proxy_pass http://upstream-name; location ~* /something-complex/(?<baseuri>.*) {
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/something-complex/$baseuri">' r; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/something-complex/$baseuri">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/something-complex/$baseuri">' r; subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$http_host/something-complex/$baseuri">' r;
`, true},
rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name;
}`, true},
} }
) )