使用outlets自定义Discourse的web服务器行为

摘要

此提交 以来,Discourse 的 默认 nginx 配置 包含所谓的“插槽”(outlets)——一种在适当位置注入额外 nginx 配置语句的受支持方式。

此更改实现了更健壮的 Web 服务器配置和行为管理方法,而我们之前依赖于相对脆弱的搜索/替换命令。

随着时间的推移,我们将调整我们自己的配置模板以使用插槽。

用法

支持三个插槽部分:

  • before-serverhttp 上下文配置语句
  • serverserver 上下文配置语句
  • discourselocation 上下文配置语句 - 这些语句应用于转发到 Discourse 的请求

示例

以下是一些如何在 app.yml 配置文件中使用这些插槽来完成目标的示例。

可以将它们添加到任何可以使用 runfile 命令的地方;我建议将其添加到 after_code 挂钩中,以便在语法出现问题时可以快速失败重建。

添加响应头

此示例将一个标头添加到转发到 Discourse 的每个响应中:

hooks:
  after_code:
    - file:
        path: /etc/nginx/conf.d/outlets/discourse/clacks-overhead.conf
        chmod: 444
        contents: |
          add_header x-clacks-overhead "GNU Terry Pratchett";

结果:

○ → curl -I https://example.contoso.com/
HTTP/2 200 
...
x-clacks-overhead: GNU Terry Pratchett

在单个路径上添加静态文件

此示例在 nginx 上将一个静态文件添加到单个路径,该路径位于 Discourse 正常目录之外。

hooks:
  after_code:
    - file:
        path: /etc/nginx/conf.d/outlets/server/well-known-important-file.conf
        chmod: 444
        contents: |
          location = /.well-known/important-file {
            default_type application/json;
            root /var/www/static/;
          }
    - file:
        path: /var/www/static/.well-known/important-file
        chmod: 444
        contents: |
          {"content-free":true}

结果:

○ → curl -i https://example.contoso.com/.well-known/important-file
HTTP/2 200 
server: nginx
date: Fri, 07 Mar 2025 23:22:38 GMT
content-type: application/json
content-length: 22
last-modified: Fri, 07 Mar 2025 23:12:08 GMT
strict-transport-security: max-age=63072000
accept-ranges: bytes

{"content-free":true}
8 个赞

我觉得我对这个真的很感兴趣,除了…我不是很懂。第二个例子是不是给了我们一种方式来提供我们选择的静态页面?

正是如此。

从管理内容的角度来看,这不是一个可扩展的解决方案,但它确实易于实现的优点。

1 个赞

谢谢 Michael,至少我明白了发生了什么。

很好,谢谢你的提示。