Nginx Proxy Guard

Guides

Publishing through Cloudflare Tunnel

By the end of this page NPG's proxy hosts are reachable from the internet through a Cloudflare Tunnel, with ports 80 and 443 closed on your router. Requests that arrive through the tunnel still pass NPG's WAF, country blocking, access lists and other security settings.

The Cloudflare connector (cloudflared) is built into NPG's nginx container (npg-proxy), so there is no separate cloudflared container to run. The connector is updated together with NPG.

Before you start

  • The domain's DNS is on Cloudflare.
  • You have an account that can use the Cloudflare Zero Trust dashboard.
  • NPG has a proxy host for each service you want to publish. (Your first proxy host)

Operating modes

ModeBehaviour
Token onlyNPG only runs the connector. Which hostnames the tunnel accepts (Public Hostnames) is managed in the Cloudflare dashboard.
ManagedNPG uses the Cloudflare API to keep the tunnel's last rule (the catch-all) pointed at NPG. Hostnames not registered as Public Hostnames still reach NPG and are routed by Host header, so a new service only needs a proxy host and a CNAME record.

1. Create a tunnel and enter the connector token

  1. In the Cloudflare Zero Trust dashboard, go to Networks → Tunnels → Create a tunnel and create a tunnel using the Cloudflared connector.
  2. After creating it, copy the token from the install command cloudflared service install <TOKEN>. Pasting the whole install command also works; NPG extracts the token.
  3. In NPG, open Settings → Cloudflare Tunnel.
  4. Paste the token into Connector Token, turn on Enable Tunnel and click Save. The token is shown masked after saving.
  5. Watch the status change to Connected.
Cloudflare Tunnel settings: enable tunnel, connector token, operating mode
Cloudflare Tunnel settings: enable tunnel, connector token, operating mode

2-A. Token only: register a Public Hostname

  1. In the Cloudflare dashboard, add a Public Hostname to the tunnel with a wildcard per domain (for example *.example.com).
  2. Set the service to the address shown in the Setup Guide on NPG's Settings → Cloudflare Tunnel screen. The default is https://localhost:443; if you changed NGINX_HTTPS_PORT, that port is shown.
  3. In the same Public Hostname's TLS settings, turn on No TLS Verify. The internal HTTPS server the tunnel connects to uses a self-signed certificate.

One wildcard is enough: NPG routes each request to the right proxy host by its Host header. Cloudflare matches Public Hostnames from the top down, so if you add specific hostnames as well, put them above the wildcard.

2-B. Managed mode: hand over the catch-all

  1. In Cloudflare, create an API token with the single permission Account / Cloudflare Tunnel / Edit. This is not the connector token from step 1.
  2. Under Operating mode on Settings → Cloudflare Tunnel, choose Managed and paste it into Cloudflare API token.
  3. Turn on Catch-all: route unmatched hostnames to NginxProxyGuard and save.
  4. Check that the status reads Catch-all active.
  5. To publish a new service, create a proxy host in NPG and add a CNAME record in Cloudflare DNS that points the hostname at the tunnel.

NPG only replaces the tunnel's last rule when it is the default http_status:404 or a rule NPG wrote itself. A catch-all managed elsewhere is left alone and reported as Conflict. Public Hostnames registered in the dashboard are kept as they are.

Certificates

SSL-enabled hosts need a certificate. In a tunnel-only setup the domain does not point at this server's port 80, so issue with DNS-01. The steps are the certificate steps in Running behind Cloudflare.

Real visitor address

The connector reaches nginx over the loopback address on the same server, and loopback is trusted by default. So with a tunnel alone you do not need Settings → Trusted Proxies: the logs and security features see the visitor's real address.

Check that it works

  • Settings → Cloudflare Tunnel shows Connected.
  • https://app.example.com opens from an outside network.
  • The client IP in Logs → Access Logs is the public address of the device you used.

Common problems

  • The status never reaches Connected. Check the lines starting with [cloudflared] in docker logs npg-proxy. They show whether the token is wrong or why the connector cannot reach Cloudflare.
  • Every host fails and the connector log says tls: unrecognized name. Check that the service address has the form https://localhost:<port> that NPG shows.
  • Redirect loops. The service address is http://localhost:80 and the host has Force HTTPS on. See the warning in step 2-A.
  • Requests arrive for subdomains you never registered. Requests taken in by the wildcard or catch-all that match no proxy host follow Direct IP Access Handling under Settings → Global Settings → Advanced. Close Connection (444) closes them without a response.
  • The catch-all status shows Conflict. The tunnel's last rule is managed somewhere else. Set that rule back to http_status:404 in the Cloudflare dashboard and NPG applies its own.