Build Your Own Ngrok Alternative Using Cloudflare Tunnels and NGINX

Securely exposing your local development server to the internet is a common need, especially for webhooks, remote testing, or temporary demos. Tools like Ngrok are popular but come with limitations on free tiers and may not suit privacy-conscious developers.

In this guide, we’ll show you how to build your own Ngrok-like tunneling system using Cloudflare Tunnels (Argo Tunnel) and NGINX — completely free, fast, and production-ready.

🧰 Requirements

  • A domain name managed in Cloudflare DNS
  • Ubuntu/Linux server (for tunneling agent)
  • Local app running on localhost:3000 (example)
  • NGINX installed
  • Cloudflare Tunnel client (cloudflared)

Step 1: Add Your Domain to Cloudflare

  1. Sign in to Cloudflare
  2. Add your domain.
  3. Set your nameservers to those provided by Cloudflare.
  4. Wait for propagation to complete.

Step 2: Install cloudflared

    

wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb sudo dpkg -i cloudflared-linux-amd64.deb


Step 3: Create a Tunnel

cloudflared tunnel login

  • This will open a browser and ask for Cloudflare login.
  • Choose the domain you want to use.

Then:

cloudflared tunnel create my-tunnel

This generates credentials at ~/.cloudflared​ and creates a tunnel ID.

Your Dynamic Snippet will be displayed here... This message is displayed because you did not provided both a filter and a template to use.


Step 4: Configure the Tunnel Routing

Create a config file:

sudo nano /etc/cloudflared/config.yml

tunnel: <TUNNEL_ID>
credentials-file: /root/.cloudflared/<TUNNEL_ID>.json
ingress:
  - hostname: test.yourdomain.com
    service: http://localhost:3000
  - service: http_status:404

Replace <TUNNEL_ID> and domain accordingly.

Step 5: Run the Tunnel

cloudflared tunnel run my-tunnel

Test your URL: https://test.yourdomain.com

Optional: Run as a Service

Create service file:

        

sudo nano /etc/systemd/system/cloudflared.service [Unit] Description=Cloudflare Tunnel After=network.target [Service] TimeoutStartSec=0 Type=simple Restart=always ExecStart=/usr/local/bin/cloudflared tunnel run my-tunnel [Install] WantedBy=multi-user.target

 sudo systemctl daemon-reexec

sudo systemctl enable --now cloudflared

Step 6: Use NGINX for Local Reverse Proxy (Optional)

If your app needs specific routes or SSL:

       

server { listen 3000; location / { proxy_pass http://localhost:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }

Restart nginx: sudo systemctl restart nginx

Use Cases

  • Webhooks from Stripe, GitHub, etc.
  • Remote demos for clients
  • Local API testing from the cloud

📂 Project Structure Summary

/var/www/your-app
 └── NGINX → localhost:5000
 cloudflared → tunnel to test.yourdomain.com

Hope you find this helpful!!


Build Your Own Ngrok Alternative Using Cloudflare Tunnels and NGINX
Ram Krishna April 5, 2025
Share this post
Sign in to leave a comment
Secure Software Development Lifecycle (SSDLC)