>Nginx 缩略图 缓存到硬盘上 带后缀 nginx 如何配置

2017-08-27 02:54:22 +08:00
 luoo369
location ~* ^/crop {
                root /home/wwwroot/$server_name/site_cache;
                set $width 240;
                set $height 180;
                set $dimens "";

       
                if ($uri ~* "^/crop_(\d+)x(\d+)/(.*)" ) {
                        set $width $1;
                        set $height $2;
                        set $image_path $3;
                        set $demins "_$1x$2";
                }

                if ($uri ~* "^/crop/(.*)" ) {
                        set $image_path $1;
                }




                set $image_uri image_crop/$image_path?width=$width&height=$height;

                if (!-f $request_filename) {
                        proxy_pass http://127.0.0.1/$image_uri;
                        break;
                }
                proxy_store /home/wwwroot/$server_name/site_cache/crop$demins/$image_path;
                proxy_store_access user:rw group:rw all:r;
                proxy_set_header Host $host;
                expires      30d;
                access_log off;
        }

        location /image_crop {
                alias /home/wwwroot/$server_name/;
                image_filter crop $arg_width $arg_height;
                image_filter_jpeg_quality 75;
                access_log off;
        }

比如访问 http://pics.v2ex.com/wp-content/uploads/465484/1.jpg 的缩略图

使用下面这样的链接可以访问到默认的裁剪为 240x180 的缩略图并缓存到硬盘上

http://pics.v2ex.com/crop/wp-content/uploads/465484/1.jpg

如何这样带后缀访问缩略图 http://pics.v2ex.com/wp-content/uploads/465484/1.jpg_crop

用的七牛的缩略图函数 ,七牛的免费流量超限了,想用自己闲置的 vps 搭建一个图床,弄好了,但是缩略图不会配置,在网上扒了一些资料,搞不定,求大牛该如何添加一个 if 语句

2347 次点击
所在节点    NGINX
1 条回复
luoo369
2017-08-27 17:14:10 +08:00
``` javascript


location ~* (.*\.(jpg|gif|png))/w/(.*)/h/(.*)$ {
root /home/wwwroot/$server_name/site_cache;
set $width $3;
set $height $4;
set $image_path $1;
set $demins "";
set $image_uri image_crop/$image_path?width=$width&height=$height;

if (!-f $request_filename) {
proxy_pass http://127.0.0.1/$image_uri;
break;
}
proxy_store /home/wwwroot/$server_name/site_cache/crop$demins/$image_path;
proxy_store_access user:rw group:rw all:r;
proxy_set_header Host $host;
expires 30d;
access_log off;


}
location /image_crop {
alias /home/wwwroot/$server_name/;
image_filter crop $arg_width $arg_height;
image_filter_jpeg_quality 75;
access_log off;
}

```
>搞定了,换了另外一种方法,访问形式是 xxx.jpg/w/240/h/180

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/386055

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX