# 处理终端用户真实IP的“信任链”

**URL:** <https://meta.discourse.org/t/handling-the-chain-of-trust-of-the-end-users-real-ip/406372>\
**Category:** Self-Hosting\
**Tags:** docker, how-to\
**Created:** [2026年六月29日 16:57 UTC](https://meta.discourse.org/t/handling-the-chain-of-trust-of-the-end-users-real-ip/406372 "2026-06-29T16:57:59Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)\
**Post date:** [2026年六月30日 22:56 UTC](https://meta.discourse.org/t/handling-the-chain-of-trust-of-the-end-users-real-ip/406372/5 "2026-06-30T22:56:47Z")

</div>

> [@MichaIng](#):
>
> 现在设置 `X-Forwarded-For` 的目的是什么？

这确实是个好问题，我们直接删除它也能得到相同的结果（不过……请看下文）

> [@MichaIng](#):
>
> 这难道不是 Discourse 的 bug 吗？没有一致地解析/使用该 header 的通用值

应用边界实际上是 nginx 本身，而不是 Discourse 或 Rails。因此，关于信任哪些远程代理的具体决策是在应用入口点（即 nginx）做出的。然后，nginx 可以将该决策传递给 Discourse。

默认情况下，Rails 在处理 x-f-f 时只信任本地地址，因此我们在另一个可以方便控制的地方进行处理。

> [@MichaIng](#):
>
> `X-Real-IP` header 已经设置了

实际上，Rails 甚至不_查看_ `x-real-ip` header……它[查看的 header](https://github.com/rails/rails/blob/8-1-stable/actionpack/lib/action_dispatch/middleware/remote_ip.rb)是

- `forwarded`
- `client-ip`
- `x-forwarded-for`

不知为何，这个设置[一路传了下来](https://github.com/discourse/discourse/commit/21b562852885f883be43032e03c709241e8e6d4f)……

```plaintext
commit 21b562852885f883be43032e03c709241e8e6d4f (tag: v0.8.0)
Author: Robin Ward
Date: Tue Feb 5 14:16:51 2013 -0500

    Initial release of Discourse

diff --git a/config/nginx.sample.conf b/config/nginx.sample.conf
new file mode 100644
index 00000000..62fabf4a
--- /dev/null
+++ b/config/nginx.sample.conf
…
+ proxy_set_header X-Real-IP $remote_addr;

```

我们需要进一步调查，但目前的回答是“它能工作”。我想这就是我们最初变成这样的原因。

看起来可能是某个 gem 在使用它？

---

_[View the full topic](https://meta.discourse.org/t/handling-the-chain-of-trust-of-the-end-users-real-ip/406372)._
