Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 30 additions & 4 deletions agent/utils/nginx/components/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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",
Expand All @@ -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"})
Expand Down
Loading