查看模块是否安装
/usr/local/nginx/sbin/nginx -V
nginx配置
server {
listen 80;
server_name 127.0.0.1; #域名或IP
root /root/data; #图片目录
#实现图片缩略图
location ~ (.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ {
set $w $3; #宽
set $h $4; #高
set $q $5; #图片质量
set $type $6;
set $image_path $1.$2; #真实图片地址
set $cache_path $1_$3w_$4h_$5Q_$6.$2; #临时文件地址
if ($type = 'r') {
set $type 'image-resize';
}
if ($type = 'c') {
set $type 'image-crop';
}
set $image_uri /$type$image_path?w=$w&h=$h&q=$q;
if (-f $cache_path) {
rewrite (.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
break;
}
if (!-f $cache_path) {
proxy_pass http://127.0.0.1$image_uri;
break;
}
proxy_store $cache_path;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /tmp/image;
proxy_set_header Host $host;
expires 30d; # 设置图片过期时间30天
}
#图片裁剪
location ~ /image-resize(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF) {
rewrite /image-resize(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)$ $1.$2 break;
image_filter resize $arg_w $arg_h;
image_filter_jpeg_quality $arg_q;
image_filter_buffer 50M;
try_files $1.$2 /img/notfound.jpg;
}
#图片缩放
location ~ /image-crop(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF) {
rewrite /image-crop(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)$ $1.$2 break;
image_filter crop $arg_w $arg_h;
image_filter_jpeg_quality $arg_q;
image_filter_buffer 50M;
try_files $1.$2 /img/notfound.jpg;
}
#正常图片
location /image {
expires 30d;
}
}
访问方式
http://127.0.0.1/image/xxx.jpg
http://127.0.0.1/xxx.jpg@100w_100h_100Q_r
http://127.0.0.1/xxx.jpg@100w_100h_100Q_c