代理不再识别真实IP

在过去几个月里(不确定具体从何时开始),我发现我根据 Add an offline page to display when Discourse is rebuilding or starting up 文档配置的外部 nginx 设置,用于传递真实 IP 地址的功能,正在被忽略。

这个外部 nginx 配置已经正常工作多年:

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Real-IP $remote_addr;

现在我看到所有流量都被归因于 RFC1918 地址空间内的内部 Docker 网络,如下所示:

这显然破坏了“对共享此 IP 地址的其他用户应用此惩罚”以及其他所有依赖真实远程 IP 地址的功能。

我猜想这个问题可能已经在某处得到解决,只是我搜索得不够仔细。:frowning: 但我非常希望能得到一些提示,告诉我可能发生了哪些变化,以便我能做出相应的调整。

我最近记得有关此事的一些内容,但我自己也找不到。Handling the "chain of trust" of the end user's real IP - #8 by supermathie 包含一个指向 Cloudflare 模板的链接,我认为这可能是一个很好的参考模型。

以下是我正在做的操作,目前看来仍然有效(截至“Discourse 2026.6.0-latest - GitHub - discourse/discourse: A platform for community discussion. Free, open, simple. · GitHub 版本 5c507d8359f4d6e8a5d98780c622d51cedbd9bd7”)

after_bundle_exec:
  - replace:
    filename: /etc/nginx/conf.d/discourse.conf
    from: "types {"
    to: |
      set_real_ip_from 192.168.1.0/24;
      set_real_ip_from 192.168.11.0/24;
      set_real_ip_from 172.16.0.0/12;
      set_real_ip_from 10.0.0.0/8;
      real_ip_recursive on;
      real_ip_header X-Forwarded-For;
      types {

可能与此提交有关:FIX: nginx sample configuration should be setting x-f-f to the end us… · discourse/discourse@b4a3389 · GitHub

这或许能帮到你:

照旧,发帖并查看回复能帮我找到其他类似的帖子。:rofl: 例如:

谢谢大家!现在去配置容器内 nginx 的 set_real_ip_from

不过我会涵盖整个 RFC1918 地址空间。供其他人参考,配置如下:

after_bundle_exec:
  - replace:
    filename: /etc/nginx/conf.d/discourse.conf
    from: "types {"
    to: |
      set_real_ip_from 192.168.0.0/16;
      set_real_ip_from 172.16.0.0/12;
      set_real_ip_from 10.0.0.0/8;
      real_ip_recursive on;
      real_ip_header X-Forwarded-For;
      types {

这对我不起作用。

我现在明白为什么从引用的提交开始它对我停止工作了;它破坏了我多年来一直使用的 set_real_ip 替换。使其更稳健的理由是合理的,这是一个更好的方案;一次破坏性变更,之后应该更少出现中断。

我不知道为什么 after_bundle_exec 替换没有生效,但我打算改为直接放入文件。

这对我有效;分享给下一个人:

run:
  - file:
     path: /etc/nginx/conf.d/outlets/server/real-ip-recursive.conf
     chmod: 644
     contents: |
       real_ip_recursive on;
  - file:
     path: /etc/nginx/conf.d/outlets/server/real-ip-header.conf
     chmod: 644
     contents: |
       real_ip_header X-Forwarded-For;
  - file:
     path: /etc/nginx/conf.d/outlets/server/set-real-ip-from.conf
     chmod: 644
     contents: |
       set_real_ip_from 192.168.0.0/16;
       set_real_ip_from 172.16.0.0/12;
       set_real_ip_from 10.0.0.0/8;