Let's Encrypt のセットアップと non-www から www へのリダイレクト

ついに動作させることができましたので、必要とする方のためにまとめてみました。
この情報は、このフォーラムのさまざまなトピックから収集したものです。@brahn@techAPJ さんに感謝します。
これは、Let’s Encrypt の初期セットアップが完了していることを前提としています。

セットアップ後、以下の内容を app.yml の hooks セクションに追加する必要があります。これにより、以下の 2 つの処理が行われます。

  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'; # 証明書 を 1 年間記憶し、自動的に 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

そのパスはコンテナ内部にあるため、ホスト OS で探している場合は、間違った場所を探しています。

複数のドメインでの Let’s Encrypt の設定を確認し、手順が異なるかどうかを確認してください。他のドメインも同じ方法で追加するだけです。

また、https://www.forcewww.com/ もご覧ください。

「いいね!」 4

これはもはや機能せず、私のインスタンスが到達不能になる原因となっています。代わりにこれを使用して、正常な状態に戻しました。

「いいね!」 2