例子:http://192.168.1.200:12345/group1/M00/00/04/wKgByFoL5JCAKs5CAAKTjdZqEq4291.jpg_500x400.jpg
server{
listen 12345;
client_max_body_size 100m;
#########使用nginx_image_filter生成缩略图并保存在服务器上############################
#第一步:访问缩略图
location ~ '^/group1/M00/([0-9A-F]{2})/([0-9A-F]{2})/(.*)\.(jpg|jpeg|png)_([\d]+)x([\d]+)\.(jpg|jpeg|png)$' {
#直接从缩略图所在目录查找
root /home/fastdfs/cache/;
#如果缩略图不存在,转到生成缩略图的location
if (!-f $request_filename) {
#记录变量
set $width $5;
set $height $6;
set $image_path $1/$2;
set $image_name $3.$4;
#把缩略图的尺寸放入请求参数中
set $image_uri thumb_image/$image_path/$image_name?width=$width&height=$height;
proxy_pass http://127.0.0.1:$server_port/$image_uri;
}
#如果缩略图存在,则直接break,因为缓存缩略图的配置只能写在location的根下
if (-f $request_filename) {
break;
}
#缓存缩略图
#缩略图保存目录
proxy_store /$request_filename;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /home/fastdfs/cache_temp/;
proxy_set_header Host $host;
proxy_set_header Content-Type image/jpeg;
}
#第二步:生成缩略图
location /thumb_image {
#原图目录
alias /home/fastdfs/data/;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 25; # 压缩质量
image_filter_buffer 20m; # 如果原始图片大小起过1m,则会报415错误
#不允许外部访问
allow 127.0.0.0/8;
deny all;
}
######使用nginx_image_filter生成缩略图并保存在服务器上
#访问原图:匹配FastDFS,不需要缩略图的,直接使用FastDFS即可
location ~*^/group1/M00/.*\.(jpg|jpeg|png)$ {
ngx_fastdfs_module;
}
}