Image Resize on Worker return 502

Hi, :grinning:
I am on business plan. and my Cloudflare domain is . I am trying to run image resize on my worker, which custom domain is . The is an A record point to google cloud load balancer ip, which routes the traffic to google cloud stroage.
I met 502 response if I add query in the url.
And I found out that when the 502 error only occurs when I turn on the proxy on

  • 200
  • 404

The reason I enable proxy is that I have tried using Cache API in the worker to cache the image resized result of url using google storage( . But the cache hit ratio only reached about 50%, causing a lot of request run image resize service. So I am replacing the google storage url with proxied, hoping to cache the result. Also, I’ve tried using the https://<ZONE>/cdn-cgi/image format in the worker but not worked, See here.

Here is my code.

addEventListener("fetch", (event) => {
  try {
    return event.respondWith(handleRequest(event));
  } catch (err) {
    return new Response(err.stack, { status: 500 });

async function handleRequest(event) {
  let url = new URL(event.request.url);

  let options = { cf: { image: {} } };

  if (url.searchParams.has("fit")) = url.searchParams.get("fit");
  if (url.searchParams.has("w")) = url.searchParams.get("w");
  if (url.searchParams.has("h")) = url.searchParams.get("h");
  if (url.searchParams.has("q")) = url.searchParams.get("q");

  // Returns 502
  const imageURL = "" + url.pathname;
  // It works fine when using gcs url
  // const imageURL = "" + url.pathname;

  const imageRequest = new Request(imageURL, {
    headers: event.request.headers,

  return fetch(imageRequest, options);

Thanks for any help!

My guess would be that 502 error is coming from your origin (Cloudflare very rarely produces that status code itself). Could you please check the logs of your load balancer?

1 Like

Thank you, @albert ,
Yes, I’ve check the logs of my load balancer.

If I specify query with There’s no logs and returns 502.
if I don’t specify query, there’s logs and return 200.