fix: Optimize Nginx configure settings (#9962)

* fix: remove unnecessary proxy header settings to simplify default Nginx config

* fix: refactor directive appending to use AppendDirectives method in Block

* fix: migrate proxy header setting to UpdateRootProxy
This commit is contained in:
KOMATA 2025-08-13 09:53:44 +08:00 committed by GitHub
parent 4c4df5cbb0
commit c9d4e2a4b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 80 additions and 48 deletions

View file

@ -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;

View file

@ -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

View file

@ -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,42 @@ 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_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,
},
)
newDir.Block = block
s.UpdateDirectiveBySecondKey("location", "/", newDir)
}
@ -343,20 +376,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 +411,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 +470,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"},
})