這篇文章主要介紹了使用Nginx作緩存服務器以及刪除其緩存文件的方法,作cache時需要注意一下磁盤的IO瓶頸,需要的朋友可以參考下
使用nginx做cache服務器
需求就是緩存android的軟件包,后綴名是apk。話不多說,直接上配置,供參考:
- 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 文件類型
- #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;
- #同上 告訴Nginx保存單個用的幾個Buffer最大用多大空間
- proxy_buffers 4 64k;
- #如果系統很忙的時候可以申請更大的proxy_buffers 官方推薦*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
- #用于指定本地目錄來緩沖較大的代理請求
- proxy_temp_path /data/soft/temp;
- #設置web緩存區名為cache_one,內存緩存空間大小為12000M,自動清除超過15天沒有被訪問過的緩存數據,硬盤緩存空間大小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 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;
- #反向代理,訪問后端內容源服務器
- 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執行超時等錯誤、自動將請求轉發到upstream負載均衡池中的另一臺服務器,實現故障轉移
- 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 200 304 12h;
- #設置Web緩存的Key值,Nginx根據Key值md5哈希存儲緩存,這里根據"域名、URI、參數"組合成key
- proxy_cache_key $uri$is_args$args;
- #反向代理,訪問后端內容源服務器
- 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;
- }
- }
說明:其實按這個配置location /這個匹配是多余的,因為過來一個后綴名為apk的軟件包location ~* .*/.(apk)$已經給匹配上了,不會再到location /了,不過由于我們還會緩存些其他后綴名的文件,所以location /就是必須的。
- c-->/etc/rc.local
- #!/bin/sh
- #
- # 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/local
- ulimit -HSn 65535
- /usr/local/nginx/sbin/nginx
刪除nginx緩存文件
一:腳本
a shell版
- #!/bin/bash
- #Date: 2013-06-27
- #Auther: budong
- #######################################################
- #說明:
- # 1.本腳本用于清除nginx緩存文件
- # 2.查看你的nginx是根據什么作為key來hash的,我的設置是 proxy_cache_key $uri$is_args$args;
- # 因此nginx會根據$uri$is_args$args作為key進行hash,因此可以模擬nginx對一個key進行再
- # hash找到相應的文件路徑,刪除(具體可隨意找個緩存文件 more 一下看看)
- # 3.緩存設置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
- # 根據相應的配置,請做相應修改測試
- # 4.uri格式請按照同級目錄下rm_apk_list.txt中填寫
- #####################################################
- while read -r line
- do
- md5uri=`echo -n $line | md5sum | awk '{ print $1 }'`
- filepath=`echo "$md5uri" | awk '{print "/data/mumayi/cache/"substr($0,length($0),1)"/"substr($0,length($0)-2,2)"/"$0}'`
- rm -rf $filepath
- done < /root/sbin/rm_apk_list.txt
- b python版
- #!/usr/local/python2.7/bin/python2.7
- # -*- coding:utf8 -*-
- #Date: 2013-06-26
- #Name: budong
- '''
- 說明:
- 1.本腳本用于清除nginx緩存文件
- 2.查看你的nginx是根據什么作為key來hash的,我的設置是 proxy_cache_key $uri$is_args$args;
- 因此nginx會根據$uri$is_args$args作為key進行hash,因此可以模擬nginx對一個key進行再
- hash找到相應的文件路徑,刪除(具體可隨意找個緩存文件 more 一下看看)
- 3.緩存設置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
- 根據相應的配置,請做相應修改測試
- 4.uri格式請按照同級目錄下rm_apk_list.txt中填寫
- '''
- import os
- import sys
- try:
- from hashlib import md5
- except:
- from 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不做考慮,據說已經停止開發了
說明:
1 我的 /root/sbin/rm_apk_list.txt 文件
- [root@budong ~]# cat /root/sbin/rm_apk_list.txt
- /2013/08/15/38/382272/shuazanzhushou_V1.8.16_mumayi_95a91.apk
2 查看一個緩存對象,應該有些明白了吧
- [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 200 OK
- Server: nginx/1.2.6
- Date: Sun, 15 Dec 2013 19:51:22 GMT
- Content-Type: application/vnd.android.package-archive
- Content-Length: 37466293
- Connection: close
- Last-Modified: Wed, 23 Oct 2013 06:15:06 GMT
- Expires: Wed, 18 Dec 2013 17:35:07 GMT
- Cache-Control: max-age=604800
- Accept-Ranges: bytes
1 2 3 4 5 6 7 8 9 10 11 |
|
新聞熱點
疑難解答