笔记|nginx image_filter 配置记录

nginx_image_filter

http_image_filter_module 配置

第一种

http://nginx.org/en/docs/http/ngx_http_image_filter_module.html#image_filter_webp_quality

#官方配置
location /img/ {
    proxy_pass   http://backend;
    image_filter resize 150 100;
    image_filter rotate 90;
    error_page   415 = /empty;
}

location = /empty {
    empty_gif;
}

第二种:裁剪图片,不存储硬盘 访问

http://xxx.com/fanfan_11.jpg@100w_100h_75Q_r http://xxx.com/fanfan.jpg@150w_100h_75Q_r http://xxx.com/img/fanfan.jpg@11w_11_h_80Q_r

# 等比缩放图片
location ~ (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_r$ {
    set $w $3; #宽
    set $h $4; #高
    set $q $5; #图片质量
    image_filter resize $w $h;
    image_filter_jpeg_quality $q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}
# 裁剪图片
location ~ (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_c$ {
    set $w $3; #宽
    set $h $4; #高
    set $q $5; #图片质量
    image_filter crop $w $h;
    image_filter_jpeg_quality $q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}

第三种:保存在磁盘 访问

http://xxx.com/image_filter/222.jpg@120w_120h_75Q_r 代理到 xxx.com/image-resize/image_filter/222.jpg?w=200&h=200&q=75

location ~ (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ {
    
    # 方便调试
    error_log  /usr/local/var/logs/nginx/xxx.com.imagefilter.error.log  debug;

    # 限制referer,防盗链
    # valid_referers xxx.com;#domain modify
    # if ($invalid_referer) {return 404;}
 
    set $w $3; #宽
    set $h $4; #高
    set $q $5; #图片质量
    set $type $6;
    set $image_path  $1.$2; #真实图片地址
    set $cache_path  $1_$3w_$4h_$5Q_$6.$2;  #临时文件地址
    if ($type = 'r') {
        set $type 'image-resize';
    }
    if ($type = 'c') {
        set $type 'image-crop';
    }
    set $image_uri  /$type$image_path?w=$w&h=$h&q=$q;
    if (-f $document_root$cache_path) {
        rewrite (.+)\.(jpg|gif|png)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
        break;
    }
    if (!-f $document_root$cache_path) {
        # proxy_pass http://$server_name.$image_uri;
        # 必须填写ip,填写域名的话,nginx在启动的时候会检测域名,解析DNS,启动后,在修改域名的解析是不生效的
        # 实际上,本机填写域名报500不生效,估计是DNS设置不对,会在server下添加
        # resolver 127.0.0.1 valid=300s;

        proxy_pass http://127.0.0.1$image_uri;
        break;
    }
    proxy_store $document_root$cache_path;
    proxy_store_access user:rw group:rw all:r;
    proxy_temp_path  /tmp/images;
    proxy_set_header Host $host;
    expires  10d; # 设置图片过期时间10天
}
location ~ /image-resize(.+)\.(jpg|gif|png) {
    rewrite /image-resize(.+)\.(jpg|gif|png)$ $1.$2 break;
    image_filter resize $arg_w $arg_h;
    image_filter_jpeg_quality $arg_q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}
location ~ /image-crop(.+)\.(jpg|gif|png) {
    rewrite /image-crop(.+)\.(jpg|gif|png)$ $1.$2 break;
    image_filter crop $arg_w $arg_h;
    image_filter_jpeg_quality $arg_q;
    image_filter_buffer 5M;
    try_files $1.$2 /img/notfound.jpg;
}

海拔科技

自媒体人,喜欢网络,热爱研究。本站头条号:星河 熊掌号:海拔科技

相关推荐

Yum:[Errno 5] [Errno 2] No such file or directory

yum安装软件时,可以连接yum 仓库,但是下载安装时失败,报错如下 这个是因为系统之前升级过python,原来版本是2.7,升级python3后,yum 调用python找不到2.7的版本了 有两个配置文件需要修改 分别将文件开头的 …

Linux下使用rsync进行单台不同目录增量备份

网络上rsync的教程基本上是基于多台服务器(2台以上)进行讲解的,今天我这里有个应用场景,因为先前使用了WebDAV挂载了个本地目录,所以就想着使用rsync进行同一台服务器不同目录增量备份。 安装rsync centos7 …