Skip to content

Commit 23f7f96

Browse files
authored
refactor: update HTTPS listener to accept port parameter and enhance configuration (#146)
1 parent 14a8da5 commit 23f7f96

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

config/ingress/nginx.tmpl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ http {
212212
{{- if eq $server.Port 0 -}}
213213
{{/* +++++ start domain server +++++ */}}
214214
server_name {{ buildServerName $server.Hostname }} {{ range $server.Aliases }}{{ . }} {{ end }};
215-
{{ buildHTTPListener $all $server.Hostname }}
215+
{{ buildHTTPListener $all $server.Hostname }};
216216

217217
{{ if $server.EnableSSL }}
218-
{{ buildHTTPSListener $all $server.Hostname }}
218+
{{ buildHTTPSListener 443 $all $server.Hostname }};
219+
{{ buildHTTPSListener 444 $all $server.Hostname }} proxy_protocol;
219220
ssl_certificate {{ $all.SSLCertificatePath -}};
220221
ssl_certificate_key {{ $all.SSLCertificateKeyPath -}};
221222
ssl_protocols {{ $cfg.SSLProtocols }};
@@ -286,9 +287,9 @@ http {
286287
proxy_set_header Connection $conn;
287288

288289
{{ if and $server.Hostname $server.EnableAuth }}
289-
rewrite_by_lua_block {
290-
lua_ingress.force_to_https()
291-
}
290+
# rewrite_by_lua_block {
291+
# lua_ingress.force_to_https()
292+
# }
292293
access_by_lua_block {
293294
token_auth.validate()
294295
}
@@ -340,9 +341,11 @@ http {
340341
{{/* +++ start server_name, listen and ssl +++ */}}
341342
{{/* +++++ start domain server +++++ */}}
342343
server_name {{ buildServerName $server.Hostname }} {{ range $server.Aliases }}{{ . }} {{ end }} ;
344+
{{ buildHTTPListener $all $server.Hostname }};
343345

344346
{{ if $server.EnableSSL }}
345-
{{ buildHTTPSListener $all $server.Hostname }}
347+
{{ buildHTTPSListener 443 $all $server.Hostname }};
348+
{{ buildHTTPSListener 444 $all $server.Hostname }} proxy_protocol;
346349
ssl_certificate {{ $server.SslCertPath -}};
347350
ssl_certificate_key {{ $server.SslKeyPath -}};
348351
ssl_protocols {{ $cfg.SSLProtocols }};
@@ -407,9 +410,9 @@ http {
407410
proxy_set_header Connection $ws_connection;
408411

409412
{{ if and $server.Hostname $server.EnableAuth }}
410-
rewrite_by_lua_block {
411-
lua_ingress.force_to_https()
412-
}
413+
# rewrite_by_lua_block {
414+
# lua_ingress.force_to_https()
415+
# }
413416
access_by_lua_block {
414417
token_auth.validate()
415418
}
@@ -444,7 +447,7 @@ http {
444447
{{ if $all.SSLCertificatePath }}
445448
## default https server
446449
server {
447-
listen 443 ssl proxy_protocol default_server;
450+
listen 444 ssl proxy_protocol default_server;
448451

449452
ssl_certificate {{ $all.SSLCertificatePath -}};
450453
ssl_certificate_key {{ $all.SSLCertificateKeyPath -}};

internal/ingress/controllers/template/template.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func buildHTTPListener(t interface{}, s interface{}) string {
377377
return strings.Join(out, "\n")
378378
}
379379

380-
func buildHTTPSListener(t interface{}, s interface{}) string {
380+
func buildHTTPSListener(p interface{}, t interface{}, s interface{}) string {
381381
var out []string
382382

383383
tc, ok := t.(config.TemplateConfig)
@@ -392,14 +392,20 @@ func buildHTTPSListener(t interface{}, s interface{}) string {
392392
return ""
393393
}
394394

395+
port, ok := p.(int)
396+
if !ok {
397+
klog.Errorf("expected a 'int' type but %T was returned", p)
398+
return ""
399+
}
400+
395401
co := commonListenOptions(tc, hostname)
396402

397403
addrV4 := []string{""}
398404
if len(tc.Cfg.BindAddressIpv4) > 0 {
399405
addrV4 = tc.Cfg.BindAddressIpv4
400406
}
401407

402-
out = append(out, httpsListener(addrV4, co, tc)...)
408+
out = append(out, httpsListener(addrV4, co, tc, port)...)
403409

404410
if !tc.IsIPV6Enabled {
405411
return strings.Join(out, "\n")
@@ -410,18 +416,14 @@ func buildHTTPSListener(t interface{}, s interface{}) string {
410416
addrV6 = tc.Cfg.BindAddressIpv6
411417
}
412418

413-
out = append(out, httpsListener(addrV6, co, tc)...)
419+
out = append(out, httpsListener(addrV6, co, tc, port)...)
414420

415421
return strings.Join(out, "\n")
416422
}
417423

418424
func commonListenOptions(template config.TemplateConfig, hostname string) string {
419425
var out []string
420426

421-
if template.Cfg.UseProxyProtocol {
422-
out = append(out, "proxy_protocol")
423-
}
424-
425427
if hostname != "_" {
426428
return strings.Join(out, " ")
427429
}
@@ -452,7 +454,6 @@ func httpListener(addresses []string, co string, tc config.TemplateConfig) []str
452454
}
453455

454456
lo = append(lo, co)
455-
lo = append(lo, ";")
456457
return lo
457458
}
458459

@@ -469,28 +470,16 @@ func httpListener(addresses []string, co string, tc config.TemplateConfig) []str
469470
return out
470471
}
471472

472-
func httpsListener(addresses []string, co string, tc config.TemplateConfig) []string {
473+
func httpsListener(addresses []string, co string, tc config.TemplateConfig, port int) []string {
473474
out := make([]string, 0)
474475

475476
fn := func(address string) []string {
476477
lo := []string{"listen"}
477478

478-
if tc.IsSSLPassthroughEnabled {
479-
if address == "" {
480-
lo = append(lo, fmt.Sprintf("%v", tc.ListenPorts.SSLProxy))
481-
} else {
482-
lo = append(lo, fmt.Sprintf("%v:%v", address, tc.ListenPorts.SSLProxy))
483-
}
484-
485-
if !strings.Contains(co, "proxy_protocol") {
486-
lo = append(lo, "proxy_protocol")
487-
}
479+
if address == "" {
480+
lo = append(lo, fmt.Sprintf("%v", port))
488481
} else {
489-
if address == "" {
490-
lo = append(lo, fmt.Sprintf("%v", tc.ListenPorts.HTTPS))
491-
} else {
492-
lo = append(lo, fmt.Sprintf("%v:%v", address, tc.ListenPorts.HTTPS))
493-
}
482+
lo = append(lo, fmt.Sprintf("%v:%v", address, port))
494483
}
495484

496485
lo = append(lo, co)
@@ -500,7 +489,6 @@ func httpsListener(addresses []string, co string, tc config.TemplateConfig) []st
500489
lo = append(lo, "http2")
501490
}
502491

503-
lo = append(lo, ";")
504492
return lo
505493
}
506494

0 commit comments

Comments
 (0)