From f8b80b56fae271287b086e93d4ba8cc53eea6acb Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:56:06 +0800 Subject: [PATCH 1/3] fix: remove unnecessary proxy header settings to simplify default Nginx config --- agent/cmd/server/nginx_conf/website_default.conf | 9 --------- 1 file changed, 9 deletions(-) diff --git a/agent/cmd/server/nginx_conf/website_default.conf b/agent/cmd/server/nginx_conf/website_default.conf index 475272064819..4c773b5b67b0 100644 --- a/agent/cmd/server/nginx_conf/website_default.conf +++ b/agent/cmd/server/nginx_conf/website_default.conf @@ -4,15 +4,6 @@ server { index index.php index.html index.htm default.php default.htm default.html; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Host $server_name; - proxy_set_header X-Real-IP $remote_addr; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; - - access_log /www/sites/domain/log/access.log main; error_log /www/sites/domain/log/error.log; From 57270e529dcc7c3cdc16900e859f4ced495ac7fa Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Tue, 12 Aug 2025 12:53:25 +0800 Subject: [PATCH 2/3] fix: refactor directive appending to use AppendDirectives method in Block --- agent/utils/nginx/components/block.go | 4 ++ agent/utils/nginx/components/server.go | 87 ++++++++++++++------------ 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/agent/utils/nginx/components/block.go b/agent/utils/nginx/components/block.go index 0e07e7793ffd..d0f40b66dc0a 100644 --- a/agent/utils/nginx/components/block.go +++ b/agent/utils/nginx/components/block.go @@ -38,6 +38,10 @@ func (b *Block) FindDirectives(directiveName string) []IDirective { return directives } +func (b *Block) AppendDirectives(directives ...IDirective) { + b.Directives = append(b.Directives, directives...) +} + func (b *Block) UpdateDirective(key string, params []string) { if key == "" || len(params) == 0 { return diff --git a/agent/utils/nginx/components/server.go b/agent/utils/nginx/components/server.go index 9ed53569a7f4..a3f21fc769bb 100644 --- a/agent/utils/nginx/components/server.go +++ b/agent/utils/nginx/components/server.go @@ -267,7 +267,7 @@ func (s *Server) UpdateRootProxyForAi(proxy []string) { Block: &Block{}, } block := &Block{} - block.Directives = []IDirective{ + block.AppendDirectives( &Directive{ Name: "proxy_buffering", Parameters: []string{ @@ -298,11 +298,12 @@ func (s *Server) UpdateRootProxyForAi(proxy []string) { "off", }, }, - } - block.Directives = append(block.Directives, &Directive{ - Name: "proxy_pass", - Parameters: proxy, - }) + &Directive{ + Name: "proxy_pass", + Parameters: proxy, + }, + ) + newDir.Block = block s.UpdateDirectiveBySecondKey("location", "/", newDir) } @@ -314,7 +315,7 @@ func (s *Server) UpdateRootLocation() { Block: &Block{}, } block := &Block{} - block.Directives = append(block.Directives, &Directive{ + block.AppendDirectives(&Directive{ Name: "root", Parameters: []string{"index.html"}, }) @@ -328,10 +329,14 @@ func (s *Server) UpdateRootProxy(proxy []string) { Block: &Block{}, } block := &Block{} - block.Directives = append(block.Directives, &Directive{ - Name: "proxy_pass", - Parameters: proxy, - }) + + block.AppendDirectives( + &Directive{ + Name: "proxy_pass", + Parameters: proxy, + }, + ) + newDir.Block = block s.UpdateDirectiveBySecondKey("location", "/", newDir) } @@ -343,20 +348,22 @@ func (s *Server) UpdatePHPProxy(proxy []string, localPath string) { Block: &Block{}, } block := &Block{} - block.Directives = append(block.Directives, &Directive{ - Name: "fastcgi_pass", - Parameters: proxy, - }) - block.Directives = append(block.Directives, &Directive{ - Name: "include", - Parameters: []string{"fastcgi-php.conf"}, - }) - block.Directives = append(block.Directives, &Directive{ - Name: "include", - Parameters: []string{"fastcgi_params"}, - }) + block.AppendDirectives( + &Directive{ + Name: "fastcgi_pass", + Parameters: proxy, + }, + &Directive{ + Name: "include", + Parameters: []string{"fastcgi-php.conf"}, + }, + &Directive{ + Name: "include", + Parameters: []string{"fastcgi_params"}, + }, + ) if localPath == "" { - block.Directives = append(block.Directives, &Directive{ + block.AppendDirectives(&Directive{ Name: "set", Parameters: []string{"$real_script_name", "$fastcgi_script_name"}, }) @@ -376,21 +383,23 @@ func (s *Server) UpdatePHPProxy(proxy []string, localPath string) { }, }, } - block.Directives = append(block.Directives, ifDir) - block.Directives = append(block.Directives, &Directive{ - Name: "fastcgi_param", - Parameters: []string{"SCRIPT_FILENAME", "$document_root$real_script_name"}, - }) - block.Directives = append(block.Directives, &Directive{ - Name: "fastcgi_param", - Parameters: []string{"SCRIPT_NAME", "$real_script_name"}, - }) - block.Directives = append(block.Directives, &Directive{ - Name: "fastcgi_param", - Parameters: []string{"PATH_INFO", "$path_info"}, - }) + block.AppendDirectives( + ifDir, + &Directive{ + Name: "fastcgi_param", + Parameters: []string{"SCRIPT_FILENAME", "$document_root$real_script_name"}, + }, + &Directive{ + Name: "fastcgi_param", + Parameters: []string{"SCRIPT_NAME", "$real_script_name"}, + }, + &Directive{ + Name: "fastcgi_param", + Parameters: []string{"PATH_INFO", "$path_info"}, + }, + ) } else { - block.Directives = append(block.Directives, &Directive{ + block.AppendDirectives(&Directive{ Name: "fastcgi_param", Parameters: []string{"SCRIPT_FILENAME", localPath}, }) @@ -433,7 +442,7 @@ func (s *Server) AddHTTP2HTTPS() { Block: &Block{}, } block := &Block{} - block.Directives = append(block.Directives, &Directive{ + block.AppendDirectives(&Directive{ Name: "return", Parameters: []string{"301", "https://$host$request_uri"}, }) From a25ceefb7302ee620bbdd939338a2f19f6573d42 Mon Sep 17 00:00:00 2001 From: HynoR <20227709+HynoR@users.noreply.github.com> Date: Tue, 12 Aug 2025 12:55:53 +0800 Subject: [PATCH 3/3] fix: migrate proxy header setting to UpdateRootProxy --- agent/utils/nginx/components/server.go | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/agent/utils/nginx/components/server.go b/agent/utils/nginx/components/server.go index a3f21fc769bb..f198367e43df 100644 --- a/agent/utils/nginx/components/server.go +++ b/agent/utils/nginx/components/server.go @@ -331,6 +331,34 @@ func (s *Server) UpdateRootProxy(proxy []string) { block := &Block{} block.AppendDirectives( + &Directive{ + Name: "proxy_set_header", + Parameters: []string{"Host", "$host"}, + }, + &Directive{ + Name: "proxy_set_header", + Parameters: []string{"X-Forwarded-For", "$proxy_add_x_forwarded_for"}, + }, + &Directive{ + Name: "proxy_set_header", + Parameters: []string{"X-Forwarded-Host", "$server_name"}, + }, + &Directive{ + Name: "proxy_set_header", + Parameters: []string{"X-Real-IP", "$remote_addr"}, + }, + &Directive{ + Name: "proxy_set_header", + Parameters: []string{"Connection", "upgrade"}, + }, + &Directive{ + Name: "proxy_set_header", + Parameters: []string{"Upgrade", "$http_upgrade"}, + }, + &Directive{ + Name: "proxy_http_version", + Parameters: []string{"1.1"}, + }, &Directive{ Name: "proxy_pass", Parameters: proxy,