Home >Backend Development >PHP Tutorial >nginx cache configuration and ngx_cache_purge summary

nginx cache configuration and ngx_cache_purge summary

WBOY
WBOYOriginal
2016-07-30 13:30:39977browse
nginx cache configurationFirst set a cache log format, you can view the "MISS" and "HIT" status in the loglog_format cache '***$time_local ' '***$upstream_cache_status ' '***Cache-Control: $upstream_http_cache_control ' '***Expires: $upstream_http_expires ' '***"$request" ($status) ' '***"$http_user_agent" ';Secondly 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;#Note: The paths specified by temp_path and cache_path must be in the same partition proxy_temp_path/cache/temp_dir;#Set the name of the Web cache area to cache_one, the memory cache space size to 200MB, and it has not been accessed for 1 day The content is automatically cleared, and the hard disk cache space is 30GB. proxy_cache_path /cache/cache levels=1:2 keys_z max_size=30g;#Set an upstream
upstream appserver {server 192.168.10.5;}# is used to clear the cache, assuming A URL is http://192.168.10.3/test.txt, which can be cleared by accessing http://192.168.10.3/purge/test.txt Caching. This function requires manual compilation of the module ngx_cache_purge. The download address is http://labs.frickle.com/files/. It is best to use the latest version, which is 2.1 in this example. Location ~ /purge(/.*) {             # Settings only allow the specified IP or IP segment to clear the URL cache. " proxy_cache_purge cache_one $host$1$is_args$args; } #Note that this rule must be placed after purge , because this rule will be matched first before being placed, and a 404 error will be reported when clearing the cache. location ~ .*.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {proxy_pass http://appserver;proxy_redirect off; #Combines the domain name, URI, and parameters to form the Key value of the Web cache, Nginx hashes it based on the Key value, and stores the cached content in the second-level 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;#Set different cache times for different HTTP status codesproxy_cache_valid 200 302 1h;proxy_cache_valid 301 1d;proxy_cache_valid any 1m ;expires 30d;# You can see the MISS and HIT status in the cache.log log access_log/usr/local/nginx/logs/cache.log cache;}The configuration is completed, restart nginx, and cannot reload. Just visit a page http://192.168.10.3/9.jpg, and the corresponding cache file will be generated under /cache/cache
Check cache.log, you can see that the first visit is MISS, the second HIT
***19/Mar/2014:10:48:16 +0800 ***MISS ***Cache-Control: - ***Expires: - *** "GET /9.jpg HTTP/1.1" (200) *** #The browser information will be omitted
***19/Mar/2014:10:48:37 +0800 ***HIT ***Cache- Control: - ***Expires: - ***"GET /9.jpg HTTP/1.1" (304) ***
Clear cacheVisit http://192.168.10.3/purge/9.jpg, Seeing the following information indicates that the cache has been cleared.

Successful purge


Key : 192.168.10.3/9.jpg

Path: /cache/cache/6/c1/368f9db143996c9f865921f8b2c94c16

The above introduces the nginx cache configuration and ngx_cache_purge summary, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn