title: Nginx rewrite http to https and proxy to another port, ERR_TOO_MANY_REDIRECTS error
body: <p>Trying to use Nginx as a reverse proxy here. This is what I want to achieve:</p>
  
  <ul>
  <li>Redirect <code>example.com</code> and <code>www.example.com</code> to <code>https://example.com</code>. </li>
  <li>Proxy the request to another port.</li>
  </ul>
  
  <p>This is the flow: <code>example.com</code> -> <code>Nginx</code> -> <code>Go web server listening on port 5000</code></p>
  
  <p>It seems that the rewriting is working properly, cause in the browser I get <code>https://example.com</code>, however I am getting this error in the browser: </p>
  
  <blockquote>
    <p>ERR_TOO_MANY_REDIRECTS</p>
  </blockquote>
  
  <p>If it matters, my DNS settings are as such:</p>
  
  <pre><code>@   - A - 11.XX.XX.XX
  www - A - 11.XX.XX.XX
  </code></pre>
  
  <p>Here is my <code>/etc/nginx/nginx.conf</code> file:</p>
  
  <pre><code>events {
      worker_connections 1024;
  }
  
  http {
      server {
          listen      80;
          listen      [::]:80;
          server_name www.example.com example.com;
  
          rewrite ^(.*)$ https://example.com$request_uri permanent;
  
          location / {
                  proxy_pass http://127.0.0.1:5000;
          }
      }
  
      server {
          listen      443 ssl;
          listen      [::]:443 ssl;
          server_name www.example.com example.com;
  
          ssl_certificate "/etc/letsencrypt/live/example.com/fullchain.pem";
          ssl_certificate_key "/etc/letsencrypt/live/example.com/privkey.pem";
  
          rewrite ^(.*)$ https://example.com$request_uri permanent;
  
          location / {
              proxy_pass http://127.0.0.1:5000;
          }
      }
  }
  </code></pre>
  
  <p>Any help would be appreciated. Networking noob here.</p>