nginx + cloudflare + digitalocean = 521

Viewed 1756

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.

My Cloudflare Configs

DNS setup:

Type ~ Name ~ Value
A ~ api ~ MyDigitalOceanIPv4
A ~ example.com ~ MyDigitalOceanIPv4
A ~ www ~ MyDigitalOceanIPv4

Crypto setup:

SSL: Full (strict)
Always use HTTPS: On 
Automatic HTTPS Rewrites: On 

I've also used Cloudflare to Create Certificate (and followed their instructions to set it up with Nginx)

My Nginx config:

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;
    }
}

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.

I have also verified that the Cloudflare SSL key paths and content are correct, nginx -t shows no errors, and I've made sure to restart nginx after making changes.

I have also tried to whitelist Cloudflare's IPs using my Nginx config file but it didn't work.

If I try to telnet MyDigitalOceanIPv4 443 or 80 then I get telnet: Unable to connect to remote host: Connection refused.

Inside my DigitalOcean instance I have tried to curl http://localhost:8000 which successfully prints my website content.

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..

0 Answers
Related