Search code examples
nginxproxydigital-oceanspaces

Can not proxy assets requests to file storage with nginx


I am trying to proxy requests to digital-ocean space storage, example: GET http://example.com/download/image_id.jpg ---> https://bucket-name.fra1.cdn.digitaloceanspaces.com/images/image_id.jpg, but I get

<Error>
<Code>NoSuchBucket</Code>
<BucketName>example.com</BucketName>
<RequestId>tx000000000000018d441fd-005d582ff2-1b7a15-fra1a</RequestId>
<HostId>1b7a15-fra1a-fra1</HostId>
</Error>

Here is my full configuration:

server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }

    location /download/ {
        proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images$request_uri;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        resolver 8.8.8.8;
 }

}

I have already tried different options with/without trailing slash, but after reading docs carefully, I think now it is optimal, configuration, also I tried to debug where I am proxied replacing

proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images$request_uri;

with

proxy_pass https://google.com/images$request_uri;

and it gave me what I expected.

According to docs, what I want to do looks like this:

location /download/ {
proxy_pass https://bucket_name.fra1.digitaloceanspaces.com/images;
...

So that download part is replaced with images, but, it didn't work.

At the moment (after debugging with google) I am almost confident that url after proxie_pass is equal to https://bucket_name.fra1.digitaloceanspaces.com/images/image_id.jpg, so maybe the key problem is in wrong approach to "spaces"

P.S. I'm open to advices about better way to log/debug proxy_pass value .


Solution

  • So... proxy_set_header Host $host;was breaking everything. Removing it fixed proxing.