Skip to content

Instantly share code, notes, and snippets.

@webmasterkai
Forked from phpdude/nginx.conf
Last active November 3, 2018 08:49
Show Gist options
  • Save webmasterkai/5178492 to your computer and use it in GitHub Desktop.
Save webmasterkai/5178492 to your computer and use it in GitHub Desktop.
Mirror remote image files, supports dynamic resizing of images. nginx image_filter remote fetching with a local mirror of original and resized image. Using try_files instead of if statements and proxy_pass with proxy_store for permanent local storage. No cache expiration, that will need to be handled outside of this.

Nginx image filter + caching of results.

Supports dynamic thumbnails images sizes processing + caching results, simple to use.

Awesome!!!

server {
server_name img.l;
root /var/www/cache/store/ns365;
index index.html;
# This requests the original file from itself and then resizes the image.
location ~ /resize/(\d+)x(\d+)/(.*) {
proxy_pass http://img.l/$3;
image_filter resize $1 $2;
image_filter_jpeg_quality 90;
image_filter_buffer 10M;
# Do not call this directly because the resized image is NOT cached.
allow 127.0.0.0/8;
deny all;
}
# Access denied.
location /resize {
return 403;
}
# RESIZED: http://img.l/200x200/files/2013/03/March_11_2013_ML.jpg
location ~ /(\d+x\d+/.*) {
try_files /$1 @img;
}
# ORIGINAL: http://img.l/files/2013/03/March_11_2013_ML.jpg
location / {
# If we don't find the file locally download it.
error_page 404 = @proxy;
}
# This saves the resized image locally.
location @img {
proxy_pass http://img.l/resize$uri;
proxy_store /var/www/cache/store/ns365$uri;
}
# This gets the remote image and saves it locally.
location @proxy {
proxy_pass http://example.com$uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_store /var/www/cache/store/ns365$uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment