Introduction
In an increasingly connected world, exposing local applications to the web can be essential for development, testing, and collaboration. Whether you’re running a web server, a database, or a development environment on your local machine, sharing access to these services can help streamline workflows and facilitate real-time feedback. Cloudflare Tunnel (previously known as Argo Tunnel) provides a secure way to expose your local applications to the internet without compromising your server's security.
This blog will guide you through the detailed steps of setting up Cloudflare Tunnel to expose your local address to the web. We will cover two options: using the Cloudflare CLI and setting up through the Cloudflare Dashboard with Zero Trust. By the end, you’ll have everything you need to get started securely.
Prerequisites
Before you begin, make sure you have the following:
- Cloudflare Account: If you don’t have an account, you can create one for free at Cloudflare's website.
- Domain Name: You will need a domain name registered through Cloudflare. If you don’t have one, you can easily register a new domain.
- Local Application: Ensure you have a local application running (e.g., a web server on port 3000).
- Terminal Access: Access to a terminal on your local machine (Linux, macOS, or Windows with WSL).
Option 1: Using the Cloudflare CLI
Step 1: Install Cloudflare Tunnel
Install the Cloudflare Tunnel Client: The client, cloudflared, is required to create and manage tunnels. You can install it via different methods depending on your operating system.
For macOS (using Homebrew):
> brew install cloudflare/tap/cloudflared
For Linux:
- Download the latest version of cloudflared:
> wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64
- Make it executable and move it to your /usr/local/bin directory:
> chmod +x cloudflared-linux-amd64 > sudo mv cloudflared-linux-amd64 /usr/local/bin/cloudflared
For Windows:
- Download the executable from Cloudflare's GitHub releases. Place the cloudflared.exe in a directory included in your system’s PATH.
- Verify Installation: After installation, check that cloudflared is correctly installed by running:
> cloudflared --version
Step 2: Authenticate cloudflared with Your Cloudflare Account
- Run Authentication Command:
> cloudflared login
- This command will open a web browser and prompt you to log into your Cloudflare account. Once logged in, you will be able to select the domain you want to use with Cloudflare Tunnel.
Step 3: Create a Cloudflare Tunnel
- Create the Tunnel: Use the following command to create a new tunnel. Replace my-tunnel with your desired tunnel name.
> cloudflared tunnel create my-tunnel
- This command generates a tunnel ID and updates your config.yml file with the tunnel information.
- Edit the Configuration File: Open the configuration file located at ~/.cloudflared/config.yml in a text editor. The file should look something like this:
> credentials-file: /path/to/.json > ingress: - hostname: yoursubdomain.yourdomain.com service: http://localhost:3000 - service: http_status:404
- Replace yourdomain.com with your actual domain and set the service to match the port of your local application (3000 in this example).
Step 4: Run the Tunnel
- Start the Tunnel: With the configuration in place, you can now start the tunnel using the command:
> cloudflared tunnel run my-tunnel
- If everything is configured correctly, you should see output indicating that the tunnel is running and accessible via the specified hostname.
Step 5: Update DNS Settings in Cloudflare
- Access Cloudflare Dashboard: Log in to your Cloudflare account and navigate to the DNS settings for your domain.
- Create a CNAME Record: To direct traffic to your tunnel, create a new DNS record. Set the type to CNAME, the name to your desired subdomain (e.g., subdomain), and the target to your-tunnel-id.cfargotunnel.com.
- Enable Proxying: Make sure the proxy status is set to “Proxied” (indicated by an orange cloud icon) to ensure traffic is routed through Cloudflare.
Step 6: Test Your Tunnel
- Open Your Web Browser: In your browser, navigate to the hostname you configured (e.g., https://yoursubdomain.yourdomain.com).
- Access Your Local Application: If everything is set up correctly, you should see your local application accessible over the web.
Option 2: Using the Cloudflare Dashboard with Zero Trust Setup
Step 1: Access the Cloudflare Dashboard
- Log in to your Cloudflare account.
- Navigate to the Zero Trust dashboard.
Step 2. Create a Tunnel
- In the Zero Trust dashboard, go to Access > Tunnels.
- Click on Create a Tunnel.
- Provide a name for your tunnel and click Save.
Step 3. Configure the Tunnel
- After creating the tunnel, you’ll see instructions to run the cloudflared command to configure it.
- Click on your tunnel to edit it.
- Under the Settings tab, make sure the hostname you defined matches the hostname you want to use for your local service.
Step 4: Install and Configure cloudflared
- Install cloudflared: If you haven’t already installed cloudflared, follow the installation steps outlined in Option 1.
- Authenticate cloudflared:
> cloudflared login
- Create and Run the Tunnel: Use the following command to create and run the tunnel:
> cloudflared tunnel --url http://localhost:3000
- This command allows cloudflared to expose your local application to the internet securely.
Step 5: Update DNS Settings
- Create a CNAME Record: Go to the DNS settings for your domain and create a new CNAME record pointing to your-tunnel-id.cfargotunnel.com.
- Enable Proxying: Ensure the proxy status is set to “Proxied.”
Step 6: Test Your Tunnel
- Open Your Web Browser: Navigate to the hostname you configured (e.g., https://yoursubdomain.yourdomain.com).
- Access Your Local Application: You should see your local application accessible over the web.
Conclusion
You have successfully exposed your local application to the web using Cloudflare Tunnel, either through the Cloudflare CLI or the Cloudflare Dashboard with Zero Trust setup. Both methods offer a secure way to share local services without exposing your server directly to the internet.
With this setup, you can easily share your local applications with colleagues, clients, or testers while keeping your network secure. Cloudflare Tunnel offers flexibility and scalability, making it suitable for various use cases, whether you’re developing a web application, API, or other services.
As you explore more advanced features, consider leveraging additional Cloudflare services for performance optimizations, security enhancements, and traffic management. The world of cloud-based application hosting is vast, and Cloudflare provides many tools to help you navigate it effectively.