For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
Host rewrites
Replace the host header value before forwarding a request to a backend service.
Replace the host header value before forwarding a request to a backend service by using the URLRewrite filter.
For more information, see the Kubernetes Gateway API documentation.
Before you begin
- Set up an agentgateway proxy.
- Install the httpbin sample app.
In-cluster service host rewrites
Create an HTTPRoute resource for the httpbin app that uses the
URLRewritefilter to rewrite the hostname of the request. In this example, all incoming requests on therewrite.exampledomain are rewritten to thewww.example.comhost.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-rewrite namespace: httpbin spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system hostnames: - rewrite.example rules: - matches: - path: type: PathPrefix value: / filters: - type: URLRewrite urlRewrite: hostname: "www.example.com" backendRefs: - name: httpbin port: 8000 EOFSetting Description spec.parentRefsThe name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the agentgateway-proxygateway that was created as part of the get started guide.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the URLRewritefilter is used.spec.rules.filters.urlRewrite.hostnameThe hostname that you want to rewrite requests to. spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. Send a request to the httpbin app on the
rewrite.exampledomain. Verify that you get back a 200 HTTP response code and that you see theHost: www.example.comheader in your response.Note
The following request returns a 200 HTTP response code, because you set up an HTTPRoute for the httpbin app on the
www.example.comdomain as part of the getting started guide. If you chose a different domain for your example, make sure that you have an HTTPRoute that can be reached under the host you want to rewrite to.curl -vi http://$INGRESS_GW_ADDRESS:80/headers -H "host: rewrite.example:80"Example output:
... { "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com" ], "User-Agent": [ "curl/8.7.1" ] } }
External service host rewrites
Create an AgentgatewayBackend that represents your external service. The following example creates an AgentgatewayBackend for the
httpbin.orgdomain.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayBackend metadata: name: httpbin namespace: httpbin spec: static: host: httpbin.org port: 80 EOFCreate an HTTPRoute resource that matches incoming traffic on the
external-rewrite.exampledomain and forwards traffic to the AgentgatewayBackend that you created. Because the AgentgatewayBackend expects a different domain, you use theURLRewritefilter to rewrite the hostname fromexternal-rewrite.exampletohttpbin.org.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: backend-rewrite namespace: httpbin spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system hostnames: - external-rewrite.example rules: - filters: - type: URLRewrite urlRewrite: hostname: "httpbin.org" backendRefs: - name: httpbin kind: AgentgatewayBackend group: agentgateway.dev EOFSend a request to the
external-rewrite.exampledomain. Verify that you get back a 200 HTTP response code and that you see theHost: httpbin.orgheader in your response.curl -vi http://$INGRESS_GW_ADDRESS:80/headers -H "host: external-rewrite.example:80"Example output:
* Request completely sent off < HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: application/json content-type: application/json < content-length: 268 content-length: 268 < server: envoy server: envoy < access-control-allow-origin: * access-control-allow-origin: * < access-control-allow-credentials: true access-control-allow-credentials: true < x-envoy-upstream-service-time: 2416 x-envoy-upstream-service-time: 2416 < { "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/8.7.1" } }
Set the host with a transformation
For an AgentgatewayBackend that uses a hostname, such as a static backend or an MCP target, agentgateway automatically sets the Host header to the hostname of the backend. This automatic host rewrite runs after transformations. If a transformation policy sets the :authority or Host header, the automatic host rewrite replaces that value, even though the policy status shows Attached. To keep the value from the transformation, turn off the automatic host rewrite for the route by setting hostRewrite.mode to None. This setting is also required when the transformation is in the spec.backend section of a policy that targets the AgentgatewayBackend.
Kubernetes Service backends do not get an automatic host rewrite by default, so a transformation can set their Host header without this setting. If you explicitly set hostRewrite.mode to Auto, agentgateway also rewrites the host for a Kubernetes Service backend, to the hostname of the Service. The URLRewrite filter in the previous sections also turns off the automatic host rewrite.
Create an AgentgatewayBackend that reaches the in-cluster httpbin app by its hostname.
kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayBackend metadata: name: httpbin-static namespace: httpbin spec: static: host: httpbin.httpbin.svc.cluster.local port: 8000 EOFCreate an HTTPRoute that matches traffic on the
transform-host.exampledomain and forwards it to the AgentgatewayBackend.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: transform-host namespace: httpbin spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system hostnames: - transform-host.example rules: - backendRefs: - name: httpbin-static kind: AgentgatewayBackend group: agentgateway.dev EOFCreate an AgentgatewayPolicy that sets the
:authorityheader with a transformation and turns off the automatic host rewrite for the route.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: transform-host namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: transform-host traffic: hostRewrite: mode: None transformation: request: set: - name: ":authority" value: '"transformed.example"' EOFSetting Description spec.traffic.hostRewrite.modeSet to Noneso that the automatic host rewrite does not replace the:authorityheader from the transformation. If you leave this setting out, the backend gets thehttpbin.httpbin.svc.cluster.localhost. This setting applies to the whole route. Other hostname-based backends on the same route also lose the automatic host rewrite, so they get the host that the client sent, unless a transformation sets the host for them.spec.traffic.transformation.request.setThe :authorityheader to set on the request. The value is a CEL expression, so a fixed string needs quotes.Send a request to the
transform-host.exampledomain. Verify that the httpbin app receives thetransformed.examplehost.curl -s http://$INGRESS_GW_ADDRESS:80/headers -H "host: transform-host.example" | jq -c '.headers.Host'Example output:
["transformed.example"]
Cleanup
You can remove the resources that you created in this guide.kubectl delete httproute httpbin-rewrite -n httpbin
kubectl delete httproute backend-rewrite -n httpbin
kubectl delete AgentgatewayBackend httpbin -n httpbinIf you set the host with a transformation, delete its resources.
kubectl delete AgentgatewayPolicy transform-host -n httpbin
kubectl delete httproute transform-host -n httpbin
kubectl delete AgentgatewayBackend httpbin-static -n httpbin