Use Nginx as the cache server and delete its cache files

  • 2020-05-14 05:42:07
  • OfStack

Use nginx for cache servers

The requirement is to cache the android package with the suffix apk. Do not say more, 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  The 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;
  
  # Timeout time for a connection to the back-end server _ Initiate handshake wait response timeout 
  proxy_connect_timeout 600;
  # After successful connection _ Time to wait for a response from the backend server _ In fact, has entered the back-end queue waiting for processing 
  proxy_read_timeout 600;
  # Backend server data return time _ This means that the back-end server has to transfer all the data within a specified time 
  proxy_send_timeout 600;
  # Proxy requests the cache area _ This cache interval will hold the user's header information for consumption Nginx Rule processing _1 As long as you can save the header information 
  proxy_buffer_size 16k;
  # Same as above   tell Nginx Save several for a single use Buffer How much space do I need 
  proxy_buffers 4 64k;
  # If the system is busy, you can apply for a larger one proxy_buffers  The official recommendation *2
  proxy_busy_buffers_size 128k;
  #proxy Cache the size of the temporary file 
  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
# Used to specify a local directory to buffer large proxy requests 
proxy_temp_path /data/soft/temp;
# Set up the web Cache name cache_one, The size of the memory cache space is 12000M , automatically clear over 15 Day has not been accessed cache 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 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;

     # Reverse proxy to access the back-end 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;

     # If the back-end server returns 500 , 502 , 503 , 504 Errors such as execution timeout, automatically forward the request to upstream Another in the load balancing pool 1 A server to achieve failover 
     proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;

     # use web buffer cache_one
     proxy_cache cache_one;

     # For different HTTP The status code cache sets different cache times 
     proxy_cache_valid 200 304 12h;

     # Set up the Web The cache Key Value, Nginx According to the Key value md5 Hash store cache, here according to " The domain name, URI , parameters, " Combined into key
     proxy_cache_key $uri$is_args$args;


     # Reverse proxy to access the back-end 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;
   }
}


Note: the configuration of location/this match is redundant, because the software package location ~*.*\.(apk)$with the suffix apk has already been matched, so location/will not be available.


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 

Delete the nginx cache file
1: the script

a shell version


#!/bin/bash

#Date: 2013-06-27
#Auther: budong

#######################################################
# Description: 
#  1. This script is used for cleanup nginx Cache file 
#  2. Look at your nginx Based on what key to hash My Settings are  proxy_cache_key $uri$is_args$args;
#   so nginx Depending on the $uri$is_args$args As a key for hash , so it can be simulated nginx right 1 a key For then 
#  hash Find the corresponding file path and delete (you can find a cache file at will)  more 1 See under )
#  3. Cache Settings  proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
#   Please modify the test according to the corresponding configuration 
#  4.uri Please follow the format in the same category rm_apk_list.txt Fill in 
#####################################################

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 version 

#!/usr/local/python2.7/bin/python2.7
# -*- coding:utf8 -*-

#Date: 2013-06-26
#Name: budong

'''
 Description: 
  1. This script is used for cleanup nginx Cache file 
  2. Look at your nginx Based on what key to hash My Settings are  proxy_cache_key $uri$is_args$args;
   so nginx Depending on the $uri$is_args$args As a key for hash , so it can be simulated nginx right 1 a key For then 
  hash Find the corresponding file path and delete (you can find a cache file at will)  more 1 See under )
  3. Cache Settings  proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g;
   Please modify the test according to the corresponding configuration 
  4.uri Please follow the format in the same category rm_apk_list.txt Fill in 
'''
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 is not considered and is said to have stopped development

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 look at a cache object, you get the idea


[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


Related articles: