Basic configuration parsing of image caching service on Nginx server _nginx

Source: Internet
Author: User
Tags nginx server

Recently prepared to use Nginx to build a picture server, the fancy is nginx super static file processing capacity.

Because the picture is larger, and the Web server (also Nginx) run separately, although the Web server calls the picture is not a problem, but is a remote call, certainly no local file system so fast, so there is still room for optimization.

Proxy_store
Nginx configuration prior to use

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

The next use of the Nginx Proxy_store module, so that Nginx will get the picture cached in a local directory, the next call directly, (this makes the page hot picture statistics become very easy, hate log analysis of the administrator will certainly like)

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 located in/var/cache/nginx/proxy_temp, which will keep the image server directory structure

Proxy_store no cache expiration, the equivalent of mirroring function, which is both advantages and disadvantages, the advantage is access to fast, the disadvantage is not know which day, the hard drive will be burst, but we can write a find script, timed to clean up the cache OK.

Nginx also has a cache proxy_cache, which is more advanced in design than Proxy_store, uses memory + hard disk caching, and can set cache size and cache expiration.

Proxy_cache
Proxy_cache There is a problem, Proxy_cache cannot clear the specified URL cache, can only set the URL expiration time, but there is a problem, someone will quickly resolve the problem, Nginx Third-party module Ngx_cache_purge can clear the specified URL.

Nginx installation requires the Ngx_cache_purege to be loaded in.

./configure--user=www--group=www--add-module=/root/dxm/nginx/ngx_cache_purge-1.2  


Among them,/root/dxm/nginx/ngx_cache_purge-1.2 for Ngx_cache_purge decompression path (the attachment provides ngx_cache_purge tar packet download)

Now for an example, implement a picture cache:
Say Proxy_tem_path, and Proxy_cache_path must be under the same partition!

Proxy_temp_path/usr/local/nginx/proxy_temp; 

Proxy_cache_path/usr/local/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;  
  Upstream tomcat_server{server 127.0.0.1:8080; 
  } server{listen 192.168.154.128; 
   
  server_name www.wolf.com; Location ~. *\. 
      (gif|jpg|jpeg|png|bmp|swf) $ {Proxy_cache cache_one; 
        Proxy_cache_methods get head POST; 
       Proxy_cache_min_uses 1; 
       Proxy_cache_valid 302 10m; 
      Proxy_cache_valid 404 1m; 
       Proxy_cache_valid any 1m; 
 
     Proxy_cache_key "$host: $server _port$uri$is_args$args"; 
     Proxy_redirect off; 
     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://tomcat_server; } location ~. *\. 
     (JSP) $ {proxy_redirect off; 
     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://tomcat_server; 
    } location ~/purge (/.*) {allow 192.168.154.128; 
      Allow 192.168.154.1; 
      Deny all; 
    Proxy_cache_purge cache_one $host: $server _port$1$is_args$args;  

 }

 
en, static page caching, dynamic request not cached!
&NBSP
Look at the last section of the Purege configuration, which clearly indicates which IP can manually clear the specified URL
 
For example, www.jb51.net/1.jpg can access My Pictures, Then use www.wolfdream.cn/purge/1.jpg  to clear the image cache.

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.