首页 运维 nginx 如何使用Nginx作缓存服务器及删除其缓存文件

如何使用Nginx作缓存服务器及删除其缓存文件

May 18, 2023 pm 06:49 PM
nginx

使用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

&#39;&#39;&#39;
说明:
  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中填写
&#39;&#39;&#39;
import os
import sys
try:
  from hashlib import md5
except:
  from md5 import md5

reload( sys )
sys.setdefaultencoding(&#39;utf-8&#39;)

project_root = os.path.dirname(os.path.abspath(__file__))
uri_file = &#39;&#39;.join([project_root,&#39;/rm_apk_list.txt&#39;])

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,&#39;r&#39;) 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
登录后复制

以上是如何使用Nginx作缓存服务器及删除其缓存文件的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

怎么查看nginx是否启动 怎么查看nginx是否启动 Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么查版本 nginx怎么查版本 Apr 14, 2025 am 11:57 AM

可以查询 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打开 Nginx 错误页,查看页面的标题。

nginx怎么配置云服务器域名 nginx怎么配置云服务器域名 Apr 14, 2025 pm 12:18 PM

在云服务器上配置 Nginx 域名的方法:创建 A 记录,指向云服务器的公共 IP 地址。在 Nginx 配置文件中添加虚拟主机块,指定侦听端口、域名和网站根目录。重启 Nginx 以应用更改。访问域名测试配置。其他注意事项:安装 SSL 证书启用 HTTPS、确保防火墙允许 80 端口流量、等待 DNS 解析生效。

docker容器名称怎么查 docker容器名称怎么查 Apr 15, 2025 pm 12:21 PM

可以通过以下步骤查询 Docker 容器名称:列出所有容器(docker ps)。筛选容器列表(使用 grep 命令)。获取容器名称(位于 "NAMES" 列中)。

nginx在windows中怎么配置 nginx在windows中怎么配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

怎么启动nginx服务器 怎么启动nginx服务器 Apr 14, 2025 pm 12:27 PM

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

docker怎么创建容器 docker怎么创建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中创建容器: 1. 拉取镜像: docker pull [镜像名] 2. 创建容器: docker run [选项] [镜像名] [命令] 3. 启动容器: docker start [容器名]

docker怎么启动容器 docker怎么启动容器 Apr 15, 2025 pm 12:27 PM

Docker 容器启动步骤:拉取容器镜像:运行 "docker pull [镜像名称]"。创建容器:使用 "docker create [选项] [镜像名称] [命令和参数]"。启动容器:执行 "docker start [容器名称或 ID]"。检查容器状态:通过 "docker ps" 验证容器是否正在运行。

See all articles