> For the complete documentation index, see [llms.txt](https://www.axisnow.io/zh/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.axisnow.io/zh/docs/edge/policies/real-ip/use-cases.md).

# 配置场景

不同前置代理拓扑下真实客户端 IP 的配置

客户端直连边缘的 HTTP 站点由默认规则处理，不需要配置。下面三种拓扑中需要配置的情况各对应一条规则；同时有 HTTP 和 TCP 转发端点时按表中的条件限定端点类型。配置后在日志检索中确认客户端 IP 不再是代理地址。

## 条件是否限定来源 <a href="#trust" id="trust"></a>

请求头和 Proxy Protocol 头都可以伪造，规则是否要限定只信任代理的出口地址，取决于边缘是否可能被绕过代理直接访问：

* **边缘不对公网直接暴露**：节点只有内网地址、公网入口是负载均衡，或安全组只放行代理的出口网段。此时条件用**任意**即可，不需要维护地址列表。
* **边缘的公网 IP 可达**：条件加上 客户端 IP 地址 在对象中，对象里放代理的出口网段，否则直连边缘并伪造头的请求可以冒充任意地址。网段放进 IP 地址[对象](/zh/docs/edge/policies/objects.md)后按厂商公布的列表维护，也可通过 Client API 同步；同一个对象还可以给通用访问控制的[后台只允许已知 IP](/zh/docs/edge/policies/access-control/use-cases.md#allowlist)复用，同时拦截绕过代理的直接访问。

以下各表按第二种情况写条件，第一种情况去掉网段条件即可。

## 边缘前面是 CDN 或七层代理 <a href="#behind-cdn" id="behind-cdn"></a>

典型拓扑是云厂商的七层负载均衡在前、一组边缘实例在后，或 Cloudflare、阿里云 CDN 在前做加速与防护、边缘作为源站侧的网关。代理在请求头里传递客户端 IP，常见产品如下，以厂商文档为准：

<table data-search="false"><thead><tr><th width="170">产品</th><th>携带客户端 IP 的请求头</th><th width="230">厂商文档</th></tr></thead><tbody><tr><td>Cloudflare</td><td><code>CF-Connecting-IP</code>，企业版可另开 <code>True-Client-IP</code></td><td><a href="https://developers.cloudflare.com/fundamentals/reference/http-headers/">HTTP 请求头</a></td></tr><tr><td>阿里云 CDN</td><td><code>Ali-CDN-Real-IP</code>、<code>X-Forwarded-For</code></td><td><a href="https://help.aliyun.com/knowledge_detail/155768.html">回源请求头</a></td></tr><tr><td>阿里云 ALB</td><td><code>X-Forwarded-For</code>，默认开启</td><td><a href="https://help.aliyun.com/zh/slb/application-load-balancer/use-cases/preserve-client-ip-addresses">获取客户端真实 IP</a></td></tr><tr><td>腾讯云 CLB 七层</td><td><code>X-Forwarded-For</code></td><td><a href="https://tencentcloud.com/document/product/214/3728">获取客户端真实 IP</a></td></tr><tr><td>AWS ALB</td><td><code>X-Forwarded-For</code></td><td><a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/x-forwarded-headers.html">X-Forwarded 头</a></td></tr><tr><td>Nginx、HAProxy 反向代理</td><td>按自身配置写入 <code>X-Forwarded-For</code> 或自定义头</td><td></td></tr></tbody></table>

<figure><img src="https://3062891322-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKDgGNU6JXG7cqYCk2aCz%2Fuploads%2Fgit-blob-28f95be922d4276ba33131688944c421ef0c3ec9%2Freal-ip-behind-cdn.png?alt=media" alt="客户端经 CDN 或七层代理访问边缘。(A) 代理在请求头中带上客户端 IP，边缘只信任来自代理出口网段的请求头；(B) 还原后以 X-Real-IP 转发给源站，源站读到 203.0.113.7"><figcaption></figcaption></figure>

1. 在**策略 › 对象**创建 IP 地址对象 `proxy-egress`，放入代理的出口网段。
2. 创建规则：

<table data-search="false"><thead><tr><th width="140">配置项</th><th>取值</th></tr></thead><tbody><tr><td>条件</td><td>客户端 IP 地址 在对象中 <code>proxy-egress</code>；同时有 TCP 转发端点时再加 域 在以下各项之中 <code>www.example.com</code></td></tr><tr><td>获取</td><td>有专用头时选 其他请求头，填入该头名，如 <code>CF-Connecting-IP</code>、<code>Ali-CDN-Real-IP</code>；只有 X-Forwarded-For 时选 X-Forwarded-For，取最后一个</td></tr><tr><td>转发</td><td>X-Real-IP</td></tr></tbody></table>

## 边缘前面是四层负载均衡 <a href="#behind-l4" id="behind-l4"></a>

一组边缘实例挂在云厂商的四层负载均衡后面是常见部署。云厂商的四层负载均衡默认透传源地址：不做源地址转换，转发到边缘的报文仍以客户端地址为源地址。边缘按客户端直连处理，默认规则即可，IP 封禁正常生效；边缘的安全组要放行客户端地址，而不只是负载均衡的地址。只有源地址不能透传时才需要在负载均衡上开启 Proxy Protocol，并按本节配置规则。常见产品如下，以厂商文档为准：

<table data-search="false"><thead><tr><th width="150">产品</th><th width="170">默认透传源地址</th><th>需要 Proxy Protocol 的情况</th><th width="230">厂商文档</th></tr></thead><tbody><tr><td>阿里云 CLB 四层</td><td>是</td><td>IPv6 客户端访问 IPv4 后端；仅支持 v2</td><td><a href="https://help.aliyun.com/document_detail/300766.html">四层监听获取客户端真实 IP</a></td></tr><tr><td>阿里云 NLB</td><td>服务器组开启客户端地址保持后是</td><td>TCPSSL 监听、IP 类型服务器组、IPv6 客户端访问 IPv4 后端；仅支持 v2</td><td><a href="https://help.aliyun.com/zh/slb/network-load-balancer/use-cases/obtain-client-ip-addresses">获取客户端真实 IP</a></td></tr><tr><td>腾讯云 CLB 四层</td><td>是</td><td></td><td><a href="https://tencentcloud.com/document/product/214/3728">获取客户端真实 IP</a></td></tr><tr><td>AWS NLB</td><td>实例类型目标是；IP 类型目标的 TCP、TLS 默认否</td><td>IP 类型目标、PrivateLink；仅支持 v2</td><td><a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/network/edit-target-group-attributes.html">目标组属性</a></td></tr><tr><td>HAProxy</td><td>否</td><td>总是，服务器行加 <code>send-proxy</code> 或 <code>send-proxy-v2</code></td><td><a href="https://docs.haproxy.org/3.0/configuration.html#5.2-send-proxy">配置手册</a></td></tr><tr><td>Nginx stream</td><td>否</td><td>总是，<code>proxy_protocol on</code></td><td><a href="https://nginx.org/docs/stream/ngx_stream_proxy_module.html#proxy_protocol">ngx_stream_proxy_module</a></td></tr><tr><td>LVS</td><td>DR、NAT 模式是</td><td>FullNAT 模式以 TOA 传递地址，边缘不支持读取</td><td></td></tr></tbody></table>

<figure><img src="https://3062891322-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKDgGNU6JXG7cqYCk2aCz%2Fuploads%2Fgit-blob-f8fa84ad2532a653d1d83e58d352f0be2b54f9d9%2Freal-ip-behind-l4.png?alt=media" alt="客户端经四层负载均衡访问边缘。(A) 负载均衡开启 Proxy Protocol，v1、v2 边缘自动识别，不需要单独开端口；(B) 还原后以 X-Real-IP 转发给源站"><figcaption></figcaption></figure>

1. 在负载均衡上对转发到边缘的监听开启 Proxy Protocol，v1 或 v2 均可。
2. 创建规则：

<table data-search="false"><thead><tr><th width="140">配置项</th><th>取值</th></tr></thead><tbody><tr><td>条件</td><td>客户端 IP 地址 在对象中 <code>lb-addresses</code>（负载均衡转发到边缘时使用的地址段）；同时有 TCP 转发端点时再加 域 在以下各项之中 <code>www.example.com</code></td></tr><tr><td>获取</td><td>Proxy Protocol</td></tr><tr><td>转发</td><td>X-Real-IP</td></tr></tbody></table>

## TCP 转发端点 <a href="#tcp" id="tcp"></a>

<figure><img src="https://3062891322-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKDgGNU6JXG7cqYCk2aCz%2Fuploads%2Fgit-blob-6759dc3cf497744e3e72b262ed0c68c5e8725fdb%2Freal-ip-tcp.png?alt=media" alt="TCP 转发端点的两种拓扑。客户端直连时边缘用连接地址；(A) 前面有 TCP 代理时代理开启 Proxy Protocol，边缘从中读取；(B) 两种情况都以 Proxy Protocol 或 TOA 传给源站，前者需源站开启接收，后者需源站加载内核模块"><figcaption></figcaption></figure>

TCP 连接没有请求头，边缘只能从连接地址或 Proxy Protocol 读取真实 IP，也只能用 Proxy Protocol 或 TOA 传给源站。前面的四层负载均衡或 TCP 代理不透传源地址时，在代理上开启 Proxy Protocol。

<table data-search="false"><thead><tr><th width="140">配置项</th><th>取值</th></tr></thead><tbody><tr><td>条件</td><td>TCP 在以下各项之中 <code>&#x3C;端点名></code>；前面有代理时再加 客户端 IP 地址 在对象中 <code>lb-addresses</code></td></tr><tr><td>获取</td><td>客户端直连：连接地址；前面有代理：Proxy Protocol</td></tr><tr><td>转发</td><td>Proxy Protocol 或 TOA，见下表</td></tr></tbody></table>

<table data-search="false"><thead><tr><th width="140">转发方式</th><th>源站侧要求</th><th>适用</th></tr></thead><tbody><tr><td>Proxy Protocol</td><td>在接收端口开启 Proxy Protocol，例如 Nginx 的 <code>listen … proxy_protocol</code>、HAProxy 的 <code>accept-proxy</code>。未开启时协议头会被当作业务数据，连接异常</td><td>源站软件支持 Proxy Protocol</td></tr><tr><td>TOA</td><td><a href="/zh/docs/edge/policies/real-ip/toa.md">加载 TOA 内核模块</a>，真实 IP 和端口随 TCP 选项传递，应用通过 <code>getpeername</code> 直接获得，无需改动。未加载时仅无法获得真实 IP，连接不受影响</td><td>源站软件不支持 Proxy Protocol，或不便修改配置</td></tr></tbody></table>

获取选了 Proxy Protocol 的规则必须同时配置转发方式，否则源站无法获得真实 IP：默认的原样透传只在没有规则读取 Proxy Protocol 时生效。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.axisnow.io/zh/docs/edge/policies/real-ip/use-cases.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
