溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Nginx作緩存服務(wù)器及刪除其緩存文件

發(fā)布時(shí)間:2022-04-25 15:28:18 來(lái)源:億速云 閱讀:127 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹了如何使用Nginx作緩存服務(wù)器及刪除其緩存文件的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇如何使用Nginx作緩存服務(wù)器及刪除其緩存文件文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

使用nginx做cache服務(wù)器

需求就是緩存android的軟件包,后綴名是apk。話不多說(shuō),直接上配置,供參考:

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;
  
  #跟后端服務(wù)器連接的超時(shí)時(shí)間_發(fā)起握手等候響應(yīng)超時(shí)時(shí)間
  proxy_connect_timeout 600;
  #連接成功后_等候后端服務(wù)器響應(yīng)的時(shí)間_其實(shí)已經(jīng)進(jìn)入后端的排隊(duì)之中等候處理
  proxy_read_timeout 600;
  #后端服務(wù)器數(shù)據(jù)回傳時(shí)間_就是在規(guī)定時(shí)間內(nèi)后端服務(wù)器必須傳完所有數(shù)據(jù)
  proxy_send_timeout 600;
  #代理請(qǐng)求緩存區(qū)_這個(gè)緩存區(qū)間會(huì)保存用戶的頭信息以供nginx進(jìn)行規(guī)則處理_一般只要能保存下頭信息即可
  proxy_buffer_size 16k;
  #同上 告訴nginx保存單個(gè)用的幾個(gè)buffer最大用多大空間
  proxy_buffers 4 64k;
  #如果系統(tǒng)很忙的時(shí)候可以申請(qǐng)更大的proxy_buffers 官方推薦*2
  proxy_busy_buffers_size 128k;
  #proxy緩存臨時(shí)文件的大小
  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
#用于指定本地目錄來(lái)緩沖較大的代理請(qǐng)求
proxy_temp_path /data/soft/temp;
#設(shè)置web緩存區(qū)名為cache_one,內(nèi)存緩存空間大小為12000m,自動(dòng)清除超過(guò)15天沒(méi)有被訪問(wèn)過(guò)的緩存數(shù)據(jù),硬盤緩存空間大小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;

     #反向代理,訪問(wèn)后端內(nèi)容源服務(wù)器
     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;

     #如果后端的服務(wù)器返回500、502、503、504執(zhí)行超時(shí)等錯(cuò)誤、自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移
     proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;

     #使用web緩存區(qū)cache_one
     proxy_cache cache_one;

     #對(duì)不同的http狀態(tài)碼緩存設(shè)置不同的緩存時(shí)間
     proxy_cache_valid 200 304 12h;

     #設(shè)置web緩存的key值,nginx根據(jù)key值md5哈希存儲(chǔ)緩存,這里根據(jù)"域名、uri、參數(shù)"組合成key
     proxy_cache_key $uri$is_args$args;


     #反向代理,訪問(wèn)后端內(nèi)容源服務(wù)器
     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;
   }
}


說(shuō)明:其實(shí)按這個(gè)配置location /這個(gè)匹配是多余的,因?yàn)檫^(guò)來(lái)一個(gè)后綴名為apk的軟件包location ~* .*\.(apk)$已經(jīng)給匹配上了,不會(huì)再到location /了,不過(guò)由于我們還會(huì)緩存些其他后綴名的文件,所以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

#######################################################
#說(shuō)明:
#  1.本腳本用于清除nginx緩存文件
#  2.查看你的nginx是根據(jù)什么作為key來(lái)hash的,我的設(shè)置是 proxy_cache_key $uri$is_args$args;
#  因此nginx會(huì)根據(jù)$uri$is_args$args作為key進(jìn)行hash,因此可以模擬nginx對(duì)一個(gè)key進(jìn)行再
#  hash找到相應(yīng)的文件路徑,刪除(具體可隨意找個(gè)緩存文件 more 一下看看)
#  3.緩存設(shè)置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
#  根據(jù)相應(yīng)的配置,請(qǐng)做相應(yīng)修改測(cè)試
#  4.uri格式請(qǐng)按照同級(jí)目錄下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

'''
說(shuō)明:
  1.本腳本用于清除nginx緩存文件
  2.查看你的nginx是根據(jù)什么作為key來(lái)hash的,我的設(shè)置是 proxy_cache_key $uri$is_args$args;
  因此nginx會(huì)根據(jù)$uri$is_args$args作為key進(jìn)行hash,因此可以模擬nginx對(duì)一個(gè)key進(jìn)行再
  hash找到相應(yīng)的文件路徑,刪除(具體可隨意找個(gè)緩存文件 more 一下看看)
  3.緩存設(shè)置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
  根據(jù)相應(yīng)的配置,請(qǐng)做相應(yīng)修改測(cè)試
  4.uri格式請(qǐng)按照同級(jí)目錄下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不做考慮,據(jù)說(shuō)已經(jīng)停止開發(fā)了

說(shuō)明:

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 查看一個(gè)緩存對(duì)象,應(yīng)該有些明白了吧

[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

關(guān)于“如何使用Nginx作緩存服務(wù)器及刪除其緩存文件”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“如何使用Nginx作緩存服務(wù)器及刪除其緩存文件”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI