1

0

I am using NGINX as a reverse server, but I have a problem when making changes to a page of my website, the changes are not seen until I clean the entire NGINX cache, causing it to clean my entire site and not just in the URL where I am working.

So I would like to know if it is possible to only clear the cache of a specific URL.

I have configured the dynamic content part but the page load exceeds 600ms and when it is only static content it loads in 200ms.

I used this command line in my terminal but it didn't work either

grep -lr 'https://example.com/blog/name-of-blog.php$' /var/cache/nginx/*

Also I already tried to configure from the .htaccess file and it has not gone as I expected.

I hope you can help me.

2 Answers2

0

In current nginx versions the cache file has a line starting with KEY: , which holds the key for the object stored in the cache file.

The default value of key is documented in nginx documentation, and it is

$scheme$proxy_host$request_uri

From nginx variables documentation we can see that $request_uri is the full request URI with arguments.

If your request is exactly for https://example.com/blog/name-of-blog.php, then your command above should find the correct file.

However, if the URL contains query arguments, those need to be included in the search string, or you need to remove the $ to do a prefix match.

Notice also that nginx still tries to access the cached version of the file after deletion, and it will emit log messages when it cannot open the cached file.

Therefore it is useful to reload nginx process after deleting files from the cache.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

Yeah, you're just doing it wrong - using overcomplicated pattern. The cache key that the entry will be stored as depends on the value of proxy_cache_key variable, but the easy way is not to try to calculate it manually, but rather just looking into the actual cachefiles in your cache tree, correct your search pattern and to search properly.

Looking at your grep keys - I'd say that

  • you're using regexps but missing -e
  • furthermore, you probably don't need regexps at all

I'd start with searching for the vhost name and then narrowing down the search to the actual URI if you need it.

And then something like

grep -R <the pattern you figured out> /var/cache/nginx | awk '{print $1}' | xargs -I % rm %

drookie
  • 8,625
  • 1
  • 19
  • 29
  • Hello, I have tried to follow your recommendations but I have not understood them 100%, I have little to install NGINX in my WHM and I understand very little of this reverse server and its configuration. Can you guide me a little more please? – jose cervantes Oct 20 '22 at 04:10