Image Resizing + R2 = Fail

Hi,

I have the following setup:

  1. Cloudflare Image Resizing Enabled in the site Speed Optimizing.
  2. Worker setup to use Image Resizing. It validates the request and sets the dimensions.
  3. Custom domain set on R2 to allow public access. The worker uses this domain to access the images in R2 for Image Resizing.

However, this does not work consistently.

When accessing image through the Worker URL, Cloudflare returns the error “ERROR 9421: Too many redirects”.

Eg.

  1. Visit link to view thumbnail: https://<myworker-domain>/<somekeypath>/thumb/IMG_5.jpg.
  2. Wrangler tail shows the worker getting to the end and forwarding the request to Cloudflare Image Resizer.
  3. Error is returned.
    Headers show cf-resized: err=9421 , cf-cache-status: BYPASS
    Status: 400
    Body: ERROR 9421: Too many redirects

If I visit the original image directly I get a constant stream of 301.

Eg.

  1. Visit link to view original image in R2 using custom public domain: https://<r2-custom-public-domain>/<somekethpath>/IMG_5.jpg
  2. Browser returns Status Code 301
  3. Header shows the location header set to the exact same path as in step 1.
  4. This repeats until chrome shows the error ERR_TOO_MANY_REDIRECTS.
  5. I do see the header shows cf-cache-status: HIT

If I purge cache, then refresh the R2 link, I can view the image. Then accessing from the worker works as well, showing the proper thumbnail. However, If I first access the worker link, then I still get the same error as above.

I’ve had a ticket open with Cloudflare support since March 30th and have heard nothing back from their support team.

Any ideas?

1 Like

I’m running into the exact same issue.

Using Image Resizing for images stored in R2, subdomain configured to serve images from the bucket.

Just with using the /cdn-cgi/image/Image Resizing prefix, I’m getting the same behavior:

Opening https://cdn-staging.example.com/cdn-cgi/image/w=256/menu/image.jpeg first results in ERROR 9421: Too many redirects.
Any subsequent requests to https://cdn-staging.example.com/menu/image.jpeg (the R2 origin’s public domain) result in redirect loop.

When I purge everything and hit the https://cdn-staging.example.com/menu/image.jpeg first, then I get the image, and all subsequent requests to https://cdn-staging.example.com/cdn-cgi/image/w=256/menu/image.jpeg keep working.

@keith25 did you get any response from the support team? Did you find a workaround?

It seems to be working correctly now.

For the reference, I found a workaround using a Cache Rule. Bypassing cache for the origin path allowed the image resizing api to work correctly.

Expression for the rule:

(not http.request.uri contains "/cdn-cgi/image" and http.host eq "cdn.example.com")

Then Bypass cache.

But again, this is not needed anymore, at least for now.

Yes, I did.

I had to change the SSL settings for the site to “Full”. I had them set to “Flexible”.

Even though traffic was being sent via HTTPS, this change made things work.

They said this was because:

R2 does not like non HTTPS traffic.
R2 was intended to perform and work only via HTTPS traffic, and as such, the error messaging/execution is not very well documented displayed/relayed.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.