# 将 Discourse 与 Cloudflare 结合使用： 最佳实践

**URL:** <https://www.isharkfly.com/t/discourse-cloudflare/16704>\
**Category:** Discourse\
**Tags:** discourse\
**Created:** [December 3, 2024, 1:28pm UTC](https://www.isharkfly.com/t/discourse-cloudflare/16704 "2024-12-03T13:28:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [December 3, 2024, 1:28pm UTC](https://www.isharkfly.com/t/discourse-cloudflare/16704/1 "2024-12-03T13:28:39Z")

</div>

## 为什么使用 Discourse 与 Cloudflare

将 Discourse 与 Cloudflare 一起使用可以带来以下几个好处：

- **性能** ：Cloudflare 的 CDN 可以加速全球访问您 Discourse 论坛上的常见资源，从而改善无论用户位于何处的社区成员的使用体验（[来源](https://meta.discourse.org/t/enable-a-cdn-for-your-discourse/14857/1)）。
- **安全性** ：Cloudflare 为您的 Discourse 论坛提供额外的安全层，例如 DDoS 防护（[来源](https://developers.cloudflare.com/ddos-protection/)）和 HTTPS 支持（[来源](https://developers.cloudflare.com/ssl/origin-configuration/ssl-modes/full-strict/)），如果没有使用 [Discourse 的 Lets Encrypt 配置](https://meta.discourse.org/t/set-up-https-support-with-lets-encrypt/40709)。

对于自托管用户来说，重要的是要注意，虽然 Cloudflare 可以提供这些好处，但它也增加了 Discourse 设置的复杂性。本文件旨在帮助您应对这种复杂性，并充分利用 Discourse 与 Cloudflare 的结合。

## 配置 Discourse 与 Cloudflare

- **Cloudflare 基础知识** ：在开始配置 Discourse 与 Cloudflare 时，首先确保您熟悉 Cloudflare 基础知识。[Cloudflare 基础知识](https://developers.cloudflare.com/fundamentals/) 是一个集中提供 Cloudflare 资源的地方。
- **设置** ：为了获得 Cloudflare 的 [安全性、性能和可靠性](https://developers.cloudflare.com/fundamentals/concepts/how-cloudflare-works/) 的好处，您需要在您的域名上配置 Cloudflare。[设置指南](https://developers.cloudflare.com/fundamentals/setup/)。

## 最佳实践

以下链接假设您已登录并且只有一个账户。

- **DNS** ：确保指向您 Discourse 实例的 DNS 记录被代理。[点击这里管理您的 DNS 记录](https://dash.cloudflare.com/?to=/:account/:zone/dns)。
- **SSL/TLS** 加密模式应设置为“完全（严格）”。[点击这里管理您的 SSL/TLS 设置](https://dash.cloudflare.com/?to=/:account/:zone/ssl-tls)。 ⚠ 如果未正确设置，可能会导致重定向循环。
- **缓存级别** 应设置为标准。[点击这里设置缓存级别](https://dash.cloudflare.com/?to=/:account/:zone/caching/configuration)。
- **创建页面规则** ：为 `community.example.com/session/*` 创建页面规则，将缓存级别设置为“绕过”。[点击这里创建页面规则](https://dash.cloudflare.com/?to=/:account/:zone/rules)。
- **规则设置** 应配置为规范化传入的 URL。[点击这里配置规则设置](https://dash.cloudflare.com/?to=/:account/:zone/rules/settings)。
- **网络设置** ：根据 Cloudflare 的计划类型，应配置如下。如果尚未启用，请启用 `IPv6 兼容性`、`WebSockets`、`IP 地理位置`、`网络错误日志` 和 `洋葱路由`。如果尚未禁用，请禁用 `伪 IPv4`、`响应缓冲`、`真实客户端 IP 头` 和 `gRPC`。根据您的站点政策设置 `最大上传大小`，100 MB 足够。[点击这里配置网络设置](https://dash.cloudflare.com/?to=/:account/:zone/network)。
- **WAF 设置** ：根据 Cloudflare 计划类型和安全需求配置。 如果您的 Cloudflare 账户支持托管规则，请配置托管规则以“跳过发布创建/编辑的 WAF”。操作步骤如下：
  - 添加一个托管规则，匹配 URI 路径和请求方法。该规则应如下所示：

```auto
(http.request.uri.path eq "/posts(/[0-9]+)?" and http.request.method in {"POST" "PUT"})

```

- 选择“跳过所有剩余规则”选项。
- 启用“记录匹配请求”。如果您使用 [数据探索器](https://meta.discourse.org/t/32566?silent=true) 插件，请配置托管规则以“跳过管理员查询的 WAF”。操作步骤如下：
- 添加一个托管规则，匹配 URI 路径和请求方法。该规则应如下所示：

```auto
(http.request.uri.path contains "/admin/plugins/explorer/queries/" and http.request.method eq "PUT")

```

- 选择“跳过所有剩余规则”选项。
- 启用“记录匹配请求”。[点击这里创建托管规则](https://dash.cloudflare.com/?to=/:account/:zone/firewall/managed-rules)。
- **内容优化** 应开启 Brotli，关闭 Rocket Loader™，并关闭“自动最小化”。[点击这里设置内容优化](https://dash.cloudflare.com/?to=/:account/:zone/speed/optimization)。⚠ 由于开启 Rocket Loader™，Discourse 收到过不少站点停机报告 ([来源](https://meta.discourse.org/search?q=rocket%20loader%20order%3Alatest))。

## 自托管用户的额外配置

为了确保正确的 IP 地址发送到 Discourse，您需要在 `containers/app.yml` 文件的末尾添加以下行：

```auto
cloudflare.template.yml

```

（相关：[如何设置 Cloudflare？ - #6 由 codinghorror](https://meta.discourse.org/t/how-do-you-setup-cloudflare/32258/6)）

## 支持

如需来自 Cloudflare 的直接支持，请访问原始帖子 @ [Cloudflare Community](https://community.cloudflare.com/t/using-discourse-with-cloudflare-best-practices/602890)。

或者，您可以在 Meta 上搜索特定问题：[‘cloudflare’ 搜索结果 - Discourse Meta](https://meta.discourse.org/search?q=cloudflare)
