Nginx image cache processing

Source: Internet
Author: User

Method 1

The code is as follows: Copy code

Server {
#... N rows of configuration are omitted here
# Example:/thumb/export md52.16.png_300x187.png
Location ~ "/Thumb/([0-9a-f] {32 }. (png | jpg | gif) _ (d + |-) x (d + | -). (png | jpg | gif) $ "{
Access_log off;
Set $ image $1;
Set $ width $3;
Set $ height $4;
# We recommend that you use try_files instead of if
Try_files/thumb/$ image = 404;
Image_filter resize $ width $ height;
Image_filter_buffer 10 M;
# It will only be cached on the browser side. If you want to cache on the server side, you can use the proxy solution (this is not provided here, you can google it yourself)
Expires 7d;
  }
}

Nginx also has a kind of cache proxy_cache, which is more advanced than proxy_store in design. It adopts the memory + hard disk cache mode and can set the cache size and cache expiration.

The code is as follows: Copy code

Http {
,,,,,
Proxy_cache_path/var/cache/nginx/cache levels = keys_zone = imgcache: 100 m inactive = 1d max_size = 10g;
Server {
........Www.111cn.net
Location ~ * ^. +. (Js | ico | gif | jpg | jpeg | png | html | htm) $ {
Log_not_found off;
Access_log off;
Expires 7d;
Proxy_pass http://www.111cn.net;
Proxy_cache imgcache;
Proxy_cache_valid 200 302 1d;
Proxy_cache_valid 404 10 m;
Proxy_cache_valid any 1 h;
Proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
        }
    }
}

The above code uses a shared memory of up to MB for the file index on the hard disk, including the file name and the number of requests. If each file is inactive within one day (no request) it is eliminated from the hard disk. The hard disk cache is up to 10 GB. When it is full, the cache is automatically cleared based on the LRU algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.