Nginx forward request with static file(png file) to R2 Storage

Hi, support. I want to using Nginx forward request with static file(png file) to R2 Storage. But it respone 404 not found error. Does any configuration is incorrect?

location ~ ^/testimage/(.*) {
proxy_set_header Referer “test”;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Connection “”;
proxy_set_header Authorization ‘’;
proxy_set_header Host abc_com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass abc_com
}

proxy_set_header Referer “test”;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Connection “”;
proxy_set_header Authorization ‘’;
proxy_set_header Host abc_com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Probably don’t need all these headers set

This should be the URL to your R2 bucket.

I would look at something like

and fill in with your R2 URL.

1 Like

Hi, As this post can not include link. So the abc_com means https abc com

Is https.abc.com the public link to your Cloudflare R2 bucket?

It is the demo link. As real link can link to my r2 storage.

What URL is being passed to nginx and what is the path to the file in R2?

Demo:
https://test.com/testimage/test.png Nginx Backend forward and pass to https://abc.com/test.png

The /testimage/ part of the URL is probably being passed to R2. So the image should be at https://abc.com/testimage/test.png.

Thanks. I also test this r2 link.It still failed. Is any r2 block it?

I got it to work using the following config, note this was run in a docker container which is why I am using port 80:

events {
    use                 epoll;
    worker_connections  2048;
    multi_accept        on;
}

http {
    server {
        listen 80;
        set $bucket "<public bucket URL>";
        server_name nginx-r2.dev.cyberjake.xyz;

        location / {
            root /usr/share/nginx/html/;
        }

        location ~ ^/image/(.*) {
            resolver 1.1.1.1;
            proxy_redirect off;

            add_header             Cache-Control max-age=31536000;
            proxy_ssl_server_name on;
            proxy_set_header       Host $bucket;
            proxy_pass https://$bucket;
        }
    }
}

This does require the images to be at images/ in R2

1 Like

I also just went and created a blog post about setting this up

1 Like

Thanks a lot. Your provide solution seems ok . I also found my mistake. The below png Ngnix config overwrite the upper /image/* ngunx config.

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