title: nginx + cloudflare + digitalocean = 521
body: <p>I'm trying host a website with multiple subdomains (created with Cloudflare, which also provides SSL) hosted on DigitalOcean with Nginx serving as a reverse proxy.</p>
  
  <p>My Cloudflare Configs</p>
  
  <p>DNS setup:</p>
  
  <pre><code>Type ~ Name ~ Value
  A ~ api ~ MyDigitalOceanIPv4
  A ~ example.com ~ MyDigitalOceanIPv4
  A ~ www ~ MyDigitalOceanIPv4
  </code></pre>
  
  <p>Crypto setup:</p>
  
  <pre><code>SSL: Full (strict)
  Always use HTTPS: On 
  Automatic HTTPS Rewrites: On 
  </code></pre>
  
  <p>I've also used Cloudflare to Create Certificate (and followed their instructions to set it up with Nginx)</p>
  
  <p>My Nginx config:</p>
  
  <pre><code>server {
      listen 443;
      server_name example.com www.example.com;
  
      ssl on;
      ssl_certificate /srv/example.com/cloudflare.pem;
      ssl_certificate_key /srv/example.com/cloudflare.key;
  
      location / {
          proxy_pass http://localhost:8000;
      }
  }
  
  server {
      listen 443;
      server_name api.example.com;
  
      ssl on;
      ssl_certificate /srv/example.com/cloudflare.pem;
      ssl_certificate_key /srv/example.com/cloudflare.key;
  
      location / {
          proxy_pass http://localhost:8080;
      }
  }
  </code></pre>
  
  <p>I have opened for all TCP ports on DigitalOcean, and if I try to open MyDigitalOceanIPv4:8000 in my browser then my website (hosted in a Docker container) successfully loads. However, if I try to open my website "example.com" then I get Cloudflare's 521 web server is down message.</p>
  
  <p>I have also verified that the Cloudflare SSL key paths and content are correct, <code>nginx -t</code> shows no errors, and I've made sure to restart nginx after making changes.</p>
  
  <p>I have also tried to whitelist Cloudflare's IPs using my Nginx config file but it didn't work.</p>
  
  <p>If I try to <code>telnet MyDigitalOceanIPv4 443</code> or 80 then I get <code>telnet: Unable to connect to remote host: Connection refused</code>.</p>
  
  <p>Inside my DigitalOcean instance I have tried to curl <code>http://localhost:8000</code> which successfully prints my website content.</p>
  
  <p>I suspect there's some DigitalOcean setting I need to configure, or there's something wrong with my Nginx file (even though I've successfully used same Nginx config on a different cloud provider), but feel like I've tried everything..</p>