Cloudflare Tunnel

HTTPS with a Cloudflare Tunnel. No open ports.

A Cloudflare Tunnel connects the server to Cloudflare from the inside. Cloudflare serves HTTPS. The server opens no ports. Use it when ports 80 and 443 are not free, or when the server has no public IP address.

A tunnel needs a domain in your Cloudflare account. With --no-domain or --ip, the script does not offer the tunnel.

Limits

  • Cloudflare stops a request after 100 seconds. A webhook that waits longer for its run gets an error.
  • Cloudflare stops uploads over 100 MB (on the Free and Pro plans).
  • Live event streams work through the tunnel.

Make the tunnel

  1. Open the Cloudflare dashboard.
  2. Open Zero Trust > Networks > Tunnels.
  3. Click Create a tunnel. Select the type Cloudflared.
  4. Give the tunnel a name, for example zerodot.
  5. Cloudflare shows an install command. Copy the token: the long text after --token.

Keep the token secret. Anyone with the token can run the tunnel.

Automatic

sudo ZERODOT_TUNNEL_TOKEN='<token>' bash install.sh zerodot.example.com --proxy cloudflared

Without ZERODOT_TUNNEL_TOKEN, the script asks for the token. It checks that the text is a tunnel token.

What the script does depends on the mode:

Case What the script does Service for the public hostname
Docker mode Adds a cloudflared container to the install. Saves the token in .env (ZERODOT_TUNNEL_TOKEN). http://proxy:3000
Native mode Installs the cloudflared package and service (cloudflared service install <token>). http://127.0.0.1:<web port>
cloudflared runs on the server already Installs nothing. Does not ask for a token. http://127.0.0.1:<port>

At the end, the script prints the hostname and the service. Then do the last step in Cloudflare:

  1. Open Zero Trust > Networks > Tunnels.
  2. Open your tunnel. Open Public hostname. Click Add a public hostname.
  3. Set Subdomain and Domain, for example zerodot and example.com.
  4. Set Service to the value that the script printed. Example: type HTTP, URL proxy:3000.
  5. Save. Cloudflare makes the DNS record.

The address works after this step.

In native mode, the tunnel has one upstream: the web port. The web app sends the API paths to the API app itself (ZERODOT_API_PORT on the web service). Thus the tunnel needs no path rules.

Manual

Docker mode

  1. Add the token to .env in the install folder:

    echo "ZERODOT_TUNNEL_TOKEN=<token>" | sudo tee -a /var/www/zerodot/.env
    
  2. Add this service to docker-compose.override.yml:

    services:
      cloudflared:
        image: cloudflare/cloudflared:latest
        command: [tunnel, --no-autoupdate, run]
        environment:
          TUNNEL_TOKEN: ${ZERODOT_TUNNEL_TOKEN:?set ZERODOT_TUNNEL_TOKEN}
        restart: unless-stopped
        depends_on: [proxy]
    
  3. Start it:

    cd /var/www/zerodot
    docker compose up -d
    
  4. In Cloudflare, add the public hostname with the service http://proxy:3000.

The cloudflared container is in the Docker network of the install. It reaches the Caddy container of the install by the name proxy.

Native mode

  1. Install cloudflared.

    Debian and Ubuntu:

    sudo install -d -m 0755 /usr/share/keyrings
    curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
    echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
    sudo apt update
    sudo apt install -y cloudflared
    

    Fedora, RHEL, Rocky and Alma:

    sudo curl -fsSL https://pkg.cloudflare.com/cloudflared.repo -o /etc/yum.repos.d/cloudflared.repo
    sudo dnf install -y cloudflared
    

    Arch:

    sudo pacman -S --needed cloudflared
    
  2. Install the service with the token:

    sudo cloudflared service install <token>
    
  3. In Cloudflare, add the public hostname with the service http://127.0.0.1:3000. Use the web port of your install (WEB_PORT in .zerodot-install).

cloudflared runs on the server already

Do not install a second cloudflared. Add a public hostname to the tunnel that exists:

  • Docker mode: service http://127.0.0.1:3000 (the port in docker-compose.override.yml).
  • Native mode: service http://127.0.0.1:<web port>.

If that cloudflared runs in a container, 127.0.0.1 is the container, not the server. Connect the container to the network of the install and use http://proxy:3000. See A proxy in Docker.

Firewall

A tunnel needs no open port. The script does not open ports 80 and 443 with a tunnel. You can keep them closed.

Change this page on GitHub