Cloudflare Error 521 (Web server is down) on an AWS origin: ALB listeners, SSL mode and blocked ranges
Fixing Cloudflare error 521 with an AWS origin: a missing 443 listener on the ALB, an SSL/TLS mode that does not match the origin, a process not listening on the right interface, and blocks on Cloudflare IP ranges.
127.0.0.1, or a firewall rejecting some of Cloudflare's addresses.
If the origin never answers and nothing refuses the connection, you are looking at error 522. If the connection works but the reply is empty or malformed, go to error 520.
Symptoms
Visitors see the Cloudflare page Error 521: Web server is down, and they see it straight away rather than after a long wait. Nothing from Cloudflare shows up in the application logs:
curl -sI https://app.example.com/ | head -4
# HTTP/2 521
# server: cloudflare
# cf-ray: 8a1b2c3d4e5f6a7b-LHR
# Straight to the origin (ALB or EC2), bypassing Cloudflare
curl -svk --connect-timeout 10 \
--resolve app.example.com:443:$(dig +short my-alb-123456.eu-west-1.elb.amazonaws.com | head -1) \
https://app.example.com/ 2>&1 | head -15
# "Connection refused" on port 443 confirms a 521
One quick tell: an ALB with no healthy targets returns a 503 of its own. If the response carries server: awselb/2.0, this is not a 521, and the ALB health check runbook is the one you need.
Cause
Cloudflare names two common causes: an origin application that is offline, and Cloudflare requests being blocked. On AWS they take these shapes:
- No 443 listener on the ALB while the zone is on Full or Full (strict). In those modes Cloudflare connects to the origin over HTTPS on port 443. An ALB with only an HTTP listener on port 80 rejects it.
- Flexible mode with an origin that listens only on 443 (or the reverse). Flexible connects over HTTP on port 80, so a closed port 80 produces a 521.
- The application is down, or bound to the wrong interface. The process crashed, a restart failed, or the server listens only on
127.0.0.1instead of0.0.0.0. This applies to EC2 instances serving traffic directly, with no ALB in front. - A certificate that does not fit the SSL mode. Full modes need an origin that speaks HTTPS, and Full (strict) needs a valid certificate (ACM or a Cloudflare Origin CA certificate).
- A block on Cloudflare’s IP ranges by fail2ban, ModSecurity or a host WAF. It is often partial, which is why the error looks intermittent.
Fix
Step 1: Find out which SSL/TLS mode the zone uses
The mode decides which port and protocol Cloudflare uses to reach the origin. Check it in the dashboard (SSL/TLS, Overview) or through the API:
curl -s -H "Authorization: Bearer $CF_API_TOKEN" \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/settings/ssl" \
| python3 -c "import json,sys; print(json.load(sys.stdin)['result']['value'])"
# off | flexible | full | strict
| Mode | Cloudflare to origin | Port the origin must serve |
|---|---|---|
| Flexible | HTTP | 80 |
| Full | HTTPS (any certificate) | 443 |
| Full (strict) | HTTPS (valid or Origin CA certificate) | 443 |
Step 2: Check the ALB has a listener on the right port
ALB_ARN=$(aws elbv2 describe-load-balancers --names my-alb \
--query 'LoadBalancers[0].LoadBalancerArn' --output text)
aws elbv2 describe-listeners --load-balancer-arn "$ALB_ARN" \
--query 'Listeners[].[Port,Protocol,DefaultActions[0].Type]' --output table
# With Full or Full (strict) there must be a row: 443 | HTTPS
If the 443 listener is missing, add it with a certificate. An ACM certificate works with Full (strict) as long as it covers the hostname Cloudflare uses. You can also import a Cloudflare Origin CA certificate into ACM, but remember that only Cloudflare trusts it, so the origin must be reachable through the proxy only.
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.app.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
certificate_arn = aws_acm_certificate.app.arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.app.arn
}
}
Step 3: Confirm the application is running and listening on the right interface
For EC2 instances that take traffic directly:
sudo systemctl status nginx # or apache2, php-fpm, your app
sudo ss -tlnp | grep -E ':(80|443)\b'
# Look for 0.0.0.0:443 or [::]:443. A 127.0.0.1:443 entry means local-only.
sudo tail -n 50 /var/log/nginx/error.log
If the process dies on restarts or deploys, that is the real cause. Run it under a systemd unit with Restart=on-failure and alarm on instance status.
Step 4: Rule out a block on Cloudflare’s ranges
Check the host firewall, ModSecurity and fail2ban the same way as in the error 522 runbook (Steps 3 and 5). The difference here is that a 521 block is active: the server answers with a refusal rather than silence. An error that hits only some visitors, or only now and then, very often means just part of Cloudflare’s address space is blocked.
# Banned addresses (fail2ban 0.11+). Compare them with api.cloudflare.com/client/v4/ips
sudo fail2ban-client banned
Step 5: Watch for a redirect loop after changing the mode
There is one more trap tied to the SSL mode. If the ALB redirects all HTTP to HTTPS while Cloudflare runs in Flexible mode, Cloudflare connects over HTTP and gets a redirect every time, which the browser reports as ERR_TOO_MANY_REDIRECTS. The fix is Full (strict) with a 443 listener on the ALB. The 80-to-443 redirect can stay for clients that reach the ALB without going through Cloudflare.
Validation
# 1. The origin answers on the right port and protocol
curl -sk -o /dev/null -w "%{http_code}\n" \
--resolve app.example.com:443:$(dig +short my-alb-123456.eu-west-1.elb.amazonaws.com | head -1) \
https://app.example.com/
# Expected: 200 (or a 301/302 from the app), not a connection error
# 2. The site works through Cloudflare
curl -sI https://app.example.com/ | head -3
# Expected: HTTP/2 200, server: cloudflare
# 3. Strict mode passes certificate validation
curl -sv --resolve app.example.com:443:ORIGIN_IP https://app.example.com/ 2>&1 | grep -E "SSL certificate verify|subject:"
Once it is fixed, set up Cloudflare’s origin error notifications or a CloudWatch alarm on HTTPCode_ELB_5XX_Count, so the next outage is not first reported by your users.
Frequently asked questions about Cloudflare error 521
What does Cloudflare error 521 mean?
Why did I get a 521 right after switching the SSL mode to Full?
Is a 521 the same as a 503 from my ALB?
server: awselb/2.0 comes from the ALB itself, which did accept the connection but has no healthy targets. The first is a network and listener problem, the second a health check and application problem.
Related
- Cloudflare error 522 on AWS: no response, security groups, NACLs and DNS
- Cloudflare error 520 on AWS: empty or malformed responses from the origin
- AWS ALB 502: target health check failures
- Cloudflare documentation: error 521
Cloudflare in front of AWS and the 52x errors will not go away?
Book a free 30-minute call. We set up SSL modes, ALB listeners, certificates and traffic filtering so that Cloudflare and AWS work together without surprises.