Skip to content

Commit 8deacae

Browse files
committed
update doc to add request header to how to guides
1 parent 8cd9d52 commit 8deacae

File tree

3 files changed

+144
-99
lines changed

3 files changed

+144
-99
lines changed
Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,3 @@
1-
# Example
1+
# HTTP Request Headers
22

3-
In this example we will deploy NGINX Gateway Fabric and configure traffic routing for a simple echo server.
4-
We will use HTTPRoute resources to route traffic to the echo server, using the `RequestHeaderModifier` filter to modify
5-
headers to the request.
6-
7-
## Running the Example
8-
9-
## 1. Deploy NGINX Gateway Fabric
10-
11-
1. Follow the [installation instructions](https://docs.nginx.com/nginx-gateway-fabric/installation/) to deploy NGINX Gateway Fabric.
12-
13-
1. Save the public IP address of NGINX Gateway Fabric into a shell variable:
14-
15-
```text
16-
GW_IP=XXX.YYY.ZZZ.III
17-
```
18-
19-
1. Save the port of NGINX Gateway Fabric:
20-
21-
```text
22-
GW_PORT=<port number>
23-
```
24-
25-
## 2. Deploy the Headers Application
26-
27-
1. Create the headers Deployment and Service:
28-
29-
```shell
30-
kubectl apply -f headers.yaml
31-
```
32-
33-
1. Check that the Pod is running in the `default` Namespace:
34-
35-
```shell
36-
kubectl -n default get pods
37-
```
38-
39-
```text
40-
NAME READY STATUS RESTARTS AGE
41-
headers-6f4b79b975-2sb28 1/1 Running 0 12s
42-
```
43-
44-
## 3. Configure Routing
45-
46-
1. Create the Gateway:
47-
48-
```shell
49-
kubectl apply -f gateway.yaml
50-
```
51-
52-
1. Create the HTTPRoute resources:
53-
54-
```shell
55-
kubectl apply -f echo-route.yaml
56-
```
57-
58-
## 4. Test the Application
59-
60-
To access the application, we will use `curl` to send requests to the `headers` Service, including sending headers with
61-
our request.
62-
Notice our configured header values can be seen in the `requestHeaders` section below, and that the `User-Agent` header
63-
is absent.
64-
65-
```shell
66-
curl -s --resolve echo.example.com:$GW_PORT:$GW_IP http://echo.example.com:$GW_PORT/headers -H "My-Cool-Header:my-client-value" -H "My-Overwrite-Header:dont-see-this"
67-
```
68-
69-
```text
70-
Headers:
71-
header 'Accept-Encoding' is 'compress'
72-
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.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# HTTP Response Headers
22

3-
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.

site/content/how-to/traffic-management/response-headers.md renamed to site/content/how-to/traffic-management/request-response-headers.md

Lines changed: 141 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
---
2-
title: "HTTP response headers"
2+
title: "HTTP Request and Response headers"
33
weight: 500
44
toc: true
55
---
66

7-
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.
88

99
## Overview
1010

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.
1212

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.
1614

1715
## Before you begin
1816

@@ -26,12 +24,39 @@ We'll begin by configuring an app with custom headers and a straightforward HTTP
2624

2725
{{< 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 >}}
2826

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+
2954
### Deploy the Headers application
3055

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.
3257

3358
```shell
34-
kubectl apply -f https://raw.githubusercontent.com/nginxinc/nginx-gateway-fabric/v1.4.0/examples/http-response-header-filter/headers.yaml
59+
kubectl apply -f https://raw.githubusercontent.com/nginxinc/nginx-gateway-fabric/v1.4.0/examples/http-request-header-filter/headers.yaml
3560
```
3661

3762
This will create the headers Service and a Deployment with one Pod. Run the following command to verify the resources were created:
@@ -41,32 +66,127 @@ kubectl get pods,svc
4166
```
4267

4368
```text
44-
NAME READY STATUS RESTARTS AGE
45-
pod/headers-6f854c478-hd2jr 1/1 Running 0 95s
69+
pod/headers-545698447b-z52kj 1/1 Running 0 23s
4670
47-
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
48-
service/headers ClusterIP 10.96.15.12 <none> 80/TCP 95s
71+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
72+
service/headers ClusterIP 10.96.26.161 <none> 80/TCP 23s
4973
```
5074

51-
### Deploy the Gateway API Resources for the Header Application
75+
### Configure the HTTPRoute with RequestHeaderModifier filter
5276

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:
5478

5579
```yaml
5680
kubectl apply -f - <<EOF
5781
apiVersion: gateway.networking.k8s.io/v1
58-
kind: Gateway
82+
kind: HTTPRoute
5983
metadata:
60-
name: gateway
84+
name: headers
6185
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
67112
EOF
68113
```
69114

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.
126+
127+
```shell
128+
curl -s --resolve echo.example.com:$GW_PORT:$GW_IP http://echo.example.com:$GW_PORT/headers -H "My-Cool-Header:my-client-value" -H "My-Overwrite-Header:dont-see-this"
129+
```
130+
131+
```text
132+
Headers:
133+
header 'Accept-Encoding' is 'compress'
134+
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.
155+
156+
```shell
157+
kubectl delete httproutes.gateway.networking.k8s.io headers
158+
```
159+
160+
```shell
161+
kubectl delete -f https://raw.githubusercontent.com/nginxinc/nginx-gateway-fabric/v1.4.0/examples/http-request-header-filter/headers.yaml
162+
```
163+
164+
## ResponseHeaderModifier example
165+
166+
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.
171+
172+
```shell
173+
kubectl apply -f https://raw.githubusercontent.com/nginxinc/nginx-gateway-fabric/v1.4.0/examples/http-response-header-filter/headers.yaml
174+
```
175+
176+
This will create the headers Service and a Deployment with one Pod. Run the following command to verify the resources were created:
177+
178+
```shell
179+
kubectl get pods,svc
180+
```
181+
182+
```text
183+
NAME READY STATUS RESTARTS AGE
184+
pod/headers-6f854c478-hd2jr 1/1 Running 0 95s
185+
186+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
187+
service/headers ClusterIP 10.96.15.12 <none> 80/TCP 95s
188+
```
189+
70190
### Configure the basic HTTPRoute
71191

72192
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

Comments
 (0)