real-ip
real-ip 插件允许 APISIX 通过 HTTP 请求头或 HTTP 查询字符串中传递的 IP 地址来设置客户端的真实 IP。当 APISIX 位于反向代理之后时,这特别有用,因为否则代理可能会充当请求发起客户端。
该插件的功能类似于 NGINX 的 ngx_http_realip_module,但提供了更大的灵活性。
示例
下面的示例展示了如何在不同场景下配置 real-ip。
从 URI 参数获取真实客户端地址
以下示例展示了如何使用 URI 参数更新客户端 IP 地址。
创建一个路由如下:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "real-ip-route",
"uri": "/get",
"plugins": {
"real-ip": {
"source": "arg_realip",
"trusted_addresses": ["127.0.0.0/24"]
},
"response-rewrite": {
"headers": {
"remote_addr": "$remote_addr",
"remote_port": "$remote_port"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- name: real-ip-route
uris:
- /get
plugins:
real-ip:
source: arg_realip
trusted_addresses:
- 127.0.0.0/24
response-rewrite:
headers:
remote_addr: $remote_addr
remote_port: $remote_port
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: real-ip-plugin-config
spec:
plugins:
- name: real-ip
config:
source: arg_realip
trusted_addresses:
- 127.0.0.0/24
- name: response-rewrite
config:
headers:
remote_addr: $remote_addr
remote_port: $remote_port
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: real-ip-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: real-ip-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: real-ip-route
spec:
ingressClassName: apisix
http:
- name: real-ip-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
plugins:
- name: real-ip
config:
source: arg_realip
trusted_addresses:
- 127.0.0.0/24
- name: response-rewrite
config:
headers:
remote_addr: $remote_addr
remote_port: $remote_port
应用配置:
kubectl apply -f real-ip-ic.yaml
❶ 配置 source 以使用 内置变量 从 URL 参数 realip 获取值。
❷ 使用 response-rewrite 插件设置响应头,以验证客户端 IP 和端口是否实际已更新。
在 URL 参数中带上真实 IP 和端口发送请求到该路由:
curl -i "http://127.0.0.1:9080/get?realip=1.2.3.4:9080"
你应该看到响应包含以下请求头:
remote_addr: 1.2.3.4
remote_port: 9080
从请求头获取真实客户端地址
以下示例展示了当 APISIX 位于反向代理(如负载均衡器)之后时,如果代理在 X-Forwarded-For 请求头中暴露了真实客户端 IP,如何设置真实客户端 IP。
创建一个路由如下:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "real-ip-route",
"uri": "/get",
"plugins": {
"real-ip": {
"source": "http_x_forwarded_for",
"trusted_addresses": ["127.0.0.0/24"]
},
"response-rewrite": {
"headers": {
"remote_addr": "$remote_addr"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- name: real-ip-route
uris:
- /get
plugins:
real-ip:
source: http_x_forwarded_for
trusted_addresses:
- 127.0.0.0/24
response-rewrite:
headers:
remote_addr: $remote_addr
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: real-ip-plugin-config
spec:
plugins:
- name: real-ip
config:
source: http_x_forwarded_for
trusted_addresses:
- 127.0.0.0/24
- name: response-rewrite
config:
headers:
remote_addr: $remote_addr
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: real-ip-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: real-ip-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: real-ip-route
spec:
ingressClassName: apisix
http:
- name: real-ip-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
plugins:
- name: real-ip
config:
source: http_x_forwarded_for
trusted_addresses:
- 127.0.0.0/24
- name: response-rewrite
config:
headers:
remote_addr: $remote_addr
应用配置:
kubectl apply -f real-ip-ic.yaml
❶ 配置 source 以使用 内置变量 从请求头 X-Forwarded-For 获取值。
❷ 使用 response-rewrite 插件设置响应头,以验证客户端 IP 是否实际已更新。
发送请求到该路由:
curl -i "http://127.0.0.1:9080/get" \
-H "X-Forwarded-For: 10.26.3.19"
你应该看到包含以下请求头的响应:
remote_addr: 10.26.3.19
IP 地址应对应于请求发起客户端的 IP 地址。
从多层代理后获取真实客户端地址
以下示例展示了当 APISIX 位于多层代理之后时,如何获取真实客户端 IP,这会导致 X-Forwarded-For 请求头包含代理 IP 地址列表。
创建一个路由如下:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "real-ip-route",
"uri": "/get",
"plugins": {
"real-ip": {
"source": "http_x_forwarded_for",
"recursive": true,
"trusted_addresses": ["192.128.0.0/16", "127.0.0.1/32"]
},
"response-rewrite": {
"headers": {
"remote_addr": "$remote_addr"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
services:
- name: httpbin
routes:
- name: real-ip-route
uris:
- /get
plugins:
real-ip:
source: http_x_forwarded_for
recursive: true
trusted_addresses:
- 192.128.0.0/16
- 127.0.0.1/32
response-rewrite:
headers:
remote_addr: $remote_addr
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到网关:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: real-ip-plugin-config
spec:
plugins:
- name: real-ip
config:
source: http_x_forwarded_for
recursive: true
trusted_addresses:
- 192.128.0.0/16
- 127.0.0.1/32
- name: response-rewrite
config:
headers:
remote_addr: $remote_addr
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: real-ip-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: real-ip-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: real-ip-route
spec:
ingressClassName: apisix
http:
- name: real-ip-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
plugins:
- name: real-ip
config:
source: http_x_forwarded_for
recursive: true
trusted_addresses:
- 192.128.0.0/16
- 127.0.0.1/32
- name: response-rewrite
config:
headers:
remote_addr: $remote_addr
应用配置:
kubectl apply -f real-ip-ic.yaml
❶ 配置 source 以使用 内置变量 从请求头 X-Forwarded-For 获取值。
❷ 将 recursive 设置为 true,以便将与受信任地址之一匹配的原始客户端地址替换为配置的 source 中发送的最后一个非受信任地址。
❸ 使用 response-rewrite 插件设置响应头,以验证客户端 IP 是否实际已更新。
发送请求到该路由:
curl -i "http://127.0.0.1:9080/get" \
-H "X-Forwarded-For: 127.0.0.2, 192.128.1.1, 127.0.0.1"
你应该看到包含以下请求头的响应:
remote_addr: 127.0.0.2