From 5737277d0b4a83772e0c96416740bd0bdc9f2ad7 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Fri, 24 Oct 2025 12:36:05 +0800 Subject: [PATCH] fix: Replace RemoveDirective with RemoveDirectiveByFullParams for better parameter matching --- agent/app/service/website.go | 4 +-- agent/utils/nginx/components/location.go | 34 +++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/agent/app/service/website.go b/agent/app/service/website.go index d6c79515cb88..9c0ea8eb4fa3 100644 --- a/agent/app/service/website.go +++ b/agent/app/service/website.go @@ -1768,14 +1768,14 @@ func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) if req.Preflight { location.AddCorsOption() } else { - location.RemoveDirective("if", []string{"(", "$request_method", "=", "'OPTIONS'", ")"}) + location.RemoveDirectiveByFullParams("if", []string{"(", "$request_method", "=", "'OPTIONS'", ")"}) } } else { location.RemoveDirective("add_header", []string{"Access-Control-Allow-Origin"}) location.RemoveDirective("add_header", []string{"Access-Control-Allow-Methods"}) location.RemoveDirective("add_header", []string{"Access-Control-Allow-Headers"}) location.RemoveDirective("add_header", []string{"Access-Control-Allow-Credentials"}) - location.RemoveDirective("if", []string{"(", "$request_method", "=", "'OPTIONS'", ")"}) + location.RemoveDirectiveByFullParams("if", []string{"(", "$request_method", "=", "'OPTIONS'", ")"}) } if err = nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { return buserr.WithErr("ErrUpdateBuWebsite", err) diff --git a/agent/utils/nginx/components/location.go b/agent/utils/nginx/components/location.go index b77372292170..b89e6c9bd3e0 100644 --- a/agent/utils/nginx/components/location.go +++ b/agent/utils/nginx/components/location.go @@ -197,6 +197,7 @@ func (l *Location) UpdateDirective(key string, params []string) { l.Directives = directives } +// RemoveDirective removes a directive by its name and optional FIRST parameter match func (l *Location) RemoveDirective(key string, params []string) { directives := l.Directives var newDirectives []IDirective @@ -216,6 +217,31 @@ func (l *Location) RemoveDirective(key string, params []string) { l.Directives = newDirectives } +// RemoveDirectiveByFullParams removes a directive by its name and full parameter match +func (l *Location) RemoveDirectiveByFullParams(key string, params []string) { + directives := l.Directives + var newDirectives []IDirective + for _, dir := range directives { + if dir.GetName() == key { + oldParams := dir.GetParameters() + if len(oldParams) == len(params) { + allMatch := true + for i, param := range params { + if oldParams[i] != param { + allMatch = false + break + } + } + if allMatch { + continue + } + } + } + newDirectives = append(newDirectives, dir) + } + l.Directives = newDirectives +} + func (l *Location) ChangePath(Modifier string, Match string) { if Match != "" && Modifier != "" { l.Parameters = []string{Modifier, Match} @@ -229,8 +255,8 @@ func (l *Location) ChangePath(Modifier string, Match string) { func (l *Location) AddCache(cacheTime int, cacheUint, cacheKey string, serverCacheTime int, serverCacheUint string) { l.RemoveDirective("add_header", []string{"Cache-Control", "no-cache"}) - l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"}) - l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2|jpeg|svg|webp|avif)$"`, ")"}) + l.RemoveDirectiveByFullParams("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"}) + l.RemoveDirectiveByFullParams("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2|jpeg|svg|webp|avif)$"`, ")"}) directives := l.GetDirectives() newDir := &Directive{ Name: "if", @@ -255,8 +281,8 @@ func (l *Location) AddCache(cacheTime int, cacheUint, cacheKey string, serverCac } func (l *Location) RemoveCache(cacheKey string) { - l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"}) - l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2|jpeg|svg|webp|avif)$"`, ")"}) + l.RemoveDirectiveByFullParams("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"}) + l.RemoveDirectiveByFullParams("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2|jpeg|svg|webp|avif)$"`, ")"}) l.RemoveDirective("proxy_ignore_headers", []string{"Set-Cookie"}) l.RemoveDirective("proxy_cache", []string{cacheKey}) l.RemoveDirective("proxy_cache_key", []string{"$host$uri$is_args$args"})