feat: Optimize HTTP to HTTPS redirection rules for non-443 port websites

This commit is contained in:
zhengkunwang223 2025-12-17 11:11:11 +08:00
parent d9579901fe
commit 917aa9b93d
4 changed files with 23 additions and 20 deletions

View file

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

View file

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

View file

@ -10,7 +10,7 @@
:validate-on-rule-change="false"
v-loading="loading"
>
<el-form-item :label="$t('commons.table.type')">
<el-form-item>
<el-radio-group v-model="website.type" @change="changeType">
<el-radio-button v-for="item in WebsiteTypes" :key="item.value" :value="item.value">
{{ item.label }}

View file

@ -13,13 +13,7 @@
:placeholder="index > 0 ? $t('website.domain') : ''"
@blur="handleDomainBlur(index)"
></el-input>
<div
v-if="domainWarnings[index]"
class="el-form-item__error"
style="position: relative; color: #e6a23c"
>
{{ $t('website.domainNotFQDN') }}
</div>
<span class="input-help" v-if="domainWarnings[index]">{{ $t('website.domainNotFQDN') }}</span>
</el-form-item>
</el-col>
<el-col :span="6">
@ -64,11 +58,9 @@
</el-form-item>
</el-col>
</el-row>
<div class="mt-1">
<el-button @click="openBatchDialog" type="primary" plain>
{{ $t('website.batchInput') }}
</el-button>
</div>
<el-button @click="openBatchDialog" type="primary" plain>
{{ $t('website.batchInput') }}
</el-button>
<el-dialog v-model="batchDialogVisible" :title="$t('website.batchAdd')" width="600px">
<el-input