diff --git a/internal/nginx/config/maps_template.go b/internal/nginx/config/maps_template.go index 4f988fc8cf..5d63bb4eea 100644 --- a/internal/nginx/config/maps_template.go +++ b/internal/nginx/config/maps_template.go @@ -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; +} ` diff --git a/internal/nginx/config/maps_test.go b/internal/nginx/config/maps_test.go index 39846b3b90..233ff90c60 100644 --- a/internal/nginx/config/maps_test.go +++ b/internal/nginx/config/maps_test.go @@ -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)) diff --git a/internal/nginx/config/servers_template.go b/internal/nginx/config/servers_template.go index 98a00e6997..4241438c49 100644 --- a/internal/nginx/config/servers_template.go +++ b/internal/nginx/config/servers_template.go @@ -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 }} }