1

I'm using a location config like this in Nginx for serving WEBP when the user supports it

    map $http_accept $webp_suffix {
        default   "";
        "~*webp"  ".webp";
    }
server {
#...
    location ~* \.(?:jpg|jpeg|png|webp)$ {
        root   /usr/share/nginx/html;
        try_files $uri$webp_suffix $uri =404;
    }
#...
}

my problem is that I want to use the same strategy but when I'm using an object-storage (Minio - S3 compatible)

so I'm wondering what is the best way to replace this part:

try_files $uri$webp_suffix $uri =404;

with something like

try_proxy_pass http://minio$uri$webp_suffix http://minio$uri =404;
erfan
  • 21

1 Answers1

0

here is a solution for this that I put together myself.

if you want to read more here is a link to a repo that I made for testing and Proof of concept that this solution is working:https://github.com/erfantkerfan/cdn-nginx-image-optimization

    recursive_error_pages on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;
proxy_intercept_errors on;

proxy_connect_timeout 300;
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;

location ~* \.(?:jpg|jpeg|png|webp)$ {
    root   /usr/share/nginx/html;
    try_files /dev/null @image_webp;
}

location @image_webp {
    proxy_pass http://minio_servers$uri$webp_suffix;

    image_filter_jpeg_quality 95;
    image_filter_webp_quality 100;
    image_filter_interlace on;
    image_filter_buffer 100M;
    image_filter resize $width $height;
    image_filter crop $width $height;
    image_filter_transparency on;

    error_page 404 415 = @image;
}

location @image {
    proxy_pass http://minio_servers$uri;

    image_filter_jpeg_quality 95;
    image_filter_webp_quality 100;
    image_filter_interlace on;
    image_filter_buffer 100M;
    image_filter resize $width $height;
    image_filter crop $width $height;
    image_filter_transparency on;
}

}

erfan
  • 21