If you’re a server owner looking to set up a reverse proxy for self-hosted applications on your own domain, you’ll eventually require a service to host your DNS (which will point users to the IP address of your web server/applications when they enter your domain into their browser’s address bar).
Luckily, Cloudflare — a well-known and reputable company in the content-delivery service industry — offers a free suite of services that will allow us to control which IP addresses our domains point to and who can access them.
In this guide, I’ll cover the basics to setting up a free Cloudflare account and how to ensure your domains and subdomains are set up to resolve properly when users try to find them.
What We’ll Need
Below is a list of the resources you’ll need to follow along with this guide. The only cost associated with any of the items listed below is the cost of a custom domain, which shouldn’t amount to more than $5–10/year.
- A custom domain (I’ll be using a dummy domain, shiftfourteen.com, which I purchased through NameCheap — but you can use any host you’d like)
- A Cloudflare account (the free account is fine for what we’ll need)
Registering Your Domain and Connecting It to Cloudflare’s DNS Service
If you haven’t already, head to Cloudflare’s site and begin the registration for a new account. After you’ve created your account and have logged in, select the option to “Add site”.
You’ll be asked for your domain — enter it to continue. Next, you’ll be asked which account type you’d like to sign up for. Select the “Free” option. We won’t need any premium features for the purposes of this guide.
After you choose your account type, you’ll be asked to review your DNS records. This is how we’ll manage your server’s domain and subdomains, but we’ll need to finish setting everything else up first. Skip the page by clicking “Continue”. We’ll modify these settings later.
Next, Cloudflare will bring you to the page with a set of instructions on updating the nameservers on your domain host’s website with two of Cloudflare’s own nameservers. The steps to do so will vary based on the domain host you selected. Below is a screenshot of my nameserver configuration in Namecheap after updating them with Cloudflare’s nameservers:
If you’ve purchased your domain through a host other than NameCheap and aren’t sure how to update your domain’s nameservers, go ahead and Google “<domain host> nameserver update Cloudflare” or something similar and one of the top search results should include a link to instructions. After you’ve updated your domain’s nameservers, click the “Done, check nameservers” button in Cloudflare.
On the next page, ensure SSL/TLS encryption mode is set to “Full” and scroll down and click “Continue”. (You can tweak the other settings if you’d like, I usually leave them as default.)
The final page of the Cloudflare setup is confirming your nameserver setup, which you should have done a few steps ago. While you may have already updated the nameservers in the settings of your domain host, it sometimes takes a bit for Cloudflare to recognize they’ve been updated.
Go ahead and click “Recheck now” to prompt Cloudflare to check for your updated settings. Note that while it mentions it may take a few hours, I’ve never experienced it taking more than a few minutes to recognize the updated nameservers. Refresh the page every 20–30 seconds until it updates with the message below, which means we’re successfully connected to Cloudflare.
Adding DNS Records to Cloudflare
Now that our domain is connected to Cloudflare, we can begin setting up its DNS records. These records will tell users how to find your application when typing your subdomain into their browser’s address bar. From your domain’s Cloudflare overview page, select “DNS” in the top row of menu items.
By default, you’ll notice five or six DNS records have already been created for you. Any MX records can be deleted. These are “Mail Exchange” records that are required for e-mail delivery to the domain, but that isn’t something I’ll cover in this guide (and I’ve personally never used them myself under this pretense).
Before we create the subdomain for our application, we need to ensure we have an A record for our base domain that points to our server’s IP address. This may be a default row for you — regardless, make sure it is set up as follows:
- Type: A record (this record type points a domain to an IP address)
- Name: Your base domain name (without the “www.”)
- Content: Your server network’s public IP address
- TTL: Auto
- Proxy status: Click the cloud until it is orange and says “Proxied”
Here’s what it looks like for me (I’ve obfuscated my IP address for obvious reasons):
Now when anyone navigates to a web address that references our base domain, Cloudflare will know the IP address of the server to direct the request to.
Next, we’ll need to create a record for a subdomain, which is what we’ll use to access individual applications on our server.
Because Cloudflare already knows our IP address from the A record we just created, we’ll set up any subsequent subdomain records as CNAME records and point them to our base domain. The benefit of doing so is that when our server’s public IP address changes, we’ll only need to update one record — our A record — and every other record pointing to it (our CNAME records) will automatically update as well.
Go ahead and enter the subdomains you’d like to use for your various applications. In this example, I was setting up a reverse proxy for my self-hosted instance of VS Code, so I’ve used development.shiftfourteen.com as my subdomain. Feel free to add as many as you’d like.
Make sure to click the cloud icon for your CNAME record until it becomes orange and says “Proxied”. This will prevent users from obtaining your server’s IP address through your domain and subdomain entries.
That’s it! Now your domain and subdomain entries have been successfully configured in Cloudflare and you’re ready to begin configuring your web server to accept the requests that will eventually begin coming in through Cloudflare.
Have any questions or comments about the content in this guide? Feel free to reach out to the author on reddit with any feedback or for additional help concerning these topics.