Skip to content

Commit 6ced7bc

Browse files
committed
fix nginx#98: allow non-standard listen ports
1 parent 199e068 commit 6ced7bc

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ osx-nginx-ingress
2626
nginx-ingress
2727
osx-nginx-plus-ingress
2828
nginx-plus-ingress
29+
nginx-controller/nginx-controller
2930

3031
# NGINX Plus license files
3132
*.crt

nginx-controller/nginx/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type Config struct {
4141
JWTKey string
4242
JWTToken string
4343
JWTLoginURL string
44+
45+
Ports []int
46+
SSLPorts []int
4447
}
4548

4649
// NewDefaultConfig creates a Config with default values
@@ -53,5 +56,7 @@ func NewDefaultConfig() *Config {
5356
MainServerNamesHashMaxSize: "512",
5457
ProxyBuffering: true,
5558
HSTSMaxAge: 2592000,
59+
Ports: []int{80},
60+
SSLPorts: []int{443},
5661
}
5762
}

nginx-controller/nginx/configurator.go

+59
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"os"
7+
"strconv"
78
"strings"
89

910
"github.com/golang/glog"
@@ -132,6 +133,8 @@ func (cnf *Configurator) generateNginxCfg(ingEx *IngressEx, pems map[string]stri
132133
ProxyHideHeaders: ingCfg.ProxyHideHeaders,
133134
ProxyPassHeaders: ingCfg.ProxyPassHeaders,
134135
ServerSnippets: ingCfg.ServerSnippets,
136+
Ports: ingCfg.Ports,
137+
SSLPorts: ingCfg.SSLPorts,
135138
}
136139

137140
if pemFile, ok := pems[serverName]; ok {
@@ -311,6 +314,15 @@ func (cnf *Configurator) createConfig(ingEx *IngressEx) Config {
311314
}
312315
}
313316

317+
ports, sslPorts := getServicesPorts(ingEx)
318+
if len(ports) > 0 {
319+
ingCfg.Ports = ports
320+
}
321+
322+
if len(sslPorts) > 0 {
323+
ingCfg.SSLPorts = sslPorts
324+
}
325+
314326
return ingCfg
315327
}
316328

@@ -405,6 +417,53 @@ func parseStickyService(service string) (serviceName string, stickyCookie string
405417
return svcNameParts[1], parts[1], nil
406418
}
407419

420+
func getServicesPorts(ingEx *IngressEx) ([]int, []int) {
421+
ports := map[string][]int{}
422+
423+
annotations := []string{
424+
"nginx.org/listen-ports",
425+
"nginx.org/listen-ports-ssl",
426+
}
427+
428+
for _, annotation := range annotations {
429+
if values, exists := ingEx.Ingress.Annotations[annotation]; exists {
430+
for _, value := range strings.Split(values, ",") {
431+
if port, err := parsePort(value); err != nil {
432+
glog.Errorf(
433+
"In %v %s contains invalid declaration: %v, ignoring",
434+
ingEx.Ingress.Name,
435+
annotation,
436+
err,
437+
)
438+
} else {
439+
ports[annotation] = append(ports[annotation], port)
440+
}
441+
}
442+
}
443+
}
444+
445+
return ports[annotations[0]], ports[annotations[1]]
446+
}
447+
448+
func parsePort(value string) (int, error) {
449+
port, err := strconv.ParseInt(value, 10, 16)
450+
if err != nil {
451+
return 0, fmt.Errorf(
452+
"Unable to parse port as integer: %s\n",
453+
err,
454+
)
455+
}
456+
457+
if port <= 0 {
458+
return 0, fmt.Errorf(
459+
"Port number should be greater than zero: %q",
460+
port,
461+
)
462+
}
463+
464+
return int(port), nil
465+
}
466+
408467
func createLocation(path string, upstream Upstream, cfg *Config, websocket bool, rewrite string, ssl bool) Location {
409468
loc := Location{
410469
Path: path,

nginx-controller/nginx/nginx.go

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ type Server struct {
7676
JWTRealm string
7777
JWTToken string
7878
JWTLoginURL string
79+
80+
Ports []int
81+
SSLPorts []int
7982
}
8083

8184
// Location describes an NGINX location

nginx-controller/nginx/templates/nginx.ingress.tmpl

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ upstream {{$upstream.Name}} {
77

88
{{range $server := .Servers}}
99
server {
10-
listen 80{{if $server.ProxyProtocol}} proxy_protocol{{end}};
10+
{{range $port := $server.Ports}}
11+
listen {{$port}}{{if $server.ProxyProtocol}} proxy_protocol{{end}};
12+
{{- end}}
1113
{{if $server.SSL}}
12-
listen 443 ssl{{if $server.HTTP2}} http2{{end}}{{if $server.ProxyProtocol}} proxy_protocol{{end}};
14+
{{- range $port := $server.SSLPorts}}
15+
listen {{$port}} ssl{{if $server.HTTP2}} http2{{end}}{{if $server.ProxyProtocol}} proxy_protocol{{end}};
16+
{{- end}}
1317
ssl_certificate {{$server.SSLCertificate}};
1418
ssl_certificate_key {{$server.SSLCertificateKey}};
1519
{{end}}
@@ -28,7 +32,7 @@ server {
2832
proxy_pass_header {{$proxyPassHeader}};{{end}}
2933
{{if $server.SSL}}
3034
if ($scheme = http) {
31-
return 301 https://$host$request_uri;
35+
return 301 https://$host:{{index $server.SSLPorts 0}}$request_uri;
3236
}
3337
{{- if $server.HSTS}}
3438
proxy_hide_header Strict-Transport-Security;

0 commit comments

Comments
 (0)