From 917aa9b93d796ecf1bac30be8c2b5fbb2326ebaa Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <1paneldev@sina.com> Date: Wed, 17 Dec 2025 11:11:11 +0800 Subject: [PATCH] feat: Optimize HTTP to HTTPS redirection rules for non-443 port websites --- agent/app/service/website_utils.go | 4 +++- agent/utils/nginx/components/server.go | 21 +++++++++++++------ .../views/website/website/create/index.vue | 2 +- .../website/website/domain-create/index.vue | 16 ++++---------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/agent/app/service/website_utils.go b/agent/app/service/website_utils.go index 6e214ff28..97d759db0 100644 --- a/agent/app/service/website_utils.go +++ b/agent/app/service/website_utils.go @@ -742,6 +742,7 @@ func applySSL(website *model.Website, websiteSSL model.WebsiteSSL, req request.W } httpPorts := make(map[int]struct{}) httpsPorts := make(map[int]struct{}) + sslPort := 0 hasDefaultPort := false for _, domain := range domains { @@ -769,6 +770,7 @@ func applySSL(website *model.Website, websiteSSL model.WebsiteSSL, req request.W defaultHttpPortIPV6 := "[::]:" + defaultHttpPort for port := range httpsPorts { + sslPort = port portStr := strconv.Itoa(port) server.RemoveListenByBind(portStr) server.RemoveListenByBind("[::]:" + portStr) @@ -789,7 +791,7 @@ func applySSL(website *model.Website, websiteSSL model.WebsiteSSL, req request.W server.UpdateListen(defaultHttpPortIPV6, website.DefaultServer) } } - server.AddHTTP2HTTPS() + server.AddHTTP2HTTPS(sslPort) case constant.HTTPAlso: if hasDefaultPort { server.UpdateListen(defaultHttpPort, website.DefaultServer) diff --git a/agent/utils/nginx/components/server.go b/agent/utils/nginx/components/server.go index 2ea3eb9dd..3bd0f1f24 100644 --- a/agent/utils/nginx/components/server.go +++ b/agent/utils/nginx/components/server.go @@ -2,6 +2,7 @@ package components import ( "errors" + "fmt" ) type Server struct { @@ -466,19 +467,27 @@ func (s *Server) RemoveListenByBind(bind string) { s.Listens = listens } -func (s *Server) AddHTTP2HTTPS() { +func (s *Server) AddHTTP2HTTPS(httpsPort int) { newDir := Directive{ Name: "if", Parameters: []string{"($scheme = http)"}, Block: &Block{}, } block := &Block{} - block.AppendDirectives(&Directive{ - Name: "return", - Parameters: []string{"301", "https://$host$request_uri"}, - }) + if httpsPort == 443 { + block.AppendDirectives(&Directive{ + Name: "return", + Parameters: []string{"301", "https://$host$request_uri"}, + }) + } else { + block.AppendDirectives(&Directive{ + Name: "return", + Parameters: []string{"301", fmt.Sprintf("https://$host$request_uri:%d", httpsPort)}, + }) + } + newDir.Block = block - s.UpdateDirectiveBySecondKey("if", "($scheme", newDir) + s.UpdateDirectiveBySecondKey("if", " ($scheme", newDir) } func (s *Server) UpdateAllowIPs(ips []string) { diff --git a/frontend/src/views/website/website/create/index.vue b/frontend/src/views/website/website/create/index.vue index 991d631df..35de7bd88 100644 --- a/frontend/src/views/website/website/create/index.vue +++ b/frontend/src/views/website/website/create/index.vue @@ -10,7 +10,7 @@ :validate-on-rule-change="false" v-loading="loading" > - + {{ item.label }} diff --git a/frontend/src/views/website/website/domain-create/index.vue b/frontend/src/views/website/website/domain-create/index.vue index 2a13045e2..2ce7e1882 100644 --- a/frontend/src/views/website/website/domain-create/index.vue +++ b/frontend/src/views/website/website/domain-create/index.vue @@ -13,13 +13,7 @@ :placeholder="index > 0 ? $t('website.domain') : ''" @blur="handleDomainBlur(index)" > -
- {{ $t('website.domainNotFQDN') }} -
+ {{ $t('website.domainNotFQDN') }}
@@ -64,11 +58,9 @@
-
- - {{ $t('website.batchInput') }} - -
+ + {{ $t('website.batchInput') }} +