Use Nginx as a caching server and a way to delete its cached files _nginx

Source: Internet
Author: User
Tags hash md5

Use Nginx to do cache server

The requirement is to cache the Android package, with the suffix name apk. Words not much said, directly on the configuration for reference:

a-->nginx.conf user www www.
Worker_processes 8;
Error_log/data/logs/nginx_error.log Crit;
Pid/usr/local/nginx/nginx.pid;

Worker_rlimit_nofile 204800;
 events {use Epoll;
Worker_connections 204800;
  } http {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;
  #跟后端服务器连接的超时时间_发起握手等候响应超时时间 Proxy_connect_timeout 600;
  #连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理 Proxy_read_timeout 600;
  #后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据 Proxy_send_timeout 600;
  #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可 Proxy_buffer_size 16k; #同上 Tell NGinx save a single use of several buffer maximum space proxy_buffers 4 64k;
  #如果系统很忙的时候可以申请更大的proxy_buffers officially recommended *2 proxy_busy_buffers_size 128k;

  #proxy缓存临时文件的大小 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:80 weight=7 max_fails=2 fail_timeout=30s;
Server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s; } b-->cache.peiqiang.net.conf #用于指定本地目录来缓冲The larger agent request proxy_temp_path/data/soft/temp; #设置web缓存区名为cache_one, memory cache space size of 12000M, automatically clear over 15 days without access to cached data, hard disk cache space size 200g proxy_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 isn't 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;
     Proxy_cache Cache_one;
     Proxy_cache_valid 304 12h;

     Proxy_cache_key $uri $is_args$args;
     #反向代理, 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 302 404 = @fallback; #如果后端的服务器返回500, 502, 503, 504 execution timeout errors, automatically forwarding requests to another server in the upstream load balancing pool, failover Proxy_next_upstream http_500 HTtp_502 http_503 http_504 error timeout invalid_header;

     #使用web缓存区cache_one Proxy_cache Cache_one;

     #对不同的HTTP状态码缓存设置不同的缓存时间 proxy_cache_valid 304 12h;


     #设置Web缓存的Key值, nginx the hash storage cache according to the key value, which is combined into the key Proxy_cache_key $uri $is_args$args According to the "Domain name, URI, parameter" MD5;
     #反向代理, 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;

 }
}


Description: In fact Press this configuration location/This match is superfluous, because come over a suffix named APK package location ~*. *\. (APK) $ has been given to match, will not go to location/, but because we will also cache some other suffix name files, so location/is necessary.

C-->/etc/rc.local
#!/bin/sh # This script would be executed *after* all the other
init scripts.
# You can put your own initialization stuff in-if you don ' t # want to do the full
Sys V style init stuff.

Touch/var/lock/subsys/local
ULIMIT-HSN 65535
/usr/local/nginx/sbin/nginx 

Delete Nginx cache file
One: script

A shell version

#!/bin/bash #Date: 2013-06-27 #Auther: Budong ####################################################### #说明: # 1. This script is used to clear
Nginx Cache File # 2. See your nginx is based on what as key to hash, my settings are Proxy_cache_key $uri $is_args$args; # so Nginx will be based on the $uri$is_args$args as a key hash, so you can simulate nginx to a key for a hash to find the corresponding file path, delete (specific can find a cache file more see) # 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 make the corresponding modification test # 4.uri format please fill in the rm_apk_list.txt in the same level directory ##################################################### while Read-r line do md5uri= ' Echo-n $line | md5sum | awk ' {print '} ' filepath= ' echo ' $md 5uri ' | awk ' {print '/data/mumayi/cache/' substr ($0,length ($), 1) "/" Substr ($0,length ($ -2,2) "/" $} "Rm-rf $filepath done </root/sbin/rm_apk_list.txt b 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 Nginx cache file 2. See your nginx is based on what as key to hash, my settings are Proxy_cache_key $uri $is_args$args; So NginX will be based on $uri$is_args$args as a key hash, so you can simulate nginx to a key to hash to find the corresponding file path, delete (specific can find a cache file more to see) 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 make the corresponding modification test 4.uri format please follow the rm_apk_list.txt in the "Import OS import sys try:from hashlib import MD5 Except:fro 
M MD5 import MD5 reload (SYS) sys.setdefaultencoding (' utf-8 ') Project_root = Os.path.dirname (Os.path.abspath (__file__)) Uri_file = '. Join ([Project_root, '/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 do not consider, it is said to have ceased to develop

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 cached object, you should know something.

[Root@budong ~]# more/data/mumayi/cache/0/00/db9327b60a6b3c164516117f90d9d000 

KEY:/2013/10/23/43/432816/dinuochongwudinopets_v1.1.1_mumayi_0b399.apk
http/1.1 OK
server:nginx/ 1.2.6
Date:sun, Dec 2013 19:51:22 GMT
content-type:application/vnd.android.package-archive
content-length:37466293
connection:close
last-modified:wed, Oct 2013 06:15:06 GMT
expires:wed, D EC 2013 17:35:07 GMT
cache-control:max-age=604800
accept-ranges:bytes

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.