Skip to content

Commit

Permalink
Set proxy host header to the exact value of the request host header (n…
Browse files Browse the repository at this point in the history
…ginx#827)

Problem: The Gateway API expects the response host header to match the 
exact value of the request host header. We set the proxy host header to the 
$host nginx variable, which does not include any characters after ":". 
This causes a conformance test to fail where the host header contains a port.

Solution: Set the proxy host header to $http_host nginx variable when it is 
non-empty. This variable contains the unmodified host header of the request. 
However, in an HTTP/1.0 request, it's possible that $http_host can be empty. 
In this case, we will use the value of $host. 
See http://nginx.org/en/docs/http/ngx_http_core_module.html#var_host.
  • Loading branch information
miledxz committed Jul 7, 2023
1 parent a0d700e commit 752f26d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions internal/nginx/config/maps_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ map {{ $m.Source }} {{ $m.Variable }} {
{{ end }}
}
{{- end }}
# Set $gw_api_compliant_host variable to the value of $http_host unless $http_host is empty, then set it to the value
# of $host. We prefer $http_host because it contains the original value of the host header, which is required by the
# Gateway API. However, in an HTTP/1.0 request, it's possible that $http_host can be empty. In this case, we will use
# the value of $host. See http://nginx.org/en/docs/http/ngx_http_core_module.html#var_host.
map $http_host $gw_api_compliant_host {
'' $host;
default $http_host;
}
`
1 change: 1 addition & 0 deletions internal/nginx/config/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestExecuteMaps(t *testing.T) {
"map ${http_my_second_add_header} $my_second_add_header_header_var {": 1,
"~.* ${http_my_second_add_header},;": 1,
"map ${http_my_set_header} $my_set_header_header_var {": 0,
"map $http_host $gw_api_compliant_host {": 1,
}

maps := string(executeMaps(conf))
Expand Down
2 changes: 1 addition & 1 deletion internal/nginx/config/servers_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ server {
{{ range $h := $l.ProxySetHeaders }}
proxy_set_header {{ $h.Name }} "{{ $h.Value }}";
{{- end }}
proxy_set_header Host $host;
proxy_set_header Host $gw_api_compliant_host;
proxy_pass {{ $l.ProxyPass }}$request_uri;
{{- end }}
}
Expand Down

0 comments on commit 752f26d

Please sign in to comment.