1panel + docker-openresty + discourse + cloudflare 使用socket连接 显示真实用户IP

nginx使用socket代理discourse, 而不是使用端口代理, 优势在于:

  • 提高安全和性能, 少一次 TCP stack
  • discourse官方推荐
  • 不暴露无谓端口
  • UNIX socket 比本地网络通信(比如 localhost:3000)效率更高,省略网络协议栈

Discourse

如果是已部署的修改, 需要重建app /var/discourse/launcher rebuild app

app.yml

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  ## Uncomment the next line to enable the IPv6 listener
  #- "templates/web.ipv6.template.yml"
  - "templates/web.ratelimited.template.yml"
  - "templates/cloudflare.template.yml"
  - "templates/web.socketed.template.yml"
  ## Uncomment these two lines if you wish to add Lets Encrypt (https)
  #- "templates/web.ssl.template.yml"
  #- "templates/web.letsencrypt.ssl.template.yml"

## 此容器应暴露哪些 TCP/IP 端口?
## 如果您希望 Discourse 与另一个 Web 服务器(如 Apache 或 nginx)共享端口,
## 请参阅 https://meta.discourse.org/t/17247 获取详细信息
expose:
  #- "2080:80"   # http
  #- "2443:443" # https

~~~~~~

1panel

需要修改openresty的compose.yml

- /var/discourse:/var/discourse

Pasted image 20250620202924

openresty

另外建议从源文里把no-cache头给删除掉, 让discourse控制Cache-Control.

吐槽一下: 1panel的openresty添加了很多不需要的配置, 比如openresty的根缓存, 导致之前discourse app搜不到站点. 看这个: https://www.sunai.net/t/topic/300

unix:/var/discourse/shared/standalone/nginx.http.sock:

Pasted image 20250620203011

成果

不再是cloudflare的IP地址了

不过好像发现用户头像图片加载有点问题, 不太确定, 再看看

Pasted image 20250620203436