The Cloudflare Zero Trust paradigm is reshaping our approach to security in the realm of self-hosted services. As concerns about privacy and data breaches continue to escalate for both individuals and organizations, the innovative security model of Cloudflare Zero Trust emerges as a beacon of hope. This guide is designed to walk you through the process of securely exposing your private, self-hosted services using this groundbreaking approach. We will utilize NGINX Proxy Manager and Docker, managing everything with a single Docker Compose file.

What is Cloudflare Zero Trust?

At its core, Cloudflare Zero Trust is a security framework crafted to address the complexities of modern threats. Its standout feature allows users to safely expose local services to the internet, guarding those services with the same rigor as a well-fortified fortress—impervious to unauthorized access. In a landscape where every click can open the door to vulnerabilities, this model is a game changer.

Setting Up Cloudflare Zero Trust Tunnels

Let’s roll up our sleeves and delve into the specifics of setting up Cloudflare Zero Trust Tunnels.

Step 1: Create Your Cloudflare Account and Domain

To get started, you’ll need both a Cloudflare account and a domain. Once your domain is configured on Cloudflare, navigate to SSL/TLS > Origin Server > Create Certificate. This step will generate a certificate for your origin server (the server running your applications). Make sure to store your certificate and private key securely—it’s your digital lifeline.

Step 2: Establish the Cloudflare Tunnel

Next, let’s install Cloudflared, the utility that will create and manage your tunnel. After installation, authenticate using the command:

cloudflared tunnel login

Follow the prompts to link Cloudflared with your Cloudflare account.

Now, create your tunnel with:

cloudflared tunnel create [your tunnel name]

Remember to jot down your Tunnel ID.

To route traffic from your domain to this tunnel, execute:

cloudflared tunnel route dns [your tunnel name] [your domain]

Congratulations! Your tunnel is now established.

Step 3: Setting Up NGINX Proxy Manager

NGINX Proxy Manager (NPM) simplifies application management. To set it up, we’ll use Docker and Docker Compose. Here’s a streamlined configuration:

version: '3'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    environment:
      DB_MYSQL_HOST: "db"
      DB_MYSQL_PORT: 3306
      DB_MYSQL_USER: "npm"
      DB_MYSQL_PASSWORD: "npm"
      DB_MYSQL_NAME: "npm"
    volumes:
      - './data:/data'
      - './letsencrypt:/etc/letsencrypt'
  db:
    image: 'jc21/mariadb-aria:latest'
    environment:
      MYSQL_ROOT_PASSWORD: 'npm'
      MYSQL_DATABASE: 'npm'
      MYSQL_USER: 'npm'
      MYSQL_PASSWORD: 'npm'
    volumes:
      - './data/mysql:/var/lib/mysql'

With this Docker Compose file in hand, start NPM and set up your proxy hosts.

Step 4: Connecting Everything

Now, let’s integrate all the components with another Docker Compose file:

version: '3'
services:
  cloudflared:
    image: cloudflare/cloudflared:2021.4.0
    command: tunnel --no-autoupdate --url tcp://proxymanager_app_1:8080
    networks:
      - proxy
    volumes:
      - ./certs:/etc/cloudflared
    restart: always

In this snippet, replace proxymanager_app_1 with the name of your application. The certs folder will house the origin server certificates generated in Cloudflare.

Wrapping Up

And there you have it! You’ve successfully exposed your self-hosted service through a secure tunnel, all managed seamlessly via Docker Compose. By leveraging the Cloudflare Zero Trust model, you’ve fortified your service hosting with a level of security that’s both robust and intuitive. Happy hosting!