Configurar Let's Encrypt + não-www > www

Finalmente consegui fazer funcionar e achei por bom reunir tudo, caso alguém precise.
Essas informações foram compiladas de diferentes tópicos neste fórum. Obrigado a @brahn e @techAPJ.
Isso pressupõe que você já tenha concluído a configuração inicial do Let’s Encrypt:

Depois de fazer isso, o seguinte precisa ser adicionado ao app.yml na seção hooks. Isso fará duas coisas:

  1. configurar SSL tanto para example.com quanto para www.example.com. Basta substituir example.com pelo seu nome de domínio.
  2. redirecionar example.com para www.example.com

Se alguém souber como melhorar isso, por favor, me avise.

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'; # lembrar o certificado por um ano e conectar automaticamente ao HTTPS para o s

como sempre, lembre-se de que espaçamento e indentação são extremamente importantes no arquivo app.yml. Após editar, você deve reconstruir o container com:

cd /var/discourse
./launcher rebuild app
6 curtidas

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 curtida

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

1 curtida

I need to put full context.
I have pretty fresh version 2.7.0.beta7 but my www and non-www config is incorrect. I try to fix it

I try instruction above (copy/paste) and now my non-www stops working.

1 curtida

That path is inside the container, so if you’re looking on the host os then you’re looking in the wrong place.

You might check Setting up Let’s Encrypt with Multiple Domains and see if those directions are different. You’d just add the other domain the same way.

You can also check out https://www.forcewww.com/

4 curtidas

Isso parece não funcionar mais e faz com que minha instância fique inacessível. Consegui colocá-la de volta em funcionamento usando isto em vez disso:

2 curtidas