[17 February 2025]
~This article explains how to tunnel pre-existing web-server using cloudflare tunnels~
Note: If you donβt already have a domain, go get a domain first, it is required to use cloudflare tunnels.
Cloudflare setups up an outbound connection between the device in your local network and the nearest cloudflare datacenter. These tunnels are persistent objects that route traffic to the DNS records.
The first step to using cloudflare tunnels is to make sure your domain is registered with cloudflare. If you purchased your domain through cloudflare you can skip this part of changing the nameserver, as they would be registered with cloudflare anyways.
To change the name servers, go to the provider who you got the domain from and change the name servers. In my case I got the website from hostinger, so I shall change my nameservers from hostinger to cloudflare.
Changing name servers can take upto 24hrs, but in most cases it happens in under 15 minutes, so be patient. Cloudflare will email you once the nameservers have been changed.
Once the nameservers have been changed sucessfully, you should be able to view them in your cloudflare dashboard, and the domain should show up like so:
After that head over to the "Zero Trust" section in the left hand side. Once you open the zero trust panel, you should be able to navigate to the tunnels section which is under the "Networks" tab and create a new tunnel like so:
Then do the following steps:
The code that is inside the green block should only be run on the device hosting the nginx server. What it does is install a cloudflare client and setup the tunnel. It is a unique one-time code that contains a key to setup the tunnel, so make sure the connection is established before trying to change / reload the page.
Once you executed the code, you should be able to see your device under the 'connectors' tab below.
Replace the ipaddr
with the IP address of the machine that is running the nginx server. Default port for nginx is 80
, so that's what we are using. If you have used another port for nginx, use that port instead.
Once the tunnel is created cloudflare should automatically create DNS records on your behalf to set things up. Now you should hopefully be able to visit your website.
Note: The domain given while creating tunnel should be mentioned in the nginx .conf (config) file, else this will not work.
Powered Not An SSG π