@pfaffman 在 2022.02.24 大幅修改了此内容。如果出错了,请责怪我。
如果你想在与 Discourse 相同的机器上运行其他网站,你需要在 Docker 容器前面设置一个额外的 NGINX 或 HAProxy 代理。
注意:这适用于高级管理员
本指南假设你已经让 Discourse 正常工作——如果还没有,可能很难判断配置是否有效。
如果另一个服务器正在使用端口 80 或 443,你就不能使用 ./discourse-setup 来设置 Discourse。你需要用你喜欢的文本编辑器复制并编辑 samples/standalone.yml。
在容器外部安装 nginx
首先,确保容器没有运行:
cd /var/discourse
./launcher stop app
然后安装 nginx 和 certbot:
sudo apt-get update && sudo apt-get install nginx certbot python3-certbot-nginx
更改容器定义
这是我们更改 Discourse 实际设置方式的地方。我们不希望容器监听端口——相反,我们将告诉它监听一个特殊文件。
你需要编辑 /var/discourse/containers/app.yml 来禁用 ssl 并添加模板以创建 nginx sock 文件。它应该如下所示:
# base templates used; can cut down to include less functionality per container templates:
- "templates/postgres.template.yml"
- "templates/redis.template.yml"
- "templates/web.template.yml"
- "templates/web.ratelimited.template.yml"
# - "templates/web.ssl.template.yml" # remove - https will be handled by outer nginx
# - "templates/web.letsencrypt.ssl.template.yml" # remove -- https will be handled by outer nginx
- "templates/web.socketed.template.yml" # <-- Added
确保通过在每行前面加上 # 来移除或注释掉暴露的端口。
# which ports to expose?
# expose: comment out entire section by putting a # in front of each line
# - "80:80" # http
# - "443:443" # https
现在你可以
/var/discourse/launcher rebuild app
来重建 Discourse,使其数据可供 socket 访问。
如果你使用的是无法使用 web socket 的其他反向代理,你可以选择在上面的部分中暴露一个不同的端口,例如 - 8080:80。
为外部 nginx 创建一个 NGINX ‘站点’
为 Discourse 创建一个站点文件:
cd /etc/nginx/sites-available
cp default discourse.example.com
cd ../sites-enabled
ln -s ../sites-available/discourse.example.com
接下来编辑该文件,注释掉这些行:
#listen 80 default_server;
#listen [::]:80 default_server;
并像这样编辑 server_name 和 location 块:
server_name discourse.example.com; # <-- change this
location / {
proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
如果你使用的是双容器安装,socket 行将是:
proxy_pass http://unix:/var/discourse/shared/web-only/nginx.http.sock:;
然后,在 shell 中:
certbot --nginx
并遵循说明。如果你不理解提示,那么你可能不应该这样做,但可以查阅 certbot 文档以获得帮助。
@pfaffman 认为 certbot 会为你做这些,但如果你更改了 nginx 配置,你需要
sudo service nginx reload
创建你的其他站点
Discourse 部分已完成!
像在上面最后一步那样,创建其他 NGINX “站点”,然后链接并启用它们。
提示
sudo netstat -tulpn:这将告诉你哪些端口正在被使用/var/log/nginx/error.log:这是 ubuntu 上 nginx 日志的位置。当你遇到 502 Bad Gateway 错误时,它会告诉你错误是什么。