Fix lack of exclude check, add usage examples, update examples to use server_name instead of host.

This commit is contained in:
GRMrGecko 2024-08-01 08:08:51 -05:00
parent a1661a4122
commit cc9e7d4f22
2 changed files with 47 additions and 15 deletions

View File

@ -1,5 +1,5 @@
# nginx-cache-purge # nginx-cache-purge
A tool to help purge Nginx cache. It can either run locally with the purge command, or run as a local unix service to allow for purging by Nginx http requests. A tool to help purge Nginx cache. It can either run locally with the purge command, or run as a local unix service to allow for purging by Nginx http requests. The tool supports using wildcard/glob syntax in the purge key to match multiple keys from the cache.
## Install ## Install
You can install either by downloading the latest binary release, or by building. You can install either by downloading the latest binary release, or by building.
@ -10,6 +10,29 @@ Building should be as simple as running:
go build go build
``` ```
## Usage
The following are some examples of ways to purge cache
### Purge a specific key
```
$ nginx-cache-purge purge /var/nginx/proxy_temp/cache example.com/index.html
```
### Purge all keys for a domain
```
$ nginx-cache-purge purge /var/nginx/proxy_temp/cache 'example.com/*'
```
### Purge all keys for jpeg and png files
```
$ nginx-cache-purge purge /var/nginx/proxy_temp/cache 'example.com/*.{jpg,jpeg,png}'
```
### Purge all keys
```
$ nginx-cache-purge purge /var/nginx/proxy_temp/cache '*'
```
## Running as a service ## Running as a service
If you want to run as a service to allow purge requests via http requests, you'll need to create a systemd service file and place it in `/etc/systemd/system/nginx-cache-purge.service`. If you want to run as a service to allow purge requests via http requests, you'll need to create a systemd service file and place it in `/etc/systemd/system/nginx-cache-purge.service`.
``` ```
@ -48,13 +71,13 @@ http {
} }
proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m; proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m;
proxy_cache_key $host$request_uri; proxy_cache_key $server_name$request_uri;
server { server {
location / { location / {
if ($is_purge) { if ($is_purge) {
proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock; proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock;
rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$host$request_uri break; rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$server_name$request_uri break;
} }
proxy_cache my_cache; proxy_cache my_cache;
@ -73,13 +96,13 @@ http {
} }
proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m; proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m;
proxy_cache_key $host$request_uri; proxy_cache_key $server_name$request_uri;
server { server {
location / { location / {
if ($is_purge) { if ($is_purge) {
proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock; proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock;
rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$host$request_uri break; rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$server_name$request_uri break;
} }
proxy_cache my_cache; proxy_cache my_cache;
@ -98,13 +121,13 @@ http {
} }
proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m; proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m;
proxy_cache_key $host$request_uri; proxy_cache_key $server_name$request_uri;
server { server {
location / { location / {
if ($is_purge) { if ($is_purge) {
proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock; proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock;
rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$host$request_uri break; rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$server_name$request_uri break;
} }
proxy_cache my_cache; proxy_cache my_cache;
@ -118,7 +141,7 @@ http {
``` ```
http { http {
proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m; proxy_cache_path /var/nginx/proxy_temp/cache levels=1:2 keys_zone=my_cache:10m;
proxy_cache_key $host$request_uri; proxy_cache_key $server_name$request_uri;
server { server {
location / { location / {
@ -129,7 +152,7 @@ http {
allow 127.0.0.1; allow 127.0.0.1;
deny all; deny all;
proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock; proxy_pass http://unix:/var/run/nginx-cache-purge/http.sock;
rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$host$1 break; rewrite ^ /?path=/var/nginx/proxy_temp/cache&key=$server_name$1 break;
} }
} }
} }
@ -137,7 +160,7 @@ http {
## Help ## Help
``` ```
$ ./nginx-cache-purge --help $ nginx-cache-purge --help
Usage: nginx-cache-purge <command> [flags] Usage: nginx-cache-purge <command> [flags]
Tool to help purge cache from Nginx Tool to help purge cache from Nginx
@ -152,7 +175,7 @@ Commands:
Run "nginx-cache-purge <command> --help" for more information on a command. Run "nginx-cache-purge <command> --help" for more information on a command.
$ ./nginx-cache-purge p --help $ nginx-cache-purge p --help
Usage: nginx-cache-purge purge (p) <cache-path> <key> [flags] Usage: nginx-cache-purge purge (p) <cache-path> <key> [flags]
Purge cache now Purge cache now
@ -167,7 +190,7 @@ Flags:
--exclude-key=EXCLUDE-KEY,... Key to exclude, can be wild card and can add multiple excludes. --exclude-key=EXCLUDE-KEY,... Key to exclude, can be wild card and can add multiple excludes.
$ ./nginx-cache-purge s --help $ nginx-cache-purge s --help
Usage: nginx-cache-purge server (s) [flags] Usage: nginx-cache-purge server (s) [flags]
Run the server Run the server

15
main.go
View File

@ -17,7 +17,7 @@ import (
const ( const (
serviceName = "nginx-cache-purge" serviceName = "nginx-cache-purge"
serviceDescription = "Tool to help purge Nginx cache " serviceDescription = "Tool to help purge Nginx cache "
serviceVersion = "0.1" serviceVersion = "0.1.1"
) )
// App structure to access global app variables. // App structure to access global app variables.
@ -118,11 +118,20 @@ func (a *App) PurgeCache(CachePath string, Key string, ExcludeKeys []string) err
} }
defer file.Close() defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
// Scan file for the key.
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
// If line is the key, check if it matches our glob pattern and delete.
if strings.HasPrefix(line, "KEY: ") { if strings.HasPrefix(line, "KEY: ") {
key := line[5:] keyRead := line[5:]
if g.Match(key) { if g.Match(keyRead) {
// If excluded, skip the key.
if keyIsExcluded(keyRead) {
fmt.Println("Key", keyRead, "is excluded, will not purge.")
return nil
}
// Delete the file.
fmt.Printf("Purging %s as it matches the key %s requested to be purged.\n", filePath, Key) fmt.Printf("Purging %s as it matches the key %s requested to be purged.\n", filePath, Key)
err := os.Remove(filePath) err := os.Remove(filePath)
if err != nil { if err != nil {