Skip to content

Instantly share code, notes, and snippets.

@jamesnguyen101
Last active June 29, 2018 08:15
Show Gist options
  • Save jamesnguyen101/f57a8684f3735affe5bd257847cd75f8 to your computer and use it in GitHub Desktop.
Save jamesnguyen101/f57a8684f3735affe5bd257847cd75f8 to your computer and use it in GitHub Desktop.
Nginx filter module - dynamic resize image
server {
# Internal image resizing server.
server_name localhost;
listen 888 ;
allow 127.0.0.1;
deny all;
access_log /var/log/nginx/image_access.log;
error_log /var/log/nginx/image_error.log;
set $image_dir /opt/nginx/html/image;
root $image_dir;
# eg
# link http://localhost:888/media/image/a/abc.png?width=300
# dir /opt/nginx/html/image/a/abc.png
location ~* \.(gif|jpg|png)$ { #ignore case sensitive
image_filter_buffer 50M;
image_filter_jpeg_quality 95;
image_filter_transparency on;
#alias $image_dir;
set $width 250;
#set $height 500;
if ( $arg_width ) {
set $width $arg_width;
#set $height $arg_height;
}
#image_filter resize $width $height;
image_filter resize $width -;
error_page 415 = /empty;
}
location = /empty {
empty_gif;
}
location / {
deny all;
}
}
# https://www.nginx.com/blog/nginx-caching-guide/
proxy_cache_path /opt/nginx/cache/images/ levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
server {
# public cache server.
# add md5 hash link for secure protection
server_name cdn.xxx.me;
listen 80;
access_log /var/log/nginx/cdn_access.log;
error_log /var/log/nginx/cdn_error.log;
location / {
deny all;
}
location /media/image/ {
proxy_pass http://localhost:888/;
proxy_set_header Host $host;
proxy_buffering on;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment