Skip to content

Commit caa21ce

Browse files
feat: Optimize HTTP to HTTPS redirection rules for non-443 port websites
1 parent 8c19272 commit caa21ce

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

agent/app/service/website_utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ func applySSL(website *model.Website, websiteSSL model.WebsiteSSL, req request.W
742742
}
743743
httpPorts := make(map[int]struct{})
744744
httpsPorts := make(map[int]struct{})
745+
sslPort := 0
745746

746747
hasDefaultPort := false
747748
for _, domain := range domains {
@@ -769,6 +770,7 @@ func applySSL(website *model.Website, websiteSSL model.WebsiteSSL, req request.W
769770
defaultHttpPortIPV6 := "[::]:" + defaultHttpPort
770771

771772
for port := range httpsPorts {
773+
sslPort = port
772774
portStr := strconv.Itoa(port)
773775
server.RemoveListenByBind(portStr)
774776
server.RemoveListenByBind("[::]:" + portStr)
@@ -789,7 +791,7 @@ func applySSL(website *model.Website, websiteSSL model.WebsiteSSL, req request.W
789791
server.UpdateListen(defaultHttpPortIPV6, website.DefaultServer)
790792
}
791793
}
792-
server.AddHTTP2HTTPS()
794+
server.AddHTTP2HTTPS(sslPort)
793795
case constant.HTTPAlso:
794796
if hasDefaultPort {
795797
server.UpdateListen(defaultHttpPort, website.DefaultServer)

agent/utils/nginx/components/server.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package components
22

33
import (
44
"errors"
5+
"fmt"
56
)
67

78
type Server struct {
@@ -466,19 +467,27 @@ func (s *Server) RemoveListenByBind(bind string) {
466467
s.Listens = listens
467468
}
468469

469-
func (s *Server) AddHTTP2HTTPS() {
470+
func (s *Server) AddHTTP2HTTPS(httpsPort int) {
470471
newDir := Directive{
471472
Name: "if",
472473
Parameters: []string{"($scheme = http)"},
473474
Block: &Block{},
474475
}
475476
block := &Block{}
476-
block.AppendDirectives(&Directive{
477-
Name: "return",
478-
Parameters: []string{"301", "https://$host$request_uri"},
479-
})
477+
if httpsPort == 443 {
478+
block.AppendDirectives(&Directive{
479+
Name: "return",
480+
Parameters: []string{"301", "https://$host$request_uri"},
481+
})
482+
} else {
483+
block.AppendDirectives(&Directive{
484+
Name: "return",
485+
Parameters: []string{"301", fmt.Sprintf("https://$host$request_uri:%d", httpsPort)},
486+
})
487+
}
488+
480489
newDir.Block = block
481-
s.UpdateDirectiveBySecondKey("if", "($scheme", newDir)
490+
s.UpdateDirectiveBySecondKey("if", " ($scheme", newDir)
482491
}
483492

484493
func (s *Server) UpdateAllowIPs(ips []string) {

frontend/src/views/website/website/create/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
:validate-on-rule-change="false"
1111
v-loading="loading"
1212
>
13-
<el-form-item :label="$t('commons.table.type')">
13+
<el-form-item>
1414
<el-radio-group v-model="website.type" @change="changeType">
1515
<el-radio-button v-for="item in WebsiteTypes" :key="item.value" :value="item.value">
1616
{{ item.label }}

frontend/src/views/website/website/domain-create/index.vue

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313
:placeholder="index > 0 ? $t('website.domain') : ''"
1414
@blur="handleDomainBlur(index)"
1515
></el-input>
16-
<div
17-
v-if="domainWarnings[index]"
18-
class="el-form-item__error"
19-
style="position: relative; color: #e6a23c"
20-
>
21-
{{ $t('website.domainNotFQDN') }}
22-
</div>
16+
<span class="input-help" v-if="domainWarnings[index]">{{ $t('website.domainNotFQDN') }}</span>
2317
</el-form-item>
2418
</el-col>
2519
<el-col :span="6">
@@ -64,11 +58,9 @@
6458
</el-form-item>
6559
</el-col>
6660
</el-row>
67-
<div class="mt-1">
68-
<el-button @click="openBatchDialog" type="primary" plain>
69-
{{ $t('website.batchInput') }}
70-
</el-button>
71-
</div>
61+
<el-button @click="openBatchDialog" type="primary" plain>
62+
{{ $t('website.batchInput') }}
63+
</el-button>
7264

7365
<el-dialog v-model="batchDialogVisible" :title="$t('website.batchAdd')" width="600px">
7466
<el-input

0 commit comments

Comments
 (0)