Using Cloudflare for Tunneling
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
- Install
cloudflared
on your server. You can find the installation instructions here. (https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/)
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
andwww.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.