Using Canasta with Cloudflare
This page gathers the configuration advice for running a Canasta instance behind Cloudflare. Cloudflare is one of the most common ways to put a public MediaWiki online β it terminates TLS at its edge, caches and shields the origin, and can absorb bot and DDoS traffic before it ever reaches your server. Because Cloudflare sits in front of Canasta's own TLS endpoint (on Docker Compose, the host's HAProxy edge; see Help:Networking and TLS), the two have to agree about who provisions the certificate and who handles the HTTP-to-HTTPS redirect β otherwise you get certificate failures (HTTP 525) or redirect loops.
The single most important decision is your Cloudflare SSL/TLS encryption mode, because it dictates how Canasta must be configured. Everything else on this page (locking down the origin, recovering the real visitor IP, blocking crawlers) is common to all modes.
Match the Cloudflare SSL/TLS mode to Canasta's TLS setting
In the Cloudflare dashboard, SSL/TLS β Overview sets how Cloudflare talks to your origin. Prefer Full (strict) whenever you can β it is the only option that keeps traffic encrypted end to end, from the visitor all the way to your origin. Pick one of these two supported patterns:
| Cloudflare mode | Cloudflare β origin | Canasta configuration | Origin port(s) needed |
|---|---|---|---|
| Full (strict), recommended | HTTPS (encrypted, certificate validated) | default: the edge obtains a Let's Encrypt certificate. TLS_MODE=acme-dns obtains it by DNS validation instead, and TLS_MODE=custom serves a Cloudflare Origin CA certificate |
443 (and 80 for acme)
|
| Full | HTTPS (encrypted, not validated) | as Full (strict), or TLS_MODE=internal |
443 |
| Flexible | plain HTTP (unencrypted) | TLS_MODE=off: the edge serves the instance over plain HTTP, no certificate, no redirect |
80 |
The two failure modes both come from mismatching these:
- Full / Full (strict) +
TLS_MODE=offβ Cloudflare tries HTTPS to the
origin, but the edge serves this instance over plain HTTP only, on itsHTTP_PORTβ HTTP 521 when nothing on the host listens on 443, or the edge's404(No site is served at this address.) when another instance uses 443.
- Full (strict) before the origin has a trusted certificate β the edge is still
serving its own CA's certificate, or a staging one β HTTP 526 (invalid certificate).
- Flexible + any
TLS_MODEbutoffβ Cloudflare sends HTTP, the edge answers with a
301 redirect to HTTPS, and Cloudflare follows it back as HTTP again. The result is a redirect loop:
Client β HTTPS β Cloudflare β HTTP:80 β edge β 301 HTTPS β β
Full and Full (strict) mode (recommended)
This is the most secure option and the one to use whenever you can: Cloudflare encrypts the connection to your origin, so HTTPS is preserved end to end. The origin must present a certificate, so leave Canasta at its default and the HAProxy edge provisions and renews a real Let's Encrypt certificate automatically. Because a Let's Encrypt certificate is publicly trusted, this works with both Full and Full (strict) β use Full (strict), which additionally validates the origin certificate, for the strongest protection.
For this to work:
With the default TLS_MODE=acme (HTTP validation):
- Port 80 must be reachable from the public internet so the edge can complete the
ACME HTTP-01 challenge. Cloudflare automatically exempts
/.well-known/acme-challenge/ from "Always Use HTTPS", so that redirect
setting is fine to leave on.
- Cloudflare security features must not challenge the ACME validator. If
"Under Attack Mode" (Security Level) or "Bot Fight Mode" is on, Cloudflare returns a 403 to Let's Encrypt's validator, the edge never gets a certificate, and the site returns 526. The fix is to turn those off (certificates renew ~30 days before expiry, so they must stay off, not just during issuance). See #Troubleshooting.
TLS_MODE=acme-dns avoids both constraints: the edge proves
control of the hostname through a Cloudflare API token instead of over port 80, so
Cloudflare's security features and origin firewall rules cannot block issuance or
renewal. See Help:User journeys/Cloudflare in front of the edge.
To use a Cloudflare Origin CA certificate instead of Let's Encrypt, place the
certificate and key in the instance's config/certs/ (as
origin.crt and origin.key, or the names set in
TLS_CERT and TLS_KEY) and set
TLS_MODE=custom. Only Cloudflare trusts that certificate, so the
origin then works only behind Cloudflare.
Flexible mode (use only as a last resort)
Warning: in Flexible mode the connection between Cloudflare and your origin is plain, unencrypted HTTP. Visitors see a padlock in their browser, but the traffic between Cloudflare's edge and your server β including session cookies, login credentials, and anything posted to the wiki β crosses the public network in the clear, where it can be read or tampered with by anything on the path. Flexible gives you edge TLS only; it is not end-to-end encryption, and it is easy to mistake for a secure setup because the browser padlock looks identical. Use it only when you genuinely cannot run Full mode, and only when the Cloudflare-to-origin hop is itself private (e.g. both endpoints sit inside the same cloud VPC) and the origin is locked to Cloudflare's IPs (see below). When in doubt, use Full.
In Flexible mode Cloudflare holds the only certificate. Turn off automatic HTTPS so
the HAProxy edge serves the instance over plain HTTP, with no
certificate and no redirect, and name Cloudflare as the trusted proxy so the edge keeps
Cloudflare's X-Forwarded-Proto header and applies per-client rate limits:
canasta config set -i myinstance TLS_MODE=off TRUSTED_PROXIES=cloudflare
The edge serves the instance on its HTTP_PORT. Cloudflare connects to port 80,
so leave it at 80, or have Cloudflare reach the instance's port with an Origin Rule.
You can also set it at create time in your env file (-e). Caddy then
generates a Caddyfile with http:// site addresses and listens on port
80 only. This is the same switch described in
Networking and TLS β it is not
Cloudflare-specific (any TLS-terminating proxy in front needs it).
Lock the origin to Cloudflare
In every mode, your origin is still directly reachable on its public IP unless you
restrict it. Configure your firewall or cloud security group to accept inbound
traffic on the relevant ports (80 and 443 in Full mode; with TLS_MODE=acme, Let's Encrypt must still reach port 80, so use acme-dns if you lock 80 down; 80 alone in Flexible mode) only
from Cloudflare's published IP ranges. This
prevents attackers from bypassing Cloudflare and hitting the origin directly.
Recover the real visitor IP
Behind Cloudflare, every request reaches your origin from a Cloudflare edge IP, so
without this setting the real visitor address is lost. Tell Canasta to recover it from
the CF-Connecting-IP header (which it trusts only from Cloudflare's own
IP ranges, so a direct-to-origin client cannot spoof it). On Docker Compose the
HAProxy edge reads the header and passes the address on to Caddy:
canasta config set -i myinstance TRUSTED_PROXIES=cloudflare
This is worth setting on every Cloudflare deployment, not only when CrowdSec is enabled. Once the real client IP is recovered:
- Caddy's access log records the actual visitor instead of a Cloudflare edge IP.
- The real IP is forwarded to MediaWiki, so anonymous-edit attribution, IP blocks
(Special:Block), and rate limits apply to the visitor rather than to
Cloudflare β without it, blocking one "IP" would block all Cloudflare traffic at
once, and every anonymous edit would appear to come from Cloudflare.
- CrowdSec, if enabled, bans the right addresses.
- On Docker Compose, the HAProxy edge's per-client rate limits and deny lists apply to
each visitor. Without the setting, every visitor arriving through one Cloudflare address shares one allowance, and the edge answers them all with 429 once it is spent.
See Help:CrowdSec for more detail.
Blocking bots and crawlers
Cloudflare has its own bot-management features, but you can also combine them with
Canasta's built-in defenses (CrawlerProtection, a Caddy User-Agent block, CrowdSec,
and robots.txt). See Help:User journeys/Combatting bots and scrapers.
Verifying
# At the origin, with TLS_MODE=off (Flexible): expect 200, not 301/308
curl -s -o /dev/null -w "%{http_code}\n" http://localhost/
# Through Cloudflare: expect 200
curl -s -o /dev/null -w "%{http_code}\n" https://your-domain.example.com/
In Full (strict) mode, check the certificate the edge serves from the host, bypassing Cloudflare:
curl -sI --resolve your-domain.example.com:443:127.0.0.1 https://your-domain.example.com/ | head -1
Troubleshooting
- 525 (SSL handshake failed). Cloudflare can't establish TLS to the origin.
Either you are in Full/strict mode but Caddy has no certificate (auto-HTTPS is off, or ACME is being blocked β see next item), or port 443 isn't open to Cloudflare.
- 525 with Bot Fight Mode / Under Attack Mode on. Cloudflare's security
challenge 403s Let's Encrypt's validator, so Caddy never gets a certificate. Turn both off (Security β Settings and Security β Bots) and leave them off so renewals keep working. A grey-cloud (DNS-only) bootstrap to issue the first certificate is a stopgap, but the security settings are the real fix because renewals need them too.
- Redirect loop / "too many redirects". Flexible mode with the instance still served
over HTTPS. Set TLS_MODE=off, or use Full (strict).
- IPv6 is not the problem. Cloudflare always answers AAAA for proxied
hostnames and it cannot be disabled; this is normal and not the cause of 525s.
- 526 (Invalid SSL certificate). Full (strict) and the origin's certificate is not
publicly trusted: Let's Encrypt has not issued yet (the edge serves its own CA's certificate meanwhile;canasta doctorsays so), or it is a staging certificate. Use Full untilcanasta doctorshows a production certificate.
See also
- Help:Networking and TLS β the full TLS model and
TLS_MODE(which has uses beyond Cloudflare). - Help:CrowdSec β
TRUSTED_PROXIESand behavioral blocking. - Help:User journeys/Combatting bots and scrapers β layered crawler defenses.
- Help:Troubleshooting β general TLS / reverse-proxy issues.
- Help:HAProxy edge: the Compose TLS endpoint, its certificate modes and client-address recovery.
- Help:User journeys/Cloudflare in front of the edge: Full (strict) with DNS validation, step by step.