On this page
Module ngx_http_realip_module
ngx_http_realip_module
模块用于将客户端地址和可选端口更改为在指定的 Headers 字段中发送的地址。
默认情况下未构建此模块,应使用--with-http_realip_module
配置参数启用它。
Example Configuration
set_real_ip_from 192.168.1.0/24;
set_real_ip_from 192.168.2.1;
set_real_ip_from 2001:0db8::/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
Directives
Syntax: | set_real_ip_from address | CIDR | unix:; |
Default: | — |
Context: | http , server , location |
定义已知可发送正确替换地址的可信地址。如果指定了特殊值unix:
,则将信任所有 UNIX 域套接字。也可以使用主机名(1.13.1)指定受信任的地址。
Note
从 1.3.0 和 1.2.1 版本开始支持 IPv6 地址。
Syntax: | real_ip_header field | X-Real-IP | X-Forwarded-For | proxy_protocol; |
Default: | real_ip_header X-Real-IP; |
Context: | http , server , location |
定义请求 Headers 字段,其值将用于替换客户端地址。
包含可选端口的请求 Headers 字段值还用于替换客户端端口(1.11.0)。地址和端口应根据RFC 3986指定。
proxy_protocol
参数(1.5.12)将客户端地址从 PROXY 协议 Headers 更改为该地址。必须先通过在listen指令中设置proxy_protocol
参数来启用 PROXY 协议。
Syntax: | real_ip_recursive on | off; |
Default: | real_ip_recursive off; |
Context: | http , server , location |
该指令出现在版本 1.3.0 和 1.2.1 中。
如果禁用了递归搜索,则将与其中一个受信任地址匹配的原始客户端地址替换为由real_ip_header指令定义的请求 Headers 字段中发送的最后一个地址。如果启用了递归搜索,则与请求地址之一匹配的原始客户端地址将替换为在请求 Headers 字段中发送的最后一个不受信任的地址。
Embedded Variables
$realip_remote_addr
- 保留原始客户地址(1.9.7)
$realip_remote_port
- 保留原始客户端端口(1.11.0)