Skip to content

Commit 6950e75

Browse files
authored
docs: improve real-ip plugin docs (#11963)
* docs: improve real-ip plugin docs * update Chinese version * fix comments & lint * Update real-ip.md * Update real-ip.md * add notes back * fix typo
1 parent a747b06 commit 6950e75

File tree

2 files changed

+257
-102
lines changed

2 files changed

+257
-102
lines changed

docs/en/latest/plugins/real-ip.md

+129-50
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ keywords:
55
- API Gateway
66
- Plugin
77
- Real IP
8-
description: This document contains information about the Apache APISIX real-ip Plugin.
8+
description: The real-ip plugin allows Apache APISIX to set the client's real IP by the IP address passed in the HTTP header or HTTP query string.
99
---
1010

1111
<!--
@@ -27,37 +27,34 @@ description: This document contains information about the Apache APISIX real-ip
2727
#
2828
-->
2929

30-
## Description
31-
32-
The `real-ip` Plugin is used to dynamically change the client's IP address and port as seen by APISIX.
33-
34-
This is more flexible but functions similarly to Nginx's [ngx_http_realip_module](https://nginx.org/en/docs/http/ngx_http_realip_module.html).
30+
<head>
31+
<link rel="canonical" href="https://docs.api7.ai/hub/real-ip" />
32+
</head>
3533

36-
:::info IMPORTANT
34+
## Description
3735

38-
This Plugin requires APISIX to run on [APISIX-Runtime](../FAQ.md#how-do-i-build-the-apisix-runtime-environment).
36+
The `real-ip` Plugin allows APISIX to set the client's real IP by the IP address passed in the HTTP header or HTTP query string. This is particularly useful when APISIX is behind a reverse proxy since the proxy could act as the request-originating client otherwise.
3937

40-
:::
38+
The Plugin is functionally similar to NGINX's [ngx_http_realip_module](https://nginx.org/en/docs/http/ngx_http_realip_module.html) but offers more flexibility.
4139

4240
## Attributes
4341

44-
| Name | Type | Required | Valid values | Description |
45-
|-------------------|---------------|----------|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
46-
| source | string | True | Any Nginx variable like `arg_realip` or `http_x_forwarded_for`. | Dynamically sets the client's IP address and an optional port, or the client's host name, from APISIX's view. |
47-
| trusted_addresses | array[string] | False | List of IPs or CIDR ranges. | Dynamically sets the `set_real_ip_from` field. |
48-
| recursive | boolean | False | True to enable, false to disable, default is false | If recursive search is disabled, the original client address that matches one of the trusted addresses is replaced by the last address sent in the configured `source`. If recursive search is enabled, the original client address that matches one of the trusted addresses is replaced by the last non-trusted address sent in the configured `source`. |
42+
| Name | Type | Required | Default | Valid values | Description |
43+
|-----------|---------|----------|---------|----------------|---------------|
44+
| source | string | True | | |A built-in [APISIX variable](https://apisix.apache.org/docs/apisix/apisix-variable/) or [NGINX variable](https://nginx.org/en/docs/varindex.html), such as `http_x_forwarded_for` or `arg_realip`. The variable value should be a valid IP address that represents the client's real IP address, with an optional port.|
45+
| trusted_addresses | array[string] | False | | array of IPv4 or IPv6 addresses (CIDR notation acceptable) | Trusted addresses that are known to send correct replacement addresses. This configuration sets the [`set_real_ip_from`](https://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from) directive. |
46+
| recursive | boolean | False | False | | If false, replace the original client address that matches one of the trusted addresses by the last address sent in the configured `source`.<br>If true, replace the original client address that matches one of the trusted addresses by the last non-trusted address sent in the configured `source`. |
4947

5048
:::note
51-
5249
If the address specified in `source` is missing or invalid, the Plugin would not change the client address.
53-
5450
:::
5551

56-
## Enable Plugin
52+
## Examples
5753

58-
The example below enables the `real-ip` Plugin on the specified Route:
54+
The examples below demonstrate how you can configure `real-ip` in different scenarios.
5955

6056
:::note
57+
6158
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:
6259

6360
```bash
@@ -66,58 +63,140 @@ admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"/
6663

6764
:::
6865

66+
### Obtain Real Client Address From URI Parameter
67+
68+
The following example demonstrates how to update the client IP address with a URI parameter.
69+
70+
Create a Route as follows. You should configure `source` to obtain value from the URL parameter `realip` using [APISIX variable](https://apisix.apache.org/docs/apisix/apisix-variable/) or [NGINX variable](https://nginx.org/en/docs/varindex.html). Use the `response-rewrite` Plugin to set response headers to verify if the client IP and port were actually updated.
71+
6972
```shell
70-
curl -i http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
71-
{
72-
"uri": "/index.html",
73+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
74+
-H "X-API-KEY: ${admin_key}" \
75+
-d '{
76+
"id": "real-ip-route",
77+
"uri": "/get",
7378
"plugins": {
74-
"real-ip": {
75-
"source": "arg_realip",
76-
"trusted_addresses": ["127.0.0.0/24"]
77-
},
78-
"response-rewrite": {
79-
"headers": {
80-
"remote_addr": "$remote_addr",
81-
"remote_port": "$remote_port"
82-
}
79+
"real-ip": {
80+
"source": "arg_realip",
81+
"trusted_addresses": ["127.0.0.0/24"]
82+
},
83+
"response-rewrite": {
84+
"headers": {
85+
"remote_addr": "$remote_addr",
86+
"remote_port": "$remote_port"
8387
}
88+
}
8489
},
8590
"upstream": {
86-
"type": "roundrobin",
87-
"nodes": {
88-
"127.0.0.1:1980": 1
89-
}
91+
"type": "roundrobin",
92+
"nodes": {
93+
"httpbin.org:80": 1
94+
}
9095
}
91-
}'
96+
}'
9297
```
9398

94-
## Example usage
95-
96-
After you have enabled the Plugin as mentioned above, you can test it as shown below:
99+
Send a request to the Route with real IP and port in the URL parameter:
97100

98101
```shell
99-
curl 'http://127.0.0.1:9080/index.html?realip=1.2.3.4:9080' -I
102+
curl -i "http://127.0.0.1:9080/get?realip=1.2.3.4:9080"
100103
```
101104

102-
```shell
103-
...
105+
You should see the response includes the following header:
106+
107+
```text
104108
remote-addr: 1.2.3.4
105109
remote-port: 9080
106110
```
107111

108-
## Delete Plugin
112+
### Obtain Real Client Address From Header
113+
114+
The following example shows how to set the real client IP when APISIX is behind a reverse proxy, such as a load balancer when the proxy exposes the real client IP in the [`X-Forwarded-For`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For) header.
109115

110-
To remove the `real-ip` Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
116+
Create a Route as follows. You should configure `source` to obtain value from the request header `X-Forwarded-For` using [APISIX variable](https://apisix.apache.org/docs/apisix/apisix-variable/) or [NGINX variable](https://nginx.org/en/docs/varindex.html). Use the `response-rewrite` Plugin to set a response header to verify if the client IP was actually updated.
111117

112118
```shell
113-
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
114-
{
115-
"uri": "/index.html",
116-
"upstream": {
117-
"type": "roundrobin",
118-
"nodes": {
119-
"127.0.0.1:1980": 1
119+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
120+
-H "X-API-KEY: ${admin_key}" \
121+
-d '{
122+
"id": "real-ip-route",
123+
"uri": "/get",
124+
"plugins": {
125+
"real-ip": {
126+
"source": "http_x_forwarded_for",
127+
"trusted_addresses": ["127.0.0.0/24"]
128+
},
129+
"response-rewrite": {
130+
"headers": {
131+
"remote_addr": "$remote_addr"
120132
}
133+
}
134+
},
135+
"upstream": {
136+
"type": "roundrobin",
137+
"nodes": {
138+
"httpbin.org:80": 1
139+
}
121140
}
141+
}'
142+
```
143+
144+
Send a request to the Route:
145+
146+
```shell
147+
curl -i "http://127.0.0.1:9080/get"
148+
```
149+
150+
You should see a response including the following header:
151+
152+
```text
153+
remote-addr: 10.26.3.19
154+
```
155+
156+
The IP address should correspond to the IP address of the request-originating client.
157+
158+
### Obtain Real Client Address Behind Multiple Proxies
159+
160+
The following example shows how to get the real client IP when APISIX is behind multiple proxies, which causes [`X-Forwarded-For`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For) header to include a list of proxy IP addresses.
161+
162+
Create a Route as follows. You should configure `source` to obtain value from the request header `X-Forwarded-For` using [APISIX variable](https://apisix.apache.org/docs/apisix/apisix-variable/) or [NGINX variable](https://nginx.org/en/docs/varindex.html). Set `recursive` to `true` so that the original client address that matches one of the trusted addresses is replaced by the last non-trusted address sent in the configured `source`. Then, use the `response-rewrite` Plugin to set a response header to verify if the client IP was actually updated.
163+
164+
```shell
165+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
166+
-H "X-API-KEY: ${admin_key}" \
167+
-d '{
168+
"id": "real-ip-route",
169+
"uri": "/get",
170+
"plugins": {
171+
"real-ip": {
172+
"source": "http_x_forwarded_for",
173+
"recursive": true,
174+
"trusted_addresses": ["192.128.0.0/16", "127.0.0.0/24"]
175+
},
176+
"response-rewrite": {
177+
"headers": {
178+
"remote_addr": "$remote_addr"
179+
}
180+
}
181+
},
182+
"upstream": {
183+
"type": "roundrobin",
184+
"nodes": {
185+
"httpbin.org:80": 1
186+
}
187+
}
122188
}'
123189
```
190+
191+
Send a request to the Route:
192+
193+
```shell
194+
curl -i "http://127.0.0.1:9080/get" \
195+
-H "X-Forwarded-For: 127.0.0.2, 192.128.1.1, 127.0.0.1"
196+
```
197+
198+
You should see a response including the following header:
199+
200+
```text
201+
remote-addr: 127.0.0.2
202+
```

0 commit comments

Comments
 (0)