Search code examples
sslfetchcloudflarecloudflare-workers

CloudFlare workers fetch HTTPS works on workers.dev subdomain but not on own subdomain getting 525 error


I have a simple worker that just does a fetch against an HTTPS endpoint somewhere else.

The code is literally just:

return await fetch('https://something.com/someResource')

When I test locally (wrangler dev) and even publish to a workers subdomain this works fine. When I curl https://foo.bar.workers.dev/myEndpoint I get the same response as https://something.com/someResource.

However I want to run this from my own domain (managed through cloudflare) so the worker also has a route of foo.mydomain.com/* and a AAAA record to 100:: for foo as per CloudFlare docs. The DNS works fine the URL is reachable, but when I try to hit https://foo.mydomain.com/myEndpoint CloudFlare's worker logs show that the fetch behind the scenes fails with a 525 error (SSL Handshake fail).

Things I've tried based on some CloudFlare forum posts:

  • Add a page rule foo.mydomain.com/* -> SSL Mode: full since my overall SSL settings are set to flexible.
  • Set the host header in the fetch to the origin domain ( fetch(url, {headers: {'Host': 'something.com'}})

FYI, I don't control the origin server as it's an external API I work with.

How come the same request works from local and *.workers.dev but not my own domain?


Solution

  • Your page rule is not taking effect. The page rule is for foo.mydomain.com/*, but it has to match the subrequest URL, which in this case is https://something.com/someResource, which doesn't match. It doesn't matter that the original worker request matched -- what matters, in this case, is whether the subrequest URL matched.

    Unfortunately, you cannot create a page rule that matches a domain other than your own.

    Instead, what you'll need to do is reverse things. Set your SSL mode to "full" by default, but then use page rules to set it to "flexible" for your own domain.

    (Note: The "SSL Handshake fail" error itself is actually a known bug in Workers, that happens when you try to talk to a host outside your zone using HTTPS but you have "flexbile" SSL set. We do not use flexible SSL when talking to domains other than your own, but there's a bug that causes the request to fail instead of just using full SSL as it should.)