nginx生成缩略图配置 – ttlsa教程系列之nginx (转)

本文介绍了如何利用Nginx及其自带模块生成多种尺寸的缩略图,以优化图片加载速度并减少用户流量消耗。通过配置示例,详细解释了如何在Nginx中实现图片缩放功能,包括使用image_filter指令来裁剪和调整图片大小,同时提供了生成缩略图到硬盘上的方法以减轻服务器压力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了手机端浏览到与手机分辨率相匹配的图片,提高app访问速度以及减少用户的手机流量,需要将图片生成缩略图,这边共有以下解决方案。
A.??? 发布新闻生成多重缩略图 – 无法匹配到各种尺寸图片
B.??? 当相应缩略图不存在,则使用php或者java等程序生成相应缩略图 – 需要程序员协助
C.??? 使用nginx自带模块生成缩略图 – 运维即可完成
D.??? 使用nginx+lua生成缩略图

经过多方的考虑,决定使用方案C,使用nginx自带模块生成缩略图,模块:–with-http_image_filter_module.
如下是我的安装参数:

./configure --prefix=/usr/local/nginx-1.4.1 --with-http_stub_status_module \

--with-http_realip_module --with-http_image_filter_module --with-debug

修改nginx.conf配置文件,或者放到你相应的server块中.

location ~* /(\d+)\.(jpg)$ {

set $h $arg_h;?? # 获取参数h的值

set $w $arg_w; # 获取参数w的值

#image_filter crop $h $w;

image_filter resize $h $w; # 根据给定的长宽生成缩略图

}

location ~* /(\d+)_(\d+)x(\d+)\.(jpg)$ {

if ( -e $document_root/$1.$4 ) { # 判断原图是否存在

rewrite /(\d+)_(\d+)x(\d+)\.(jpg)$ /$1.$4?h=$2&w=$3 last;

}

return 404;

}

例如图片:

http://test.ttlsa.com/123_100x10.jpg

1、??? 首先判断是否存在原图123.jpg,不存在直接返回404(如果原图都不存在,还生成缩略图干啥,对吧)
2、??? 跳转到http://tset.ttlsa.com/123.jpg?h=100&w=10,将参数高h和宽10带到url中。
3、??? Image_filter resize指令根据h和w参数生成相应缩略图。
备注:长宽取小,例如原图是100*10,你传入的是10*2,那么他会给你生成10*1的图片.

生成缩略图只是image_filter功能中的一个,它一共支持4种参数:
test:返回是否真的是图片
size:返回图片长短尺寸,返回json格式数据
corp:截取图片的一部分,从左上角开始截取,尺寸写小了,图片会被剪切
resize:缩放图片,等比例缩放

nginx生成缩略图优缺点
优点:
1、??? 根据传入参数即可生成各种比例图片
2、??? 不占用任何硬盘空间

缺点:
1、消耗CPU,访问量大将会给服务器带来极大的负担.

几点建议:
1、??? 生成缩略是个消耗cpu的操作,如果访问量比较大的站点,最好考虑使用程序生成缩略图到硬盘上,或者在前端加上cache或者使用CDN。

github:


 

location /resize {

alias /tmp/nginx/resize;

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:8080/$image_uri;

break;

}

?

proxy_store /tmp/nginx/resize$demins/$image_path;

proxy_store_access user:rw group:rw all:r;

proxy_temp_path /tmp/images;

proxy_set_header Host $host;

}

?

location /image_resize {

alias /path/to/media/;

image_filter resize $arg_width $arg_height;

image_filter_jpeg_quality 75;

allow 127.0.0.0/8;

deny all;

}

?

又一篇:

现在随着各终端的出现(手机,ipad等平板),以及各种终端的手机分辨率和尺寸都不同,现在手机用户流量都是宝,网上出现了各种各样的生成缩略图功能的架构,有使用php实时生成缩略图的,

也有用nginx + lua实现的,上节我也讲到了使用nginx生成缩略图,但是用户每次访问都需要生成一次,会给cpu和硬盘带来比较大的压力,今天带来了另外一种方式,

这次使用nginx将原图生成缩略图到硬盘上.看我的配置

1. 首先建好cache目录

?

1

# mkdir /data/site_cache/

2. 修改nginx配置

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_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/;

image_filter resize $arg_width $arg_height;

image_filter_jpeg_quality 75;

access_log off;

}

生成缩略图流程如下:
1、原图在www.ttlsa.com/image/1.jpg。我需要一份100×100的缩略图。
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生成缩略图到硬盘上的功能到这里就结束了
转载请注明出处: http://www.ttlsa.com/html/2355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值