How to use Nginx as a cache server and delete its cache files
This article mainly introduces how to use Nginx as a cache server and delete its cache file related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will have something to gain after reading this article on how to use Nginx as a cache server and delete its cache files, let's take a look.
Using nginx as cache server
The requirement is the package that caches android, with the suffix apk. Without saying much, go directly to the configuration for reference:
A http-> nginx.confuser www www;worker_processes 8 client_header_buffer_size errorists log / data/logs/nginx_error.log crit;pid / usr/local/nginx/nginx.pid;worker_rlimit_nofile 204800; events {include mime.types; # apk file type # default_type application/vnd.android.package-archive; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 2k Large_client_header_buffers 4 4k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; tcp_nodelay on; client_body_buffer_size 512k; # timeout for connection to backend server _ initiating handshake waiting for response timeout proxy_connect_timeout 600 # time to wait for a response from the backend server after a successful connection _ actually entered the queue at the backend to process proxy_read_timeout 600; # backend server data return time _ that is, the backend server must pass all the data proxy_send_timeout 600 within a specified time # proxy request cache area _ this cache zone will hold the user's header information for nginx to process rules. Generally, as long as the header information can be saved, you can proxy_buffer_size 16k; # the same as above tells nginx how much space proxy_buffers 464k should be used to save several buffer for a single use; # if the system is busy, you can apply for a larger proxy_buffers official recommendation * 2 proxy_busy_buffers_size 128k # proxy cache temporary file size proxy_temp_file_write_size 128k; gzip on; gzip_proxied expired no-cache no-store private auth; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_disable "msie [1-6]\."; gzip_vary on # log_format access'$remote_addr-$remote_user [$time_local]'#'"$request" $status $body_bytes_sent'#'"$http_referer"$http_user_agent"'#'$host $request_time $http_x_forwarded_for'; # access_log / data/logs/http.a.log; # error_log / data/logs/http.e.log; include vhosts/cache.peiqiang.net.conf } upstream source_site {server 192.168.1.1 weight=4 max_fails=2 fail_timeout=30s; 80 weight=7 max_fails=2 fail_timeout=30s; server 192.168.1.2 weight=4 max_fails=2 fail_timeout=30s;} b color-> cache.peiqiang.net.conf# is used to specify a local directory to buffer larger proxy requests proxy_temp_path / data/soft/temp # set web cache to cache_one, memory cache size to 12000m, automatically clear cache data that has not been accessed for more than 15 days, hard disk cache space size 200gproxy_cache_path / data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g;server {listen 80; server_name cache.peiqiang.net; access_log / data/logs/a.log; error_log / data/logs/e.log notice # php scripts is not allowed within this site! Location ~ *\. (php | php5 | jsp | asp | aspx) ${deny all;} location / {proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $uri$is_args$args; # reverse proxy to access the backend content source server proxy_set_header host $host; proxy_set_header x-forwarded-for $remote_addr Proxy_pass http://source_site;} location ~ *. *\. (apk) ${error_page 302404 = @ fallback; # if the backend server returns 500,502,503,504 execution timeout and other errors, automatically forwards the request to another server in the upstream load balancing pool to achieve failover proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header # use web cache cache_one proxy_cache cache_one; # to set different caching time proxy_cache_valid 200 304 12h for different http status code caches; # set the key value of web cache. Nginx stores the cache according to key value md5 hash. Here, it is combined into key proxy_cache_key $uri$is_args$args according to "domain name, uri, parameter". # reverse proxy to access the backend content source server proxy_set_header host $host; proxy_set_header x-forwarded-for $remote_addr; proxy_pass http://source_site; expires 1d;} location @ fallback {rewrite ^ $scheme://apke.peiqiang.net$uri redirect; expires-1;}}
Note: in fact, it is unnecessary to configure location / this match according to this, because a package with the suffix apk location ~ *. *\. (apk) $has been matched and will not go to location /, but because we will also cache files with other suffixes, location / is a must.
Cmure-> / ETC _ hsn 65535/usr/local/nginx/sbin/nginx rc.localautomobile _ hsn 65535/usr/local/nginx/sbin/nginx _ binram _ this script will be executed * after* all the other init scripts.# you can put your own initialization stuff in here if you don't# want to do the full sys v style init stuff.touch / var/lock/subsys/localulimit-hsn 65535/usr/local/nginx/sbin/nginx
Delete nginx cache files
One: script
A shell version
#! / bin/bash#date: 2013-06-27#auther: budong### description: # 1. This script is used to clear the nginx cache file # 2. To see what your nginx is based on as a key to hash, my setting is proxy_cache_key $uri$is_args$args;# so nginx will hash according to $uri$is_args$args as key, so you can simulate nginx for a key and then # hash to find the corresponding file path and delete (feel free to find a cache file more to have a look) # 3. Cache Settings proxy_cache_path / data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g # according to the corresponding configuration Please modify and test the # 4.uri format. Enter # while read-r linedo md5uri= `echo-n $line in the rm_apk_list.txt in the same directory | md5sum | awk'{print $1} '`filepath= `echo "$md5uri | awk' {print" / data/mumayi/cache/ "substr ($0) Length ($0), 1) "/" substr ($0 rm length ($0)-2) "/" $0}'`rm-rf $filepathdone < / root/sbin/rm_apk_list.txtb python version #! / usr/local/python2.7/bin/python2.7#-*-coding:utf8-*-# date: 2013-06-26#name: budong''' description: 1. This script is used to clear the nginx cache file 2. To see what your nginx is based on as key to hash, my setting is proxy_cache_key $uri$is_args$args; so nginx will hash according to $uri$is_args$args as key, so you can simulate nginx to hash a key to find the corresponding file path and delete (feel free to find a cache file more to have a look) 3. Cache Settings proxy_cache_path / data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g According to the corresponding configuration Please modify the test 4.uri format accordingly. Fill in''import osimport systry: from hashlib import md5except: from md5 import md5reload (sys) sys.setdefaultencoding (' utf-8') project_root = os.path.dirname (os.path.abspath (_ _ file__)) uri_file = '.join ([project_root]) in the same directory. '/ rm_apk_list.txt']) def nginx_purge (uri): M = md5 () m.update ("% s"% uri) md5uri=m.hexdigest () md5uri_len=len (md5uri) dir1= MD5uri [MD5uri _ len-1:] dir2= MD5uri [md5uri _ len-3:md5uri_len-1] file_path= ("/ data/mumayi/cache/%s/%s/%s"% (dir1, dir2) Md5uri) if os.path.exists (file_path): os.remove (file_path) with open ("% s"% uri_file,'r') as uri_file: for line in uri_file: line = line.rstrip () nginx_purge (line)
C ngx_cache_purge doesn't think about it and is said to have stopped developing
Description:
1 my / root/sbin/rm_apk_list.txt file
[root@budong ~] # cat / root/sbin/rm_apk_list.txt / 2013/08/15/38/382272/shuazanzhushou_v1.8.16_mumayi_95a91.apk
2 to view a cache object, you should understand it a little bit.
[root@budong ~] # more / data/mumayi/cache/0/00/db9327b60a6b3c164516117f90d9d000key: / 2013/10/23/43/432816/dinuochongwudinopets_v1.1.1_mumayi_0b399.apkhttp/1.1 200 okserver: nginx/1.2.6date: sun, 15 dec 2013 19:51:22 gmtcontent-type: application/vnd.android.package-archivecontent-length: 37466293connection: closelast-modified: wed, 23 oct 2013 06:15:06 gmtexpires: wed 18 dec 2013 17:35:07 gmtcache-control: max-age=604800accept-ranges: bytes article on "how to use Nginx as a cache server and delete its cache files" ends here Thank you for reading! I believe you all have a certain understanding of "how to use Nginx as a cache server and delete its cache files". If you want to learn more, you are welcome to follow the industry information channel.