Nginx Image Cache

Source: Internet
Author: User
Recently, we are going to build an image server with nginx, which looks at nginx's powerful static file processing capabilities. Because the image volume is relatively large, it runs separately from the web server (which is also nginx). although it is useless for the web server to call images, it is a remote call, and it is certainly not as fast as the local file system, therefore, there is still room for optimization. Use nginx to configure location ~ * ^. + \. (Js | ico | gif | jpg | jpeg | png | html | htm) $ {log_not

Recently, we are going to build an image server with nginx, which looks at nginx's powerful static file processing capabilities.


Because the image volume is relatively large, it runs separately from the web server (which is also nginx). although it is useless for the web server to call images, it is a remote call, and it is certainly not as fast as the local file system, therefore, there is still room for optimization.


Nginx configuration before use

location ~* ^.+\.(js|ico|gif|jpg|jpeg|png|html|htm)$ {log_not_found off;access_log off;expires 7d;}

Next, we will use the nginx proxy_store module to cache the obtained images in a local directory and directly call the module next time. (this makes statistics on hotspot images on webpages very easy, administrators who hate log analysis must like this)

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ {log_not_found off;expires 7d ;access_log off;proxy_store on;proxy_store_access user:rw group:rw all:rw;if( !-e $request_filename) {proxy_pass  http://img.example.com}}

The default cache path is/var/cache/nginx/proxy_temp, which maintains the directory structure of the image server.


Proxy_store has no cache expiration, which is equivalent to the image function. this is both an advantage and a disadvantage. the advantage is that the access is fast, and the disadvantage is that the hard disk will be cracked one day, but we can write a find script, it is OK to clear the cache regularly.


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.

http {,,,,,proxy_cache_path /var/cache/nginx/cachelevels=1:2 keys_zone=imgcache:100m inactive=2h max_size=1g;server {........location ~* ^.+\.(js|ico|gif|jpg|jpeg|png|html|htm)$ {log_not_found off;access_log off;expires 7d;proxy_pass http://img.example.com ;proxy_cache imgcache;proxy_cache_valid 200 302 1d;proxy_cache_valid 404 1h;proxy_cache_valid any 10m;proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;}}}

The above code will create a M memory cache. if each file is inactive within 2 hours, it will be eliminated into the hard disk cache. the hard disk cache is up to 1 GB, and the cache will be cleared automatically when it is full.


This cache method uses hash classification to store images for faster indexing, and the image directory structure and name are all out of sight. Therefore, statistics on webpage hotspot images, such as logs, must be made through other methods.


The latter method is more like a caching system, with a wider application and better performance.


This article is from the "Purple_Grape blog", please be sure to keep this source http://purplegrape.blog.51cto.com/1330104/1205129


Related Article

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.