From 8f14057c89a95e31db667a0d0da4143cfb3caa49 Mon Sep 17 00:00:00 2001 From: Huanle Han Date: Mon, 11 Jan 2021 23:02:21 +0800 Subject: [PATCH] fix for rewrite --- internal/ingress/controller/template/template.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 4fe6ce7a71..01d49b6305 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -395,13 +395,15 @@ func enforceRegexModifier(input interface{}) bool { // buildLocation produces the location string, if the ingress has redirects // (specified through the nginx.ingress.kubernetes.io/rewrite-target annotation) -func buildLocation(input interface{}, enforceRegex bool) string { +func buildLocation(input interface{}, allEnforceRegex bool) string { location, ok := input.(*ingress.Location) if !ok { klog.Errorf("expected an '*ingress.Location' type but %T was returned", input) return slash } + enforceRegex := needsRewrite(location) || location.Rewrite.UseRegex + path := location.Path if enforceRegex { return fmt.Sprintf(`~* "^%s"`, path) @@ -410,8 +412,14 @@ func buildLocation(input interface{}, enforceRegex bool) string { if location.PathType != nil && *location.PathType == networkingv1beta1.PathTypeExact { return fmt.Sprintf(`= %s`, path) } + prefix := "" + if allEnforceRegex { + // if some location other than this is regex, then use "^~ ". so that has a high priority. + prefix = "^~ " + + } - return path + return prefix + path } func buildAuthLocation(input interface{}, globalExternalAuthURL string) string {