nginx image_filter 配置記錄


nginx_image_filter 

http_image_filter_module
配置

----------------------------------
第一種:

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

location = /empty {
    empty_gif;
}

http://nginx.org/en/docs/http/ngx_http_image_filter_module.html#image_filter_webp_quality
-----------------------------------
第二種:

//裁剪圖片,不存儲硬盤
訪問 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;
}


https://blog.wangjunfeng.com/archives/669

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM