Skip to content

Instantly share code, notes, and snippets.

@roman-wb
Last active January 14, 2020 17:24
Show Gist options
  • Select an option

  • Save roman-wb/d6a7a050e7d4175d520d3bc3e20faa57 to your computer and use it in GitHub Desktop.

Select an option

Save roman-wb/d6a7a050e7d4175d520d3bc3e20faa57 to your computer and use it in GitHub Desktop.
Nginx + Image filter (resize/crop) + Proxy cache (optional) + Secure link (optional)
# nginx.conf
load_module /etc/nginx/modules/ngx_http_image_filter_module.so;
# host.conf
proxy_cache_path /tmp levels=1:2 keys_zone=thumbs:10m inactive=24h max_size=100M;
server {
listen 80;
root /usr/share/nginx/html;
location /i/ {
# comment if not used secure link
# echo -n '/i/crop/100x100/test.jpg SECURE' | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =
# ruby convert Base64.encode64(url).gsub('+','-').gsub('/','_').gsub('=','').strip
secure_link $arg_hash;
secure_link_md5 "$uri SECURE";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 418;
}
gzip_static on;
expires max;
add_header Cache-Control public;
proxy_pass http://127.0.0.1:8080;
# comment if not used proxy cache
proxy_cache thumbs;
proxy_cache_valid 200 24h;
proxy_cache_valid 404 415 1m;
}
}
server {
listen 127.0.0.1:8080;
root /usr/share/nginx/html;
image_filter_buffer 10m;
image_filter_jpeg_quality 90;
image_filter_interlace on;
location ~ ^/i/resize/(?<width>\d+|-)x(?<height>\d+|-)(?<path>/.*?\.(?:jpg|gif|png))$ {
rewrite ^.*$ /$path;
image_filter resize $width $height;
break;
}
location ~ ^/i/crop/(?<width>\d+|-)x(?<height>\d+|-)(?<path>/.*?\.(?:jpg|gif|png))$ {
rewrite ^.*$ /$path;
image_filter crop $width $height;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment