1. 首先建好cache目录
1
|
# mkdir /data/site_cache/
|
2. 修改nginx配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
location ~* ^
/resize
{
root
/data/site_cache/
$server_name; #
set
$width 150;
set
$height 100;
set
$dimens
""
;
if
($uri ~*
"^/resize_(\d+)x(\d+)/(.*)"
) {
set
$width $1;
set
$height $2;
set
$image_path $3;
set
$demins
"_$1x$2"
;
}
if
($uri ~*
"^/resize/(.*)"
) {
set
$image_path $1;
}
set
$image_uri image_resize/$image_path?width=$width&height=$height;
if
(!-f $request_filename) {
proxy_pass http:
//127
.0.0.1/$image_uri;
break
;
}
proxy_store
/data/site_cache/
$server_name
/resize
$demins/$image_path; #保存[缩略图]地址【proxy_store /data/web/wallpaper/Public/data/site_cache/$server_name/resize$demins/$image_path;】
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
expires 30d;
access_log off;
}
location
/image_resize
{
alias
/data/site/
$server_name/; #这是原图地址【alias /data/web/wallpaper/Public/data;】
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
access_log off;
}
|
生成缩略图流程如下:
1、原图在www.ttlsa.com/image/1.jpg。我需要一份100x100的缩略图。
2、请求www.ttlsa.com/resize_100x100/image/1.jpg.
3、这个请求进入了location ~* ^/resize,接着判断image_path这个目录下是否存在这张图片,如果存在直接放回给用户,
4、不存在那么跳转到http://www.ttlsa.com/image_resize/image/1.jpg?width=100&height=100;
5、location /image_resize根据传入的width和height执行缩略功能,并且设置图像质量为75
6、接着生成文件到/data/site_cache/www.ttlsa.com/resize_100x100/image/1.jpg
7、并且返回图片给用户
8、nginx生成缩略图到硬盘上的功能到这里就结束了
转载请注明出处: nginx实时生成缩略图到硬盘上