Score:1

Is it possible to clear the cache of a specific url with NGINX?

pe flag

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.

Score:0
us flag

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.

drookie avatar
za flag
Nah, `reload` is not needed at all.
us flag
I like to keep my logs clean...
jose cervantes avatar
pe flag
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? Do you have any example?
Score:0
za flag

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 %

jose cervantes avatar
pe flag
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?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.