Using Cloudflare for Tunneling

Zafir Sk Heerah
2 min readOct 15, 2024

--

Do you need to expose a local project to the web for testing or securely share a service without revealing your public IP? Cloudflare Tunnel makes it easy to expose your local server, host internal services, or test your development environment on the internet, all while ensuring privacy, security, and control.

Prerequisites

  • A domain registered
  • Access to your domain registrar for DNS changes
  • A Cloudflare account was created

Domain Setup

Add Your Domain to Cloudflare

  • Log in to your Cloudflare account.
  • Click on “Add a Site”.
  • Enter your domain name (e.g., example.com) and click "Add Site".
  • Choose a plan that fits your needs and click “Continue”. (The free version also works)

Update DNS Records

  • Cloudflare will scan and retrieve your existing DNS records. Review and update them if necessary.
  • Click “Continue” once you have verified the DNS records.

Change Your Nameservers

  • Cloudflare will provide nameservers for you to use.
  • Log in to your domain registrar.
  • Replace your current nameservers with the Cloudflare nameservers provided.
  • Save your changes.

Activating your domain

  • Once your nameserver changes propagate (which may take a few hours to 48 hours), your domain will be active on Cloudflare.

Cloudflare Tunnel Setup

Installing cloudflared

Authenticating cloudflared

  • Run the following command to authenticate cloudflared with your Cloudflare account:
cloudflared tunnel login
  • This will open a browser window asking you to log in to your Cloudflare account and authorize the request. Once you log in and are authorized, you can set up the tunnel.

Create a Tunnel

  • Create a new tunnel and give it a name:
cloudflared tunnel create mytunnel
  • Note the tunnel ID and credentials file path are provided by the command once executed.

Configure the Tunnel

  • Create a configuration file for your tunnel. The default location is ~/.cloudflared/config.yml
  • Example configuration for your domain:
tunnel: [TUNNEL_ID]
credentials-file: /path/to/credentials-file.json

ingress:
- hostname: example.com
service: http://localhost:8080
  • Replace [TUNNEL_ID] and /path/to/credentials-file.json with the values obtained when creating the tunnel.
  • Update yourdomain.com with your specific domain or subdomain.
  • Change the service URL to match the address where your application is running (e.g., http://localhost:4200).

Update DNS Records in Cloudflare

  • In the Cloudflare dashboard, navigate to “DNS”.
  • Ensure that you have A or CNAME records for your domain (example.com and www.example.com) pointing to the Cloudflare Tunnel endpoint (your-tunnel-id.cfargotunnel.com).
  • These records should be proxied (orange cloud icon) to enable Cloudflare’s features.

Run the Tunnel

  • Start the tunnel after creating the configuration file:
cloudflared tunnel run mytunnel
  • This will start the tunnel and expose your local service at your domain.

--

--

Zafir Sk Heerah
Zafir Sk Heerah

Written by Zafir Sk Heerah

Software Engineer | Consultant | Android and iOS Development | www.zfir.dev | blog.zfir.dev

No responses yet