Skip to Content
HostWall

Behind Cloudflare and CDNs

Run HostWall behind Cloudflare, another CDN, a load balancer or a reverse proxy: trusted proxies, real client IPs and SSL mode Full (strict).

HostWall works well behind Cloudflare, another CDN, a load balancer or your own reverse proxy. Two things need attention: the real client IP and HTTPS between the proxy and HostWall.

Why the client IP matters

When a proxy sits in front of HostWall, every connection to HostWall comes from the proxy. Without extra configuration, HostWall would record Cloudflare's addresses instead of your visitors':

  • the Attacks page and hostwall logs would show Cloudflare IPs;
  • WAF rules that look at the client address would see the wrong one;
  • your app would receive Cloudflare's IP in X-Forwarded-For and X-Real-IP;
  • rate limits and IP rules (planned for v0.2) would hit Cloudflare instead of the attacker.

The proxy sends the visitor's IP in a header, but any client can send that header too. So HostWall only believes it when the connection comes from an address you list in server.trusted_proxies. Connections from anywhere else keep their own address, whatever headers they send. That makes it safe to trust Cloudflare even if some traffic reaches your server directly.

Cloudflare

1. Trust Cloudflare's IP ranges

In /etc/hostwall/hostwall.yaml:

server:
  trusted_proxies: [cloudflare]

HostWall reloads the file within a couple of seconds. cloudflare is a built-in list of Cloudflare's published IPv4 and IPv6 ranges (cloudflare.com/ips). It is a snapshot taken for this release; from v0.2 it will update automatically. If Cloudflare adds a range before then, add it yourself as a CIDR next to cloudflare.

HostWall then takes the client IP from X-Forwarded-For (reading from the right and skipping trusted addresses), falling back to CF-Connecting-IP. See Trusted proxies for the exact rules.

2. Get the certificate

HostWall gets its own Let's Encrypt certificate with the HTTP challenge: Let's Encrypt requests http://your-domain/.well-known/acme-challenge/... on port 80. HostWall answers that path itself, before any redirect.

This works through Cloudflare's proxy (orange cloud) as long as Cloudflare passes that request to your server over HTTP on port 80. It does by default. Things that can get in the way:

  • Always Use HTTPS, or a redirect rule that sends HTTP to HTTPS. Let's Encrypt follows the redirect to https://, Cloudflare then connects to HostWall over HTTPS, and HostWall has no certificate yet. If the certificate isn't issued, turn the redirect off until it is, or exclude /.well-known/acme-challenge/* from it. HostWall redirects HTTP to HTTPS itself, so you don't need Cloudflare to do it.
  • WAF, bot or "Under Attack" rules in Cloudflare that challenge the request. Let's Encrypt can't solve a challenge page.

The most reliable way for a new site: set the DNS record to DNS only (grey cloud), wait until the Sites page shows a valid certificate, then switch it to Proxied (orange cloud). Renewals happen 30 days before expiry and use the same path, so keep port 80 reachable through Cloudflare.

3. Set SSL/TLS mode to Full (strict)

In the Cloudflare dashboard, go to SSL/TLS → Overview and choose Full (strict). Cloudflare then connects to HostWall over HTTPS and checks that the certificate is valid, which it is, because HostWall has a real Let's Encrypt certificate.

Don't use the other modes:

ModeWhat goes wrong
FlexibleCloudflare connects over HTTP. HostWall redirects HTTP to HTTPS, Cloudflare follows it over HTTP again: an endless redirect loop ("too many redirects").
Full (not strict)Works, but Cloudflare doesn't check the certificate, so anyone between Cloudflare and your server could impersonate it. There's no reason to use it when HostWall has a valid certificate.
OffNo HTTPS for visitors.

4. Check it

Send a test request from your own computer:

curl -s -o /dev/null "https://example.com/?id=1%27%20OR%20%271%27%3D%271"

Then open Attacks in the dashboard, or run sudo hostwall logs -n 1. The client IP should be your IP, not a Cloudflare address (Cloudflare's IPv4 addresses mostly start with 104., 172.64–172.71, 162.158, 141.101, 108.162, 173.245 and so on).

Optional: only accept traffic from Cloudflare

If every visitor should come through Cloudflare, you can block direct access to ports 80 and 443 except from Cloudflare's ranges, so attackers can't skip Cloudflare by finding your server's IP. Do this with your firewall or your cloud provider's firewall. Keep in mind that Let's Encrypt must still reach port 80 through Cloudflare.

Cloudflare Tunnel

With cloudflared, the tunnel connects to HostWall from the same machine (or your private network), so:

  • trust the tunnel's address: trusted_proxies: [private] (covers 127.0.0.1 and private networks);
  • point the tunnel at HostWall's HTTP port (http://localhost:80) and keep the original hostname, since HostWall routes by hostname;
  • set the site's tls: "off". Cloudflare provides HTTPS for visitors, and HostWall won't redirect to HTTPS or try to get a certificate.

This setup hasn't been tested end to end yet. Reports in Discussions are welcome.

Other CDNs and load balancers

List the proxy's addresses as CIDRs or single IPs. Your CDN publishes its ranges; your load balancer has a fixed address or subnet.

server:
  trusted_proxies:
    - 203.0.113.0/24      # your CDN's published ranges
    - 10.0.0.0/8          # or: private, for a load balancer on your private network

The proxy must send the client IP in X-Forwarded-For, True-Client-IP or X-Real-IP. Most do by default.

Only list addresses that you or your provider control. A trusted address can claim any client IP.

HostWall behind your own reverse proxy

If nginx, Caddy, Traefik or HAProxy must stay in front (for example because it already handles TLS for other services), put HostWall behind it:

  1. Make HostWall listen on a local port and skip HTTPS, for example:

    server:
      http: "127.0.0.1:8080"
      https: "off"
      trusted_proxies: [private]

    With https: "off", every site is served over plain HTTP and nothing is redirected. You can also keep HTTPS on and set tls: "off" on individual sites.

  2. Have the front proxy pass the original Host header (HostWall routes by hostname) and the client IP. For nginx:

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
  3. Point HostWall's sites at your app as usual.

Known limitation: HostWall sets X-Forwarded-Proto from its own connection. Behind a TLS-terminating proxy, your app receives X-Forwarded-Proto: http even though the visitor used HTTPS. Apps that build absolute URLs from that header may generate http:// links. Putting HostWall in front, with its own HTTPS, avoids this.

Troubleshooting

  • Every event shows the same IP. That IP is your proxy. Add it (or its range) to trusted_proxies. See Troubleshooting.
  • "Too many redirects" through Cloudflare. Set SSL/TLS mode to Full (strict).
  • Cloudflare error 526 (invalid SSL certificate). HostWall has no valid certificate yet. See Certificate not issued and step 2 above.
  • Cloudflare error 521 or 522. Cloudflare can't reach your server. Check that HostWall is running and ports 80 and 443 are open in every firewall.
  • "Site not found" through the proxy. The proxy isn't sending the original Host header, or the hostname isn't configured as a site domain or alias.
Edit this page on GitHub

On this page