I have a Cloudflare worker that’s supposed to redirect my domain based on the configuration I have in my Cloudflare worker. It works successfully for the Cloudflare URL (the worker .dev url) but is not working for my domain mycloudsafari .com/blog
I just switched the nameservers over from AWS to Cloudflare and it seems to have propagated. I also have a proxy infront of my domains so it should be working.
Don’t know if this is the right place to post this, but was redirected here after trying to fill our a support question. Any help would be greatly appreciated cheers!
May I ask if you’ve correctly set the route path for the Worker?
Is there a way to share at least a part of your Worker code which does the redirection thing?
Furthermore, I can see the IP is with 188.xxx and some users are reporting some issues with the access/connection.
Nevertheless, what I see mycloudsafari.com/blog is different than mycloudsafari.com.com/blog/ → the trailing slash led me to some “redirection loop” and kind of a strange URL in my Web browser.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
try {
const urlObject = new URL(request.url);
// If the request is to the Ghost subdirectory
if (/^\/blog/.test(urlObject.pathname)) {
// Then Proxy to Ghost
const GHOST_URL = "blog.mycloudsafari.com";
const CUSTOM_URL = "mycloudsafari.com/blog";
let url = new URL(request.url);
url.hostname = GHOST_URL;
let proxyRequest = new Request(url, request);
proxyRequest.headers.set('Host', GHOST_URL);
//Have an X-Forwarded-Host header that matches the custom domain in my.ghost.org.
proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);
//Include the X-Forwarded-Proto header set to https not http.
proxyRequest.headers.set("X-Forwarded-Proto", "https");
//Include the X-Forwarded-For header, populated with the remote IP of the original request.
let ip = proxyRequest.headers.get("CF-Connecting-IP");
proxyRequest.headers.set("X-Forwarded-For", ip);
return await fetch(proxyRequest);
}
} catch (error) {
// if no action found, play the regular request
let response = await fetch(`https://mycloudsafari.com${urlObject.pathname}`);
return await fetch(request);
}
return await fetch(request);
}
I spent a good three days on this, needed to move quick so for now ended up just setting the blog at blog.mycloudsafari.com via Ghost (blog). Deleted the digital ocean droplet I had initially set up with the server and nginx serving the site.
Just giving this a bump again. Trying to get this setup again but running into the same issue.
My domain https://mycloudsafari.com is proxied by Cloudflare and I have a route set up to respond to all requests to *mycloudsafari.com/blog* with ghost.mycloudsafari.com/blog - this doesn’t seem to work despite the proxy.
In the worker dev URL the preview works (I.e. going to https://blog-trigger-2.cloudsafari-blog.workers.dev/blog/) but the actual domain for mycloudsafari.com/blog just goes to my 404 page on Next JS. Would love some help on this again, thanks.