nginx proxy_cache batch cache clearing script introduction

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as CDN static cache before and found that it was a big problem to clear the cache in this module. It can only clear URLs one by one. For this reason, I wrote a script to clear the cache in batches.

Key features include:

1. Clean by file type

2. Clean up by specific file name

3. Clean up by website directory

The following is the bash shell script code. There are many similar scripts on the Internet before, but they are not very good and can easily mislead people to make mistakes.

#!/bin/bash
#Email:[email protected]
#Auto Clean Nginx Proxy_Cache Shell Scripts
#Aunthor:sun~shell
#Date:2017-02-23
echo -e "\n\n"
echo -n -e "\e[35;1mPlease enter the specific path of Nginx Proxy_cache cache (Friendly reminder: You can use the Tab completion function!)\e[0m\e[34;5m:\e[0m"
read -e path
CACHE_DIR=$path
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -n -e "\e[32;1mPlease enter the action you want to delete\n1. Delete by file type\t2. Delete by specific file name\t3. Delete by file directory\n:"
read action
   case $action in
1)
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -n -e "\e[34;1m Please enter the cache file type you want to delete (you can enter multiple parameters separated by spaces)\e[0m\e[34;5m:\e[0m"
read -a FILE
for i in `echo ${FILE[*]}|sed 's/ /\n/g'`
do
grep -r -a \.$i ${CACHE_DIR}| awk 'BEGIN {FS=":"} {print $1}' > /tmp/cache_list.txt
 for j in `cat /tmp/cache_list.txt`
do
  rm -rf $j
  echo "$i $j deleted successfully!"
 done
done
;;
2)
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -n -e "\e[33;1m Please enter the specific name of the cache file you want to delete (you can enter multiple parameters separated by spaces)\e[0m\e[34;5m:\e[0m"
read -a FILE
for i in `echo ${FILE[*]}|sed 's/ /\n/g'`
do
grep -r -a $i ${CACHE_DIR}| awk 'BEGIN {FS=":"} {print $1}' > /tmp/cache_list.txt
 for j in `cat /tmp/cache_list.txt`
do
  rm -rf $j
  echo "$i $j deleted successfully!"
 done
done
;;
3)
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -e "\e[32;1m----------------------------------------------------------------\e[0m"
echo -n -e "\e[33;1mSupported modes are:\n1. Clear all caches under the store directory of the website: test.dd.com/data/upload/shop/store\n2. Clear all caches under the shop directory of the website: test.dd.com/data/upload/shop\e[0m\n"
echo -n -e "\e[34;1m Please enter the specific directory of the cache file you want to delete\e[0m\e[34;5m:\e[0m"
read -a FILE
for i in `echo ${FILE[*]}|sed 's/ /\n/g'`
do
grep -r -a "$i" ${CACHE_DIR}| awk 'BEGIN {FS=":"} {print $1}' > /tmp/cache_list.txt
 for j in `cat /tmp/cache_list.txt`
do
  rm -rf $j
  echo "$i $j deleted successfully!"
 done
done
;;
*)
echo "Input error, please re-enter"
;;
esac

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to hide the version number and web page cache time in Nginx
  • Detailed explanation of setting resource cache in nginx
  • How to set up static files on the nginx cache server
  • How to handle Nginx and browser cache
  • Nginx cache configuration example

<<:  How to use VUE and Canvas to implement a Thunder Fighter typing game

>>:  Tutorial on using prepare, execute and deallocate statements in MySQL

Recommend

Vue's new partner TypeScript quick start practice record

Table of contents 1. Build using the official sca...

Use of Vue3 pages, menus, and routes

Table of contents 1. Click on the menu to jump 1....

Analysis of the difference between Mysql InnoDB and MyISAM

MySQL supports many types of tables (i.e. storage...

Detailed explanation of the basic use of react-navigation6.x routing library

Table of contents react-native project initializa...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

How to find and delete duplicate records in MySQL

Hello everyone, I am Tony, a teacher who only tal...

JavaScript timer to achieve seamless scrolling of pictures

This article shares the specific code of JavaScri...

Analysis of the principle of using PDO to prevent SQL injection

Preface This article uses pdo's preprocessing...

How to construct a table index in MySQL

Table of contents Supports multiple types of filt...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

How to delete a MySQL table

It is very easy to delete a table in MySQL, but y...

MySQL Error 1290 (HY000) Solution

I struggled with a problem for a long time and re...

Detailed explanation of Javascript event capture and bubbling methods

Table of contents 1. Event Processing Model 1. Ev...