Cloudflare Pages With Worker Re-Direction Origin Down
I have a pages project that I wish to display if my origin is down... I have a worker setup with the following code:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const originUrl = 'https://x.x.io/panel';
try { // Try to fetch from the origin server const response = await fetch(originUrl); // If the origin server is not down, return the response return response; } catch (err) { // If there's an error (e.g., the origin server is down), // fetch from the backup source return fetch('https://cloudflare-x.dev/'); } }
try { // Try to fetch from the origin server const response = await fetch(originUrl); // If the origin server is not down, return the response return response; } catch (err) { // If there's an error (e.g., the origin server is down), // fetch from the backup source return fetch('https://cloudflare-x.dev/'); } }
18 Replies
I do still see the route on the worker still stuck on init.
Issuing a certificate, then I should be able to test
Ok, so I got a setup that mostly works, but I still need to check about something else. I'll let you know what I figure out
Just to check, what do you see when you visit https://myservice.goalastair.com/
Hello from Origin page
And now?
Hello from pages
nice
Why a block
I think the script I used needed some adjustments to get what you have
Think it’s just the code, as I have a route to point traffic to the worker, then a custom hostname on the pages
The setup is pretty simple.
1. Create a DNS record for your origin. This should not be the hostname you want your users to see.
2. Add the following Advanced Function to the output directory of your Pages Project with the name
_worker.js/index.ts
(you can also convert it to JavaScript if you want, then you can call it _worker.js
.
3. Modify the origin to match the DNS record you created. The timeout is set to 1 second, modify it if you wish.
4. Deploy the Pages project, then add a custom domain to the hostname you want your users to see.
5. (Optional) Create a firewall rule that blocks requests if (http.host eq "yourorigin.com" and cf.worker.upstream_zone ne "yourproject.pages.dev")
, replacing project.pages.dev with your Pages domain, and yourorigin.com with your origin hostname.Here's the Worker in JS if you prefer that.
The block page prevents you from hitting the origin without hitting the Function first
That’s awesome. Thank You!
Just on the DNS records. Say I want users to see example.com. Are you saying create an A record for say proxy.example.com and also another records for example.com. What would the values be. Currently I’d have say example.com = 123.123.123.123
And then the obligatory warning that while the normal proxy and Pages are free, requests served via Functions are not free(after the free cap), so there may be a cost there, though very small
A
record for proxy.example.com
(though I would probably go with origin.example.com
, for clarity's sake) pointing to 123.123.123.123
.
CNAME
record for example.com
to project.pages.dev
. This one should be generated by Pages automatically when you add a Custom Domain, as long as example.com
is using Cloudflare Nameservers.
Also, bonus tip if you want, you can completely avoid exposing your origin IP by using Tunnels. This also means that your origin can entirely block incoming connections.
As long as you don't need them for something else, of courseI have tunnels on this test. Assume this will still work regardless?
Yes, just note that when using tunnels, instead of an
A
record for proxy.example.com
, you would have a CNAME
to the Tunnel. If you use Dash Tunnels then this record will be created automaticallyI will try this again later. Thanks!
Caching rules etc are all still going to work using the method of going through the worker initially?
No, they will not. You need to add those to the
fetch
toward the origin.
Requests to Pages are cached automatically, so those don't need to be modified.Hmm ok that’s another day 😀
I have caching to origin now. But you say I need to amend those?