Linux

笔记|nginx image_filter 配置记录

微信扫一扫,分享到朋友圈

笔记|nginx image_filter 配置记录
0

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/[email protected]_100h_75Q_r http://xxx.com/[email protected]_100h_75Q_r http://xxx.com/img/[email protected]_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/[email protected]_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;
}
自媒体人,喜欢网络,热爱研究。本站头条号:星河 熊掌号:海拔科技

畅捷POS机怎么设置打印一张小票?

上一篇

中付支付电签EPOS入网/代理政策介绍

下一篇

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片