设置 Let's Encrypt + 非-www  www

我终于让它运行起来了,并想把所有步骤整合在一起,以防有人需要。
此信息汇总自本论坛的不同主题。感谢 @brahn@techAPJ
本指南假设您已完成 Let’s Encrypt 的初始设置:

完成初始设置后,需要在 app.yml 的 hooks 部分添加以下内容。它将实现两个功能:

  1. example.comwww.example.com 都配置 SSL。只需将 example.com 替换为您的域名。
  2. example.com 重定向到 www.example.com

如果您知道如何改进,请告诉我。

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git

  after_web_config:
    - replace:
        filename: /etc/nginx/nginx.conf
        from: /sendfile.+on;/
        to: |
          server_names_hash_bucket_size 64;
          sendfile on;
    - file:
        path: /etc/nginx/conf.d/discourse_redirect_1.conf
        contents: |
          server {
            listen 443;
            server_name example.com;
            return 301 $scheme://www.example.com$request_uri;
          }
  after_ssl:
    - replace:
        filename: "/etc/runit/1.d/letsencrypt"
        from: /-k 4096 -w \\/var\\/www\\/discourse\\/public/
        to: |
          -d www.example.com -d example.com -k 4096 -w /var/www/discourse/public

    - replace:
        filename: "/etc/runit/1.d/letsencrypt"
        from: /-k 4096 --force -w \\/var\\/www\\/discourse\\/public/
        to: |
          -d www.example.com -d example.com -k 4096 --force -w /var/www/discourse/public

    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: /return 301 https.+/
        to: |
          return 301 https://$host$request_uri;

    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: /gzip on;[^\}]+\}/m
        to: |
          gzip on;
          add_header Strict-Transport-Security 'max-age=31536000'; # 记住证书一年,并自动通过 HTTPS 连接

如往常一样,请记住 app.yml 文件中的空格和缩进非常重要。编辑完成后,您需要使用以下命令重建容器:

cd /var/discourse
./launcher rebuild app
6 个赞

I wonder if this part is needed, since we already have the “force https” option in Admin settings.
@brahn, this is your part of the code, can you explain why is this necessary?

- replace:
    filename: "/etc/nginx/conf.d/discourse.conf"
    from: /return 301 https.+/
    to: |
      return 301 https://$host$request_uri;
1 个赞

I’m not really sure, does that setting redirect or just block non-https requests?

1 个赞

我需要提供完整的上下文。
我使用的是相当新的版本 2.7.0.beta7,但我的 www 和非 www 配置不正确。我尝试修复它。

我尝试了上述指令(复制/粘贴),现在我的非 www 配置无法工作了。

1 个赞

该路径位于容器内部,因此如果你在主机操作系统上查找,那么找错地方了。

你可以查看 在多个域名上设置 Let’s Encrypt,看看其中的说明是否有不同。你只需以相同的方式添加其他域名即可。

你也可以访问 https://www.forcewww.com/。

4 个赞

这似乎不再起作用,并导致我的实例无法访问。使用以下方法将其恢复到正常工作状态:

2 个赞