# 反向代理 X-Forwarded-For

**URL:** <https://meta.discourse.org/t/reverse-proxy-x-forwarded-for/403181>\
**Category:** Support\
**Tags:** unsupported-install, nginx\
**Created:** [2026年五月17日 04:27 UTC](https://meta.discourse.org/t/reverse-proxy-x-forwarded-for/403181 "2026-05-17T04:27:01Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)\
**Post date:** [2026年五月17日 15:28 UTC](https://meta.discourse.org/t/reverse-proxy-x-forwarded-for/403181/6 "2026-05-17T15:28:55Z")

</div>

> [@Eviepayne](#):
>
> 我最近将论坛迁移到了一个性能高得多的主机，并正在努力实现高可用性。

你使用的是双容器架构吗？

我认为没有内置的环境变量用于自定义负载均衡器 IP，因为 nginx 本身不会为 server 块读取这些变量。

此外，如果你在运行中的容器内手动编辑了 nginx 配置，下次重建时这些修改会被清除。

我认为，为了让配置永久生效并能够抵御重建，你需要在 `app.yml`（如果是双容器架构，则是 `web_only.yml`）中使用 `replace` 命令——滚动到你的 `.yml` 文件最底部的 `run:` 部分，并添加以下代码块。这会指示 Discourse 构建器在 `server {` 块打开后自动注入你的真实 IP 设置：

```plaintext
run:
  - replace:
      filename: /etc/nginx/conf.d/discourse.conf
      from: /server.+{/
      to: |
        server {
          set_real_ip_from 192.168.1.100; ## 替换为你实际的负载均衡器 IP/子网
          real_ip_header X-Forwarded-For;
          real_ip_recursive on;

```

---

_[View the full topic](https://meta.discourse.org/t/reverse-proxy-x-forwarded-for/403181)._
