For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
Strip port from Host header
When a downstream client includes a port number in the Host or authority header, such as www.example.com:9999, the port is forwarded to the upstream backend by default. Some upstream services do not accept ports in the Host header and might reject or mishandle such requests. Use the stripHostPortMode field in a ListenerPolicy to configure your gateway proxy to strip the port from the Host header before forwarding the request.
Note that the stripHostPortMode field only affects requests where the client explicitly sends a port in the Host header. If the client does not include a port, the header is forwarded unchanged regardless of this setting.
Supported modes
You can configure the following port stripping behaviors.
| Mode | Description |
|---|---|
AnyPort | Strips the port from the Host header unconditionally, regardless of what port the client sent. For example, a Host header of www.example.com:9999 becomes www.example.com. |
MatchingPort | Strips the port only when it matches the listener’s own port. For example, if the listener is exposed on port 8080 and the client sends the www.example.com:8080 header, the port is stripped. If the client sends www.example.com:9999, the port is preserved. |
Before you begin
-
Follow the Get started guide to install kgateway.
-
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
-
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESS
Strip any port
Use the AnyPort setting to strip any port from the Host header, regardless of its value. For example, a Host header of www.example.com:443 becomes www.example.com.
-
Send a request to the httpbin app and include a port number in the
Hostheader. Verify that you see the port included in theHostheader of your response.curl -v http://$INGRESS_GW_ADDRESS:8080/headers -H "host: www.example.com:9999"Example output:
{ "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com:9999" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Envoy-External-Address": [ "127.0.0.1" ], "X-Forwarded-For": [ "10.244.0.7" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "03f7152b-0546-4080-b7cb-43a4c64b0d2a" ] } } -
Create a ListenerPolicy with the
stripHostPortMode: AnyPortsetting.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: ListenerPolicy metadata: name: strip-host-port namespace: kgateway-system spec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: http default: httpSettings: stripHostPortMode: AnyPort EOFReview the following table to understand this configuration. For more information about the available fields, see the API reference.
Setting Description spec.targetRefsThe Gateway this policy applies to. spec.default.httpSettings.stripHostPortModeHow Envoy strips the port from the Host/authorityheader. In this example, theAnyPortsetting is used that removes any port that the client sends as part of the request. -
Send another request to the httpbin app. Verify that this time, port 9999 is not returned in the
Hostheader.curl -v http://$INGRESS_GW_ADDRESS:8080/headers -H "host: www.example.com:9999"Example output:
{ "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Envoy-External-Address": [ "127.0.0.1" ], "X-Forwarded-For": [ "10.244.0.7" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "03f7152b-0546-4080-b7cb-43a4c64b0d2a" ] } }
Strip listener ports only
Use the MatchingPort setting to strip the port only when it matches the listener’s own port. Ports that do not match the listener port are preserved in the header and forwarded to the upstream backend.
-
Review the port settings on your Gateway. The Gateway in this example has a listener on port 8080.
kubectl get gateway http -n kgateway-system -o yamlExample output:
apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: generation: 1 name: http namespace: kgateway-system resourceVersion: "768" uid: 6dba42be-3e96-4616-a56a-267f0f01a207 spec: gatewayClassName: kgateway listeners: - allowedRoutes: namespaces: from: All name: http port: 8080 protocol: HTTP ... -
Create or update the ListenerPolicy to enable port stripping when a client sends a port in the
Hostheader that matches the listener’s port.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: ListenerPolicy metadata: name: strip-host-port namespace: kgateway-system spec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: http default: httpSettings: stripHostPortMode: MatchingPort EOFReview the following table to understand this configuration. For more information about the available fields, see the API reference.
Setting Description spec.targetRefsThe Gateway this policy applies to. spec.default.httpSettings.stripHostPortModeHow Envoy strips the port from the Host/authorityheader. In this example, theMatchingPortsetting is used that removes a port only if it matches the listener’s own port. -
Send a request to the httpbin app and include the listener port in the
Hostheader. Verify that the port is removed from theHostheader.curl -v http://$INGRESS_GW_ADDRESS:8080/headers -H "host: www.example.com:8080"Example output:
{ "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com" ], "User-Agent": [ "curl/8.7.1" ], ... -
Send another request to the httpbin app. This time, you include a port in the
Hostheader that does not match the listener port, such as9999. Verify that the port is not removed from theHostheader.curl -v http://$INGRESS_GW_ADDRESS:8080/headers -H "host: www.example.com:9999"Example output:
{ "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com:9999" ], "User-Agent": [ "curl/8.7.1" ], ...
Cleanup
You can remove the resources that you created in this guide.kubectl delete listenerpolicy strip-host-port -n kgateway-system