You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
header 'My-cool-header' is 'my-client-value, this-is-an-appended-value'
73
-
header 'My-Overwrite-Header' is 'this-is-the-only-value'
74
-
header 'Host' is 'echo.example.com:$GW_PORT'
75
-
header 'X-Forwarded-For' is '$GW_IP'
76
-
header 'Connection' is 'close'
77
-
header 'Accept' is '*/*'
78
-
```
3
+
This directory contains the YAML files used in the [HTTP Request and Response Headers](https://docs.nginx.com/nginx-gateway-fabric/how-to/traffic-management/request-response-headers/) guide.
This directory contains the YAML files used in the [HTTP Response Headers](https://docs.nginx.com/nginx-gateway-fabric/how-to/traffic-management/response-headers/) guide.
3
+
This directory contains the YAML files used in the [HTTP Request and Response Headers](https://docs.nginx.com/nginx-gateway-fabric/how-to/traffic-management/request-response-headers/) guide.
Learn how to modify the response headers of your application using NGINX Gateway Fabric.
7
+
Learn how to modify the request and response headers of your application using NGINX Gateway Fabric.
8
8
9
9
## Overview
10
10
11
-
[HTTP Header Modifiers](https://gateway-api.sigs.k8s.io/guides/http-header-modifier/?h=request#http-header-modifiers) can be used to add, modify or remove headers during the request-response lifecycle. The [ResponseHeaderModifier](https://gateway-api.sigs.k8s.io/guides/http-header-modifier/#http-response-header-modifier) is used to alter headers in a response to the client.
11
+
[HTTP Header Modifiers](https://gateway-api.sigs.k8s.io/guides/http-header-modifier/?h=request#http-header-modifiers) can be used to add, modify or remove headers during the request-response lifecycle. The [RequestHeaderModifier](https://gateway-api.sigs.k8s.io/guides/http-header-modifier/#http-request-header-modifier) is used to alter headers in a request sent by client and [ResponseHeaderModifier](https://gateway-api.sigs.k8s.io/guides/http-header-modifier/#http-response-header-modifier) is used to alter headers in a response to the client.
12
12
13
-
In this guide we will modify the headers for HTTP responses when client requests are made. For an introduction to exposing your application, we recommend that you follow the [basic guide]({{< relref "/how-to/traffic-management/routing-traffic-to-your-app.md" >}}) first.
14
-
15
-
We'll begin by configuring an app with custom headers and a straightforward HTTPRoute. We'll then observe the server response in relation to header responses. Next, we'll delve into modifying some of those headers using an HTTPRoute with filters to modify *response* headers. Our aim will be to verify whether the server responds with the modified headers.
13
+
In this guide we will first configure the headers application to modify the headers in the request. We'll then use another version of headers application to modify response headers when client requests are made. For an introduction to exposing your application, we recommend that you follow the [basic guide]({{< relref "/how-to/traffic-management/routing-traffic-to-your-app.md" >}}) first.
16
14
17
15
## Before you begin
18
16
@@ -26,12 +24,39 @@ We'll begin by configuring an app with custom headers and a straightforward HTTP
26
24
27
25
{{< note >}}In a production environment, you should have a DNS record for the external IP address that is exposed, and it should refer to the hostname that the gateway will forward for.{{< /note >}}
28
26
27
+
## HTTP Header Modifiers examples
28
+
29
+
We will configure a common gateway for the `RequestHeaderModifier` and `ResponseHeaderModifier` examples mentioned below.
30
+
31
+
### Deploy the Gateway API Resources for the Header Application
32
+
33
+
The [Gateway](https://gateway-api.sigs.k8s.io/api-types/gateway/) resource is typically deployed by the [Cluster Operator](https://gateway-api.sigs.k8s.io/concepts/roles-and-personas/#roles-and-personas_1). This Gateway defines a single listener on port 80. Since no hostname is specified, this listener matches on all hostnames. To deploy the Gateway:
34
+
35
+
```yaml
36
+
kubectl apply -f - <<EOF
37
+
apiVersion: gateway.networking.k8s.io/v1
38
+
kind: Gateway
39
+
metadata:
40
+
name: gateway
41
+
spec:
42
+
gatewayClassName: nginx
43
+
listeners:
44
+
- name: http
45
+
port: 80
46
+
protocol: HTTP
47
+
EOF
48
+
```
49
+
50
+
## RequestHeaderModifier example
51
+
52
+
In this example we will deploy NGINX Gateway Fabric and configure traffic routing for a simple echo server. We will use HTTPRoute resources to route traffic to the headers application, using the `RequestHeaderModifier` filter to modify headers in the request. Our aim will be to verify whether the server responds with the modified request headers.
53
+
29
54
### Deploy the Headers application
30
55
31
-
Begin by deploying the example application `headers`. It is a simple application that adds response headers which we'll later tweak and customize.
56
+
Begin by deploying the example application `headers`. It is a simple application that returns the request headers which we'll later tweak and customize.
### Deploy the Gateway API Resources for the Header Application
75
+
### Configure the HTTPRoute with RequestHeaderModifier filter
52
76
53
-
The [Gateway](https://gateway-api.sigs.k8s.io/api-types/gateway/) resource is typically deployed by the [Cluster Operator](https://gateway-api.sigs.k8s.io/concepts/roles-and-personas/#roles-and-personas_1). This Gateway defines a single listener on port 80. Since no hostname is specified, this listener matches on all hostnames. To deploy the Gateway:
77
+
Next, let's create a simple HTTPRoute that exposes the header application outside the cluster using the listener created in the previous section. To do this, create the following HTTPRoute:
54
78
55
79
```yaml
56
80
kubectl apply -f - <<EOF
57
81
apiVersion: gateway.networking.k8s.io/v1
58
-
kind: Gateway
82
+
kind: HTTPRoute
59
83
metadata:
60
-
name: gateway
84
+
name: headers
61
85
spec:
62
-
gatewayClassName: nginx
63
-
listeners:
64
-
- name: http
65
-
port: 80
66
-
protocol: HTTP
86
+
parentRefs:
87
+
- name: gateway
88
+
sectionName: http
89
+
hostnames:
90
+
- "echo.example.com"
91
+
rules:
92
+
- matches:
93
+
- path:
94
+
type: PathPrefix
95
+
value: /headers
96
+
filters:
97
+
- type: RequestHeaderModifier
98
+
requestHeaderModifier:
99
+
set:
100
+
- name: My-Overwrite-Header
101
+
value: this-is-the-only-value
102
+
add:
103
+
- name: Accept-Encoding
104
+
value: compress
105
+
- name: My-Cool-header
106
+
value: this-is-an-appended-value
107
+
remove:
108
+
- User-Agent
109
+
backendRefs:
110
+
- name: headers
111
+
port: 80
67
112
EOF
68
113
```
69
114
115
+
This HTTPRoute has a few important properties:
116
+
117
+
- The `parentRefs` references the Gateway resource that we created, and specifically defines the `http` listener to attach to, via the `sectionName` field.
118
+
-`echo.example.com` is the hostname that is matched for all requests to the backends defined in this HTTPRoute.
119
+
- The `match` rule defines that all requests with the path prefix `/headers` are sent to the `headers` Service.
120
+
- The filter used here is `RequestHeaderModifier` and it sets a new header `My-Overwrite-Header`, adds new headers `Accept-Encoding` and `My-cool-header` and removes `User-Agent` header.
121
+
122
+
123
+
### Send traffic to the Headers application
124
+
125
+
To access the application, we will use `curl` to send requests to the `headers` Service, including sending headers with our request.
header 'My-Cool-header' is 'my-client-value,this-is-an-appended-value'
135
+
header 'My-Overwrite-Header' is 'this-is-the-only-value'
136
+
header 'Host' is 'echo.example.com:8080'
137
+
header 'X-Forwarded-For' is '127.0.0.1'
138
+
header 'Connection' is 'close'
139
+
header 'X-Real-IP' is '127.0.0.1'
140
+
header 'X-Forwarded-Proto' is 'http'
141
+
header 'X-Forwarded-Host' is 'echo.example.com'
142
+
header 'X-Forwarded-Port' is '80'
143
+
header 'Accept' is '*/*'
144
+
```
145
+
146
+
147
+
In the output above, you can see that the headers application modifies the following custom headers:
148
+
149
+
The request headers are modified above as `Accept-Encoding` remains unchanged as we did not modify it in the curl request and `User-Agent` header is absent. The header `My-Cool-header` gets appended with the new value and `My-Overwrite-Header` gets overwritten from `dont-see-this` to `this-is-the-only-value` as defined in the _HTTPRoute_.
150
+
151
+
152
+
### Delete the resources
153
+
154
+
Delete the headers application and HTTPRoute since we will be configuring a different version of this for the examples below.
We'll begin by configuring an app with custom headers and a straightforward HTTPRoute. We'll then observe the server response in relation to header responses. Next, we'll delve into modifying some of those headers using an HTTPRoute with filters to modify response headers. Our aim will be to verify whether the server responds with the modified headers.
167
+
168
+
### Deploy the Headers application
169
+
170
+
Begin by deploying the example application `headers`. It is a simple application that adds response headers which we'll later tweak and customize.
Next, let's create a simple HTTPRoute that exposes the header application outside the cluster using the listener created in the previous section. To do this, create the following HTTPRoute:
0 commit comments