Deploying a Hugo Site to Cloudflare Pages
For my personal blog (this site) I wanted something that is easy to use since this is my first blog. Hugo seems like a good choice because I can write in Markdown, and using Cloudflare Pages means I can register my domain, configure DNS, and host the site all from the same interface. Pretty convenient.
Cloudflare Pages have one big advantage vs Github Pages: with Cloudflare Pages you can build your site from a private Github repository. Where as if you want a Github Pages site from a private repo, you need to pay Microsoft.
Setting up Hugo
Cloudflare’s guide covers pretty much everything. Basically:
- Install Hugo
- Create a new project/site using
hugo new site
- Turn the project into a git repository (don’t know why
hugo new site
doesn’t do this automatically) - Do some basic setup for your site
- Add a theme you like from Hugo Themes
- Modify your site’s configuration by copying the default configuration for the theme
- Create a new Github repository and push the site to it (should also work with Gitlab)
Deploying to Cloudflare Pages
This part is straight forward. You almost don’t need to read the guide, the UI is intuitive enough to setup everything.
- From your Cloudflare dashboard, in account home (not site), go to
Workers & Pages
>Create application
>Pages
>Connect to Git
- Follow the instruction to give permission for Cloudflare to access your repo. I only gave permission to the my site’s private repo.
- Back at Cloudflare’s interface, setup the build and deployment configuration.
- Click
Save and Deploy
After deploying the site, you get a unique *.pages.dev
subdomain for
your project.
Setting up Custom Domain
This part is also straight forward since I registered my domain with Cloudflare, so I didn’t need to configure nameservers.
Redirect www
to domain apex
You need to do this if you also want to access your site with the www
subdomain,
for example https://www.example.com
should also go to your site
(https://example.com
).
This is the part where I had trouble following the guide, since the guide
had some discrepancy with the actual Cloudflare UI. I was also confused since I
thought you can do this by creating a CNAME record that points the www
subdomain
to the apex domain, which I think doesn’t work because you have to use Cloudflare Page’s
UI to add the custom domain in order to use custom domains to access your site.
According to Cloudflare (link):
To ensure a custom domain is added successfully, you must go through the Add a custom domain process described above. Manually adding a custom CNAME record pointing to your Cloudflare Pages site - without first associating the domain (or subdomains) in the Cloudflare Pages dashboard - will result in your domain failing to resolve at the CNAME record address, and display a 522 error.
Anyways, to redirect www
to your domain apex:
First, proxy the DNS record of the www
subdomain through Cloudflare,
in order to use Cloudflare’s Bulk Redirect (and Single Redirects, however that
is not used here) (doc):
- Add an A record that points your
www
subdomain to192.0.2.1
and set the proxy status to proxied
Next create the Bulk Redirect rules:
- Create a new Bulk Redirects list, by clicking
Create Bulk Redirect List
- Name your redirect list
- Click
manually add URL redirects
- Fill in
Source URL
andTarget URL
Source URL
: yourwww
subdomain without thehttps
, ex:www.example.com
Target URL
: your apex domain with thehttps
, ex:https://example.com
- Select
Edit parameters
and selectPreserve query string
,Subpath matching
andPreserve path suffix
- Click
Next
andNext
to finish creating the Bulk Redirects list - Next you need to enable the list by creating a Bulk Redirect rule, so click
Create Bulk Redirect Rule
- Give your rule a name, select the list you created earlier, and click
Save and Deploy
- Done!
Now you have a working Hugo site hosted with Cloudflare Pages, and you
can access it with a custom domain with or with out the www
!