nginx层使用http_image_filter_module模块:

http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时缩放图片,旋转图片,验证图片有效性以及获取图片宽高以及图片类型信息,由于是即时计算的结果,所以网站访问量大的话,不建议使用。

我们的业务访问量也很大,但是用到了cdn,即访问顺序是cdn->nginx->图片服务器,cdn会回源访问nginx,nginx动态生成指定分辨率的图片后,下次用户再访问此分辨率图片时,cdn上已经有了缓存,会大大减少并发动态生成缩略图的访问量。


#图片自动缩略图
server {
        listen       803;
       server_name  localhost ;
       access_log  off ;
       client_max_body_size 100M;


        location / {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            #proxy_set_header Host "m.isoftstone-gz.com";
            #proxy_pass     http://210.22.27.210:8139;
            root   /data/apps/static;
            index  index.html index.htm;
        }
      location ~* /image_data/(.*)_(\d+)x(\d+)\.(jpg|gif|png)$ {
            root /data/apps/static;
            set $s $1;
            set $w $2;
            set $h $3;
            set $type $4;
            #default_type image/jpeg;
            image_filter resize $w $h;
            #image_filter crop $w $h;
            image_filter_buffer 10M;
            image_filter_jpeg_quality 85;
            image_filter_interlace on;            # 将jpeg图片转换为可以渐进式加载的格式,这样用户可以尽快看到图片效果
            image_filter_transparency on;         # 是否保留图片的透明像素,因为我们还有png图,所以这里要打开
            rewrite ^/image_data/(.*)$  /image_data/$s.$type break;
            try_files /image_data/$s.$type /image_data/notfound.jpg;
        }

    }



================================================

 thumbnail.gddlh.conf

#图片自动缩略图
server {
        listen       803;
       server_name  localhost ;
       access_log  off ;
       client_max_body_size 100M;


        location / {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            #proxy_set_header Host "m.isoftstone-gz.com";
            #proxy_pass     http://210.22.27.210:8139;
            root   /data/apps/static;
            index  index.html index.htm;
        }
      location ~* /image_data/(.*)_(\d+)x(\d+)\.(jpg|gif|png)$ {
            root /data/apps/static;
            set $s $1;
            set $w $2;
            set $h $3;
            set $type $4;
            #default_type image/jpeg;
            image_filter resize $w $h;
            #image_filter crop $w $h;
            image_filter_buffer 10M;
            image_filter_jpeg_quality 85;
            image_filter_interlace on;            # 将jpeg图片转换为可以渐进式加载的格式,这样用户可以尽快看到图片效果
            image_filter_transparency on;         # 是否保留图片的透明像素,因为我们还有png图,所以这里要打开
            rewrite ^/image_data/(.*)$  /image_data/$s.$type break;
            try_files /image_data/$s.$type /image_data/notfound.jpg;
        }

    }


===================================

#图片缓存配置

proxy_cache_path /data/caches/  levels=1:2 keys_zone=Zone:10m inactive=1m max_size=30g;

server {
        listen       80;
        server_name  img.gddlh.com ;
       access_log  off ;
       client_max_body_size 100M;

       #静态文件缓存 proxy_stroe
       location ~ .*\.(html|htm|css|js|ico|jpeg|git|jpg|png|bmp|swf)$ {
        expires 1d;
        root /data/caches/temp/;
        proxy_store on;
        proxy_store_access user:rw group:rw all:rw;
        proxy_temp_path /data/caches/temp/;
        #proxy_set_header Accept-Encoding '';
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        if (!-e $request_filename) {
                proxy_pass http://localhost:803;
        }
      }

      #动态文件缓存 proxy_cache
        location ~ .*$ {
            proxy_cache Zone;
            proxy_cache_valid  200 206 304 301 302 10d;
            proxy_cache_key $uri;
            proxy_set_header Host  $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:803;

        }

    }