nginx实时生成缩略图存储到硬盘上

本文介绍如何利用Nginx服务器配置实现图片缩略图的生成与缓存,通过特定URL请求,Nginx能根据指定尺寸生成缩略图并存储在硬盘上,减轻CPU及硬盘压力。

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

   现在随着各终端的出现(手机,ipad等平板),以及各种终端的手机分辨率和尺寸都不同,现在手机用户流量都是宝,网上出现了各种各样的生成缩略图功能的架构,有使用php实时生成缩略图的,也有用nginx + lua实现的,上节我也讲到了使用nginx生成缩略图,但是用户每次访问都需要生成一次,会给cpu和硬盘带来比较大的压力,今天带来了另外一种方式,这次使用nginx将原图生成缩略图到硬盘上.看我的配置


1、首先创建好cache目录

[root@masterserver ~]# mkdir -p /data/stie_cache    

[root@masterserver ~]# 


      wKiom1bn2ZmjCRdCAAANZU6kfVU558.png

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;

        }

完整的nginx配置文件如下:

[root@masterserver conf]# cat nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

           image on;

          image_output on;

        }

        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;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

[root@masterserver conf]# 


提示:

nginx编译的时候要添加参数--with-http_image_filter_module,保险起见将模块ngx_image_thumb-master也带上,所以最终nginx编译参数为:

 ./configure   --add-module=../ngx_image_thumb-master/ --with-http_image_filter_module

生成缩略图流程如下:

1、原图在http://10.0.0.10/image/1.jpg。我需要一份100x100的缩略图。

2、请求http://10.0.0.10/resize_100x100/image/1.jpg.

3、这个请求进入了location ~* ^/resize,接着判断image_path这个目录下是否存在这张图片,如果存在直接放回给用户,

4、不存在那么跳转到http://10.0.0.10/image_resize/image/1.jpg?width=100&height=100;

5、location /image_resize根据传入的width和height执行缩略功能,并且设置图像质量为75

6、接着生成文件到/data/site_cache/10.0.0.10/resize_100x100/image/1.jpg

7、并且返回图片给用户

8、nginx生成缩略图到硬盘上的功能到这里就结束了





      本文转自027ryan  51CTO博客,原文链接: http://blog.51cto.com/ucode/1751605 ,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值