如今随着各终端的出现(手机,ipad等平板),以及各类终端的手机分辨率和尺寸都不一样,如今手机用户流量都是宝,网上出现了各类各样的生成缩略图功能的架构,有使用php实时生成缩略图的,也有用nginx + lua实现的,上节我也讲到了使用nginx生成缩略图,可是用户每次访问都须要生成一次,会给cpu和硬盘带来比较大的压力,今天带来了另一种方式,此次使用nginx将原图生成缩略图到硬盘上.看个人配置 php
1. 首先建好cache目录 html
1 |
# mkdir /data/site_cache/ |
2. 修改nginx配置 nginx
01 |
location ~* ^/resize { |
02 |
root /data/site_cache/$server_name; |
07 |
if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) { |
14 |
if ($uri ~* "^/resize/(.*)" ) { |
18 |
set $image_uri image_resize/$image_path?width=$width&height=$height; |
20 |
if (!-f $request_filename) { |
24 |
proxy_store /data/site_cache/$server_name/resize$demins/$image_path; |
25 |
proxy_store_access user:rw group:rw all:r; |
26 |
proxy_set_header Host $host; |
31 |
location /image_resize { |
32 |
alias /data/site/$server_name/; |
33 |
image_filter resize $arg_width $arg_height; |
34 |
image_filter_jpeg_quality 75; |
生成缩略图流程以下:
一、原图在www.ttlsa.com/image/1.jpg。我须要一份100x100的缩略图。
二、请求www.ttlsa.com/resize_100x100/image/1.jpg.
三、这个请求进入了location ~* ^/resize,接着判断image_path这个目录下是否存在这张图片,若是存在直接放回给用户,
四、不存在那么跳转到http://www.ttlsa.com/image_resize/image/1.jpg?width=100&height=100;
五、location /image_resize根据传入的width和height执行缩略功能,而且设置图像质量为75
六、接着生成文件到/data/site_cache/www.ttlsa.com/resize_100x100/image/1.jpg
七、而且返回图片给用户
八、nginx生成缩略图到硬盘上的功能到这里就结束了
转载请注明出处: nginx实时生成缩略图到硬盘上 架构