Skip to content
This repository was archived by the owner on May 27, 2019. It is now read-only.

Commit 0d9b119

Browse files
committed
fix(ngnix): handle X-Forwarded-Port and X-Forwarded-Proto properly
1 parent d3d3847 commit 0d9b119

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

nginx/config.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ http {
7575
default $scheme; # if X-Forwarded-Proto header is empty, $tmp_access_scheme will be the actual protocol used
7676
"~^(.*, ?)?http$" "http"; # account for the possibility of a comma-delimited X-Forwarded-Proto header value
7777
"~^(.*, ?)?https$" "https"; # account for the possibility of a comma-delimited X-Forwarded-Proto header value
78+
"~^(.*, ?)?ws$" "ws"; # account for the possibility of a comma-delimited X-Forwarded-Proto header value
79+
"~^(.*, ?)?wss$" "wss"; # account for the possibility of a comma-delimited X-Forwarded-Proto header value
7880
}
79-
# 2. If the request is an HTTPS request, upgrade $access_scheme to https, regardless of what the X-Forwarded-Proto
81+
# 2. If the request is an HTTPS/wss request, upgrade $access_scheme to https/wss, regardless of what the X-Forwarded-Proto
8082
# header might say.
8183
map $scheme $access_scheme {
8284
default $tmp_access_scheme;
8385
"https" "https";
86+
"wss" "wss";
8487
}
8588
8689
# Determine the forwarded port:
@@ -94,10 +97,16 @@ http {
9497
# 2. If the X-Forwarded-Port header has been set already (e.g. by a load balancer), use its
9598
# value, otherwise, the port we're forwarding for is the $standard_server_port we determined
9699
# above.
97-
map $http_x_forwarded_proto $forwarded_port {
100+
map $http_x_forwarded_port $forwarded_port {
98101
default $http_x_forwarded_port;
99102
'' $standard_server_port;
100103
}
104+
# uri_scheme will be the scheme to use when the ssl is enforced.
105+
map $access_scheme $uri_scheme {
106+
default "https";
107+
"ws" "wss";
108+
}
109+
101110
102111
{{ $sslConfig := $routerConfig.SSLConfig }}
103112
{{ $hstsConfig := $sslConfig.HSTSConfig }}{{ if $hstsConfig.Enabled }}
@@ -110,7 +119,7 @@ http {
110119
111120
{{/* Since HSTS headers are not permitted on HTTP requests, 301 redirects to HTTPS resources are also necessary. */}}
112121
{{/* This means we force HTTPS if HSTS is enabled. */}}
113-
{{ $enforceHTTPS := or $sslConfig.Enforce $hstsConfig.Enabled }}
122+
{{ $enforceSecure := or $sslConfig.Enforce $hstsConfig.Enabled }}
114123
115124
# Default server handles requests for unmapped hostnames, including healthchecks
116125
server {
@@ -201,8 +210,8 @@ http {
201210
proxy_set_header Upgrade $http_upgrade;
202211
proxy_set_header Connection $connection_upgrade;
203212
204-
{{ if or $enforceHTTPS $appConfig.SSLConfig.Enforce }}if ($access_scheme != "https") {
205-
return 301 https://$host$request_uri;
213+
{{ if or $enforceSecure $appConfig.SSLConfig.Enforce }}if ($access_scheme !~* "^https|wss$") {
214+
return 301 $uri_scheme://$host$request_uri;
206215
}{{ end }}
207216
208217
{{ if $hstsConfig.Enabled }}add_header Strict-Transport-Security $sts always;{{ end }}

0 commit comments

Comments
 (0)