Cloudflare tunnel
Exposing your local server on the Internet (clearnet) has various solutions, but the Cloudflare Tunnel stands out as the easiest and most cost-effective option. Traditionally, configuring Firewalls, using Tor, or setting up an SSH reverse tunnel to a public VPS were common approaches, each with its challenges and costs. Cloudflare Tunnel offers an alternative, though it acts as a middleman and can access or modify your traffic.
Cost: Paid service
Difficulty: Medium
With Cloudflare Tunnel, you gain low latency access to your server on clearnet, without the need for complex firewall or router configurations, dynamic DNS, or relying on an internet service provider.
Cloudflare Tunnel ensures secure connectivity without exposing your server's publicly routable IP address. Instead, a lightweight daemon, cloudflared, creates outbound-only connections to Cloudflare's global network. This establishes persistent tunnels that route traffic to DNS records. You can run multiple cloudflared processes within a tunnel, connecting your resources securely to Cloudflare's nearest data center.
Requirements
Before you start, make sure you:
Buy a domain name
Buy a domain or use an existing one, there are different options to buy a domain, to this example, we will use Namecheap
Go to the Namecheap, search your wish domain between available, and follow the registration and buying process (you can pay using Bitcoin onchain), the price depends on the domain extensions chosen, a common extension like .com or .net generally has an annual cost between 10€ and 20€, but some less common extensions may have higher prices. In general, the most common extensions like .com, .net, and .org usually have low costs due to their popularity and availability. However, other less common extensions, such as .xyz or .online, are often offered at lower prices to attract more users.
Create an account on Cloudflare
Create an account on Cloudflare and add the recently created domain to it:
In the top navigation bar, click [Add site]
Enter your domain (
example.com
) and then click on the [Add site] button againSelect your plan level. The free plan is enough for this case of use. For more details on features and pricing of available plans, refer to the Plans page. Click Continue
Click Done, take note of the nameservers assigned to your account
On Overview, locate the nameserver names in 2
Change the domain nameservers to Cloudflare
Before your domain can begin using Cloudflare for DNS resolution, all requests should be redirected to Cloudflare’s network first, where Access policies can be applied. You need to add these nameservers to your registrar (Namecheap in our case)
Access your Namecheap account or the registrar selected, from the left sidebar, select Dashboard, and click on the Manage button next to your domain
Staying in the Domain tab, go to the Nameservers section, select CustomDNS, type the nameservers provided for Cloudflare before, and click on the green checkmark to save the changes
Make sure DNSSEC is disabled at this point
Select the Advanced DNS tab and find the DNSSEC section
Toggle the button to the left if are to the right
Once all of this is done, you need to wait for the registrar to update the nameservers. This process can take up to 24 hours, but it is usually much more immediate. In any case, Cloudflare will send an email when the process is finished
Check DNS records
From the left sidebar, select Websites, click on your site added, and again from the new left sidebar click on DNS -> Records
You can manually add a new record by clicking the "Add record" button. More later we will use this. Right now you will not have any record
Keep this Cloudflare session open, we will add and modify some registries to configure the tunnel
Installation
With user
admin
, go to the temporary folder
Set a temporary version environment variable to the installation
Download Cloudflare Tunnel Client (Cloudflared)
Use the deb package manager to install it
Check the correct installation
Example of expected output:
Remove the package installation file
If you come to update this is the final step
Authenticate on Cloudflare and authorize
With user
admin
, authenticate Cloudflared with your Cloudflare account
Example of expected output:
Leave Cloudflared running to download the cert automatically while you complete the authentication on the Cloudflare account
In the same browser session with the Cloudflare account already open, enter the URL of the prompt provided earlier. Then, select your pre-configured site on Cloudflare to authorize the tunnel for that site
Authorize its connection
Go back to the MiniBolt terminal session, and you will see this:
Expected output:
Create a tunnel and give it a name
Suggestion: <NAME> = miniboltunnel
Example of expected output:
Take note of the tunnel ID -><UUID>: e.g: 8666c35d-6ac3-4b39-9324-12ae32ce64a8
you will need it later
Ensure that the tunnel has been created
Example of expected output:
You can obtain more detailed information about the tunnel with
Example of expected output:
Start routing traffic
Now assign a CNAME record that points traffic to your tunnel subdomain
If you want to tunnel only a specific service, you can choose the final subdomain for that service, for example, if you going to expose only the
BTC RPC Explorer
, chooseexplorer.<domain.com>
or if you want to expose only theBTCpay Server
, choosebtcpay.<domain.com>
Replace
<UUID>
for your one obtained before
Example of expected output:
Configuration
We will create a configuration file in your .cloudflared
directory. This file will configure the tunnel to route traffic from a given origin to the hostname of your choice. We will use ingress rules to let you specify which local services traffic should be proxied to.
Staying with user
admin
, createconfig.yml
Here you should choose services that you want to expose publicly. This is only an example, so replace the ingress rules with your preferences. For example, you can replace
btcpay
orexplorer
with your name (subdomain) chosen for the service, and<domain.com>
with the domain, you purchased previously. Ensure to replace<UUID>
with your obtained before
Electrum server are not supported using Cloudflared
For security reasons, you shouldn't expose publically the administration access services using Cloudflared e.g SSH or Thunderhub, for these cases you should use Wireguard VPN
If you want to expose only a service, you can delete or comment the associated lines of other services, always maintaining the "
- service: http_status:404"
line at the end of the rules. Example, expose only BTCPay Server, the comment the associated lines for BTC RPC Explorer:
Configure Cloudflare DNS records
We will go back to the Cloudflare DNS records table to make modifications.
If you wanted to expose 2 services or more, that is to say, you ingressed more than one service on the ingress rules, follow the next steps, if not, you can only check the current recently created registry or jump directly to the next Increase the maximum UDP Buffer Sizes section:
Edit the existing CNAME record that was recently created, and replace the
name
value with the name of the first or one of the services selected, or keep it if it's correct. For example, if you selectedbtcpay
, keep the existing target content, which is the UUID of your tunnelAdd a new record by selecting CNAME type.Enter the second subdomain selected in the second ingress rule e.g
explorer
, in thename
box, and in thetarget
content, enter theUUID
of your tunnel (the same content as before)Ensure you have enabled
Proxy
for each record you have added (Proxy status: Proxied)
Example of DNS record table:
Increase the maximum UDP Buffer Sizes
Experiments have shown that QUIC transfers on high-bandwidth connections can be limited by the size of the UDP receive and send buffer.
With user
admin
, increase the maximum buffer size by editing the next file to add kernel parameters
Here are the lines you’ll want to add at the end of the file. Save and exit
Then apply the changes with
These parameters would increase the maximum send and receive buffer size to roughly 2.5 MB
Create systemd service
Create the configuration file in the nano text editor and copy the following content. Save and exit
Enable autoboot (optional)
Prepare
cloudflared
monitoring by the systemd journal and checking the logging output. You can exit monitoring at any time with Ctrl-C
Keep this terminal open, you'll need to come back here on the next step to monitor the logs
Run
To keep an eye on the software movements, start your SSH program (eg. PuTTY) a second time, connect to the MiniBolt node, and log in as admin
. Run the tunnel to proxy incoming traffic from the tunnel to any number of services running locally on your origin
Start the service
Now point your browser to the hostnames created in your
config.yml
e.ghttps://explorer.domain.com
orhttps://btcpay.domain.com
and check if it resolves correctly to the local service
You should see the service properly running as if it were a local connection
Ensure Cloudflared is listening on the random port assigned:
Example of expected output:
Upgrade
With user
admin
, stop Cloudflared
Check the current version of Cloudflared
Follow again the installation section of this guide, replacing the environment variable
"VERSION=x.xx"
value for the latest if it has not been already changed in this guideStart Cloudflared again
Monitor logs with journalctl -fu cloudflared
to ensure that all is still working well
Uninstall
With user
admin
, stop the Cloudflared
Use the deb package manager to uninstall Cloudflared
Example of expected output:
Port reference
Last updated