侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

使用Nginx作缓存服务器以及删除其缓存文件的方法

linux  /  管理员 发布于 7年前   457

使用nginx做cache服务器

需求就是缓存android的软件包,后缀名是apk。话不多说,直接上配置,供参考:

a-->nginx.confuser 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天没有被访问过的缓存数据,硬盘缓存空间大小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;     #反向代理,访问后端内容源服务器     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/localulimit -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 linedo  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 $filepathdone < /root/sbin/rm_apk_list.txtb 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 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,'/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.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


  • 上一条:
    Nginx服务器的安装与一些基本配置总结
    下一条:
    mybatis collection 多条件查询的实现方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客