Local Hosting using CloudFlare DNS

Dec 07, 2023
 — by 
Kris Kratz

CloudFlare is the absolute best for hosting domains for a lot of great reasons. Not only do they offer the lowest prices, they include all manner of features to accompany the service. They want you to find great success with your websites and pay for their more advanced features, but the basics are awesome for self hosting.

I like using a home server to run several websites and to test sites too. I run a PhotoPrism site, which is like a mini Google Photos. I run my own Git server too. Then I have some other test websites on my laptop, so I can test very quickly before uploading to a live server.

Another great feature from CloudFlare is that they support ddclient for Dynamic DNS updates. I used to have a shell script that would run every minute, but that stopped working when I activated the VPN. Plus, it required my server to be running. A better solution is something that runs directly on the router and only updates the records when it detects an IP change. My router allows me to install ddclient as a service. When my internet service provider changes my public IP address, ddclient updates my website DNS records to they continue to point to the correct location.

I also activate the “proxied” option with my CloudFlare DNS domain records. When CloudFlare proxies those domains, it obscures the actual IP Address of my local home server and it offers DDoS protection and CloudFlare automatically upgrades the protocol HTTPS. They even provide the SSL certificate.

I use Caddy as a reverse proxy and I needed to make sure that Caddy was only serving the site over HTTP. I found out about this because I ran into a frustrating error, “too many redirects” on all of my sites when I activated CloudFlare’s proxying feature on my DNS record. It was driving me nuts for an hour until I found their explanation on why I was getting the “too many redirect” error on my CloudFlare proxied DNS records.

Caddy is configured to automatically elevate the protocol to HTTPS, but CloudFlare is expecting an HTTP response to elevate it to HTTPS themselves! So I just needed to make Caddy serve the site over HTTP to CloudFlare.

I found two ways to accomplish this in your CaddyFile. You can disable Caddy’s auto_https with the global configuration. Just put this at the top of your CaddyFile:

    auto_https off

That will turn it off for all sites, and it’s perfect if you’re using CloudFlare proxied DNS records for all of your sites, like me.

You can also turn off HTTP -> HTTPS redirection in Caddy on a site by site basis for sites that Caddy does manage the certificate for. Just add http:// in front of any of your reverse proxy so it looks like this:

http://example.com {
    reverse_proxy ....