Nginx cache Configuration and Ngx_cache_purge summary

Source: Internet
Author: User
Nginx Cache Configuration

first, set the format of a cache log, you can view the "MISS" and "hit" status in the log

Log_format Cache ' * * * $time _local ' * * * * $upstream _cache_status ' ***cache-control: $upstream _http_cache_control ' Expires: $upstream _http_expires "* * *" $request "($status)" * * * "$http _user_agent";

next set the cache parameters

Proxy_connect_timeout 5;

Proxy_read_timeout 60;

Proxy_send_timeout 5;

Proxy_buffer_size 16k;

Proxy_buffers 4 64k;

Proxy_busy_buffers_size 128k;

Proxy_temp_file_write_size 128k;

#注: The path specified by Temp_path and Cache_path must be in the same partition

Proxy_temp_path/cache/temp_dir;

#设置Web缓存区名称为cache_one, the size of the memory cache is automatically cleared for content that is not accessed for 200mb,1 days, and the disk cache space size is 30GB.

Proxy_cache_path/cache/cache levels=1:2 keys_z max_size=30g;

#设定一个upstream

Upstream AppServer {

Server 192.168.10.5;

}

# used to clear the cache, assuming a URL to be Http://192.168.10.3/test.txt , by accessing Http://192.168.10.3/purge/test.txt you can clear the URL the cache. This feature requires manual compilation of the module Ngx_cache_purge, the download address is http://labs.frickle.com/files/, preferably with the latest version, in this case 2.1.

Location ~/purge (/.*) {

# setting allows only the specifiedIp orIp segment to clearUrl Cache.

Allow 127.0.0.1;

Allow 192.168.10.0/24;

Deny all;

Proxy_cache_purge Cache_one $host $1$is_args$args;

}

#注意, this rule must be placed after purge, because the rule is first matched before release, and a 404 error is reported when the cache is cleared.

Location ~. *\. (gif|jpg|png|htm|html|css|js|flv|ico|swf) (.*) {

Proxy_pass Http://appserver;

Proxy_redirect off;

# by domain name,Uri , parameters are combined intoWeb of the cacheKey values,Nginx according toKey value Hash, Store cached content in a level two cache directory

Proxy_cache_key $host $uri$is_args$args;

Proxy_set_header Host $host;

Proxy_set_header x-forwarded-for $remote _addr;

Proxy_cache Cache_one;

# Of the differentHTTP status codes set different cache times

Proxy_cache_valid 302 1h;

Proxy_cache_valid 301 1d;

Proxy_cache_valid any 1m;

Expires 30d;

# you can see it in the Cache.log log again.MISS and theHit status

Access_log/usr/local/nginx/logs/cache.log Cache;

}

configuration complete, reboot Nginx, cannot reload.

random access to a page http://192.168.10.3/9.jpg, the corresponding cache file will also be generated under/cache/cache

View Cache.log, you can see the first visit for Miss, the second for hit

***19/mar/2014:10:48:16 +0800 ***miss * * * Cache-control:-***expires:-* * * * * * * * * * * * * * * * * #后面浏览器信息就省略了
***19/mar/2014:10:48:37 +0800 ***hit * * * Cache-control:-***expires:-* * * * "get/9.jpg http/1.1" (304) * * *

clear cache

Access http://192.168 .10.3/purge/9.jpg, see the following message stating that the cache has been purged.

Successful purge

key:192.168.10.3/9.jpg

Path:/CACHE/CACHE/6/C1/368F9DB143996C9F865921F8B2C94C16

The above describes Ng Inx cache Configuration and Ngx_cache_purge summary, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.