Skip to content

Instantly share code, notes, and snippets.

@BaseMax
Forked from webmasterkai/img.l
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaseMax/8c34bee3617e50f3a0ab to your computer and use it in GitHub Desktop.
Save BaseMax/8c34bee3617e50f3a0ab to your computer and use it in GitHub Desktop.

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