By Freewind


2011-06-04 10:04:30 8 Comments

I use nginx to as the front server, I have modified the CSS files, but nginx is still serving the old ones.

I have tried to restart nginx, to no success and I have Googled, but not found a valid way to clear it.

Some articles say we can just delete the cache directory: var/cache/nginx, but there is no such directory on my server.

What should I do now?

19 comments

@Sajjad Ashraf 2018-11-19 16:48:39

If you want to clear the cache of specific files then you can use the proxy_cache_bypass directive. This is how you do it

location / {
    proxy_cache_bypass $cookie_nocache $arg_nocache;
    # ...
}

Now if you want bypass the cache you access the file by passing the nocache parameter

http://www.example.com/app.css?nocache=true

@Marcelo Agimóvel 2019-02-11 18:17:28

I guess this could be use to attack and consume bandwidth in your website.

@David Eyk 2017-12-07 22:57:57

We have a very large nginx cache (gigabytes) that we occasionally need to wipe. I've worked out a script that instantly clears the cache (as far as Nginx is concerned) and then removes the cache directory without starving the main application for disk I/O.

In summary:

  1. Move the cache folder to a new location (on the same filesystem!) (this doesn't disrupt any open file descriptors)
  2. Recreate the original cache folder, empty
  3. Reload Nginx (graceful reload, where nginx lets old workers finish in-progress requests)
  4. Remove old cached data

Here's the script, tailored to Ubuntu 16.04 LTS, with the cache located at /mnt/nginx-cache:

#!/bin/bash
set -e

TMPCACHE=`mktemp --directory --tmpdir=/mnt nginx-cache-XXXXXXXXXX`
TMPTEMP=`mktemp --directory --tmpdir=/mnt nginx-temp-XXXXXXXXXX`

# Move the old cache folders out of the way
mv /mnt/nginx-cache $TMPCACHE
mkdir -p /mnt/nginx-cache
chmod -R 775 /mnt/nginx-cache
chown www-data:www-data /mnt/nginx-cache

mv /mnt/nginx-temp $TMPTEMP
mkdir -p /mnt/nginx-temp
chmod -R 775 /mnt/nginx-temp
chown www-data:www-data /mnt/nginx-temp

# Tell Nginx about the new folders.
service nginx reload

# Create an empty folder.
rm -rf /mnt/empty
mkdir -p /mnt/empty

# Remove the old cache and old temp folders w/o thrashing the disk...
# See http://serverfault.com/questions/546177/how-to-keep-subtree-removal-rm-rf-from-starving-other-processes-for-disk-i
# Note: the `ionice` and `nice` may not actually do much, but why not?
ionice -c 3 nice -19 rsync -a --delete /mnt/empty/ $TMPCACHE
ionice -c 3 nice -19 rsync -a --delete /mnt/empty/ $TMPTEMP
rm -rf $TMPCACHE
rm -rf $TMPTEMP

rm -rf /mnt/empty

And in case it's helpful, here's the Nginx config we use:

upstream myapp {
    server localhost:1337 fail_timeout=0;
}

proxy_cache_path /mnt/nginx-cache/app levels=2:2:2 keys_zone=app_cache:100m inactive=1y max_size=10g;
proxy_temp_path  /mnt/nginx-temp/app;

server {
    listen   4316 default;
    server_name  myapp.com;

    location / {
        proxy_pass http://appserv;
        proxy_cache app_cache;
        proxy_cache_valid 200 1y;
        proxy_cache_valid 404 1m;
    }
}

@MitchellK 2017-03-24 13:21:00

I run a very simple bash script which takes all of 10 seconds to do the job and sends me a mail when done.

#!/bin/bash
sudo service nginx stop
sudo rm -rf /var/cache/nginx/*
sudo service nginx start | mail -s "Nginx Purged" [email protected]
exit 0

@Asle 2017-01-19 09:39:24

I had this problem also.

  • Could not find any nginx/cache folder
  • sendfile was off

My domain uses cloudflare.com for DNS (great service!). Aha! There it was:

cloudflare.com -> caching -> Purge Cache (I purged everything) That solved my problem!

@mahemoff 2017-06-28 11:24:33

This purges Cloudflare's edge caches. It doesn't clear the Nginx cache on your own server.

@Fernando Kosh 2017-08-20 19:44:21

As an advice, I think is a valid answer .

@undefinedman 2018-06-01 02:35:42

This was excellent answer. I was digging hours why some files are still being cached and couldn't guess it was CloudFlare 'fault'. Thanks!

@SilentMiles 2016-05-18 02:00:46

For those who have tried deleting the nginx cache files, and either it hasn't worked or has worked intermittently, have a look at your setting for open_file_cache. If this is enabled and configured to cache a file descriptor for a long time, then Nginx may still see a version of the cached file, even after you've deleted it from disk. I had to reduce open_file_cache_valid to 1s (I'm not certain if this is essentially the same as disabling the file cache completely).

@Mr G 2016-05-16 15:09:52

I was experiencing a kind of similar issue:

System setup and Problem: (On a virtualbox I'm web hosting using ubuntu and nginx - PHP webpage refreshes did not reflect changes to external css file). I'm developing website on windows machine and transferring files to nginx via shared folder. It seems nginx does not pick up changes to css file (refreshing in any fashion does not help. Changing css file name is only thing that worked)

Solution: On VM find shared file (css file in my case). Open with nano and compare to file in windows share (they appear identical). On VM save shared file with nano. All changes are now reflected in browser. Not sure why this works but it did in my case.

UPDATE: After rebooting the VM server the problem returned. Following the instructions under Solution made the css responsive to updates again

@Deepan Chakravarthy 2012-10-29 06:21:23

I had the exact same problem - I was running my nginx in Virtualbox. I did not have caching turned on. But looks like sendfile was set to on in nginx.conf and that was causing the problem. @kolbyjack mentioned it above in the comments.

When I turned off sendfile - it worked fine.

This is because:

Sendfile is used to ‘copy data between one file descriptor and another‘ and apparently has some real trouble when run in a virtual machine environment, or at least when run through Virtualbox. Turning this config off in nginx causes the static file to be served via a different method and your changes will be reflected immediately and without question

@nickl- 2013-09-06 19:34:44

Works as advertised but a link and some information about the purpose of sendfile would be more appreciated than the personal account. Change this from a journal entry into a factual answer and gain my vote.

@Sian Lerk Lau 2014-01-20 06:49:48

Refer to this link

@Dingle 2014-03-23 00:28:27

In my case, the alternative workaround is to turn on gzip for these file types. Either way the problem is solved.

@T1000 2015-01-27 07:21:01

Thank you and kolbyjack so much for the answer. Saved my life.

@Koray Güclü 2015-07-08 21:08:13

I used following 'sudo vim /etc/nginx/nginx.conf' and change ' sendfile on' to 'sendfile off'

@CodingWithSpike 2016-03-02 20:51:08

This is the only solution I can find anywhere, but I actually need to use sendfile so can't disable it :(

@marukobotto 2016-05-21 06:29:03

I turned off sendfile. No luck.

@Jack Nicholson 2016-08-20 10:45:12

Same. I'm using a DO server. Centos 7.2

@Didzis 2017-02-20 13:57:33

Turned sendfile off and changes appeared instantly. Running on an Amazon EC virtual instance. Would want to keep this setting on though...

@dwt 2016-02-02 10:17:07

Please take note that proxy_cache_bypass can give you a world of hurt if your app doesn't return a cacheable response for that specific request where you trigger it.

If for example your app sends a cookie with every first request, then a script which triggers proxy_pass_bypass via curl will probably get that cookie in the answer, and nginx will not use that response to refresh the cached item.

@colapsnux 2016-01-03 14:27:43

find /etc/nginx/cache_folder -type d -exec rm -rvf {} \;
mkdir /etc/nginx/cache_folder
service nginx restart

Be careful to properly specify the correct path.

@Leopoldo Sanczyk 2015-09-18 07:07:32

For those who other solutions are not working, check if you're using a DNS service like CloudFlare. In that case activate the "Development Mode" or use the "Purge Cache" tool.

@Brilliant 2015-05-13 01:47:24

On my server, the nginx cache folder is at /data/nginx/cache/

So I removed it only: sudo rm -rf /data/nginx/cache/

Hope this will help anyone.

@Ivan BlaBlaBla 2015-02-24 19:21:15

There is one right method to remove only cache-files, which matches any KEY. For example:

grep -lr 'KEY: yahoo' /var/lib/nginx/cache | xargs rm -rf

This removes all cache-files, which matches to KEY "yahoo/*", if in nginx.conf was set:

proxy_cache_key $host$uri;

@deyes 2012-12-06 18:11:17

There's two answers in this question.

  • One for nginx as reverse cache
  • Another for cleaning the browser cache by header input (this one)

Use:

expires modified +90d;

E.G.:

location ~* ^.+\.(css|js|jpg|gif|png|txt|ico|swf|xml)$ {
    access_log off;
    root /path/to/htdocs;
    expires modified +90d;
}

@Humber 2013-11-20 19:14:52

The "modified" keywork worked for me. Tks!

@Aaron 2015-09-20 08:41:45

I tried this implementation because i'm having similar issue. However, after I made the change - it shows the default Nginx page. I'm using Niginx as LB with proxy, do I need to change root maybe?

@Jason Wiener 2014-05-21 04:58:27

You can also bypass/re-cache on a file by file basis using

proxy_cache_bypass $http_secret_header;

and as a bonus you can return this header to see if you got it from the cache (will return 'HIT') or from the content server (will return 'BYPASS').

add_header X-Cache-Status $upstream_cache_status;

to expire/refresh the cached file, use curl or any rest client to make a request to the cached page.

curl http://abcdomain.com/mypage.html -s -I -H "secret-header:true"

this will return a fresh copy of the item and it will also replace what's in cache.

@Spock 2015-10-28 07:42:02

Why can I only upvote this one time? I want to do a gazillion :)

@rbu 2016-02-02 10:17:20

This can only update cached pages when the new page is cacheable as well. If you have removed a page (404 or other errors are now served by the backend), the page now sends a Set-Cookie or a "Content-Control: private" header, the cached content will not be "invalidated".

@Maxim Masiutin 2017-04-24 05:19:19

This "add_header X-Cache-Status $upstream_cache_status;" is such a cool feature!

@Ivan Semochkin 2017-06-05 05:48:32

thanks a lot. nice tip for cache invalidation, there is so little tutorials about nginx

@Pluc 2017-08-10 19:30:07

Has this changed since you posted? I can successfully get a fresh copy with the "secret-header" but as soon as I remove the header, I get the cached version again...

@kidz 2017-09-07 20:50:57

Nothing works actually with proxy_cache. Looks like it depends on luck!

@Mike5050 2017-11-25 14:49:44

This worked for me. However, I am not sure where exactly the nginx cache is located ...

@Werner Weber 2019-02-11 12:37:42

Works exactly as advertised on my DigitalOcean Ubuntu 16.04 NodeJS instance. Struggled for hours with different methods.

@austinzmchen 2014-04-07 03:53:22

You can add configuration in nginx.conf like the following.

...
http {
proxy_cache_path  /tmp/nginx_cache levels=1:2 keys_zone=my-test-cache:8m max_size=5000m inactive=300m;

server {
    proxy_set_header X- Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_cache my-test-cache;
    proxy_cache_valid  200 302  1m;
    proxy_cache_valid  404      60m;
    proxy_cache_use_stale   error timeout invalid_header updating;
    proxy_redirect off;

    ....
}
...
}

From above, a folder named "nginx_cache" is dynamicly created in /tmp/ to store cached content.

@jwg 2017-03-27 15:57:47

The right answer.

@agustik 2013-10-19 22:35:26

I found this useful

grep -lr 'jquery.js' /path/to/nginx/cache/folder/* | xargs rm

Search, and if found then delete.

@Ganesh Shankar 2012-02-03 04:50:21

In my nginx install I found I had to go to:

/opt/nginx/cache

and

sudo rm -rf *

in that directory. If you know the path to your nginx install and can find the cache directory the same may work for you. Be very careful with the rm -rf command, if you are in the wrong directory you could delete your entire hard drive.

@Eliel Haouzi 2016-09-21 15:21:24

you need to restart NGINX after that

@kidz 2017-09-07 20:51:54

And that's the bad part

@Łukasz Sokolik 2011-07-14 10:23:49

You can delete cache directory of nginx or You can search specific file:

grep -lr 'http://mydomain.pl/css/myedited.css' /var/nginx/cache/*

And delete only one file to nginx refresh them.

@Jifeng Zhang 2014-10-17 11:39:48

To get the exact hit, you can append $ to the search term. Like grep -lr 'http://mydomain.pl/css/myedited.css$' /var/nginx/cache/*

@webtobesocial 2016-02-06 14:46:37

Unfortunately I got the following output grep: /var/nginx/cache/*: No such file or directory I'm using Ubuntu 14.04.3 LTS and nginx/1.8.1. Any idea?

@jaybrau 2017-10-09 20:56:31

Try the following to grep files under /var/nginx/cache: sudo find /var/nginx/cache -type f -exec grep -l '/css/myedited.css' {} \;

@Gnarfoz 2011-08-01 10:07:57

Unless you configured a cache zone via proxy_cache_path and then used it (for example in a location block), via: proxy_cache nothing will get cached.

If you did, however, then according to the author of nginx, simply removing all files from the cache directory is enough.

Simplest way: find /path/to/your/cache -type f -delete

@Collin Anderson 2013-06-30 20:23:14

i'm getting this in my error log after deleting the files: [crit] 1640#0: unlink() "/path/to/cache/85/1cc5328db278b328f2c200c65179ad85" failed (2: No such file or directory)

@Gnarfoz 2013-07-02 17:50:37

Repeatedly, or just once? It shouldn't be an actual problem. It probably just means that the cache manager tried to delete a file that you already deleted. Maybe reloading nginx (nginx -s reload) might help if you get the message repeatedly. (Not sure if that reinitializes the cache manager, too.)

@Collin Anderson 2013-07-02 21:13:54

yeah, I automatically clear the cache for my website by a script whenever I deploy a change, and reloading nginx doesn't fix it either.

@Thomas Decaux 2013-08-26 09:52:17

Nop Nginx caches something even if you don't use proxy stuff, but it's a bug with Nginx + VirtualBox.

@Gnarfoz 2013-09-02 09:14:39

That sounds rather vague. Could you elaborate on that? Doesn't seem like it's related to the topic at hand here.

@here 2013-11-12 02:32:15

Search nginx conf files for proxy_cache_path to find the path to your cache like grep -r proxy_cache_path /etc/nginx/ Mine was set in /etc/nginx/conf.d/proxy_cache.conf as /var/lib/nginx/proxy

@OsakaWebbie 2014-04-19 21:48:47

@ThomasDecaux: Yes, please tell us more about that bug - I think I'm a victim of it! I have a VM with nginx, the word "cache" does not appear in any of my config files, and turning off sendfile did not solve it for me. I know it's not browser cache because in PHP, echo(file_get_contents("http://my.vm/css/style.css")); will serve up the old content.

@OsakaWebbie 2014-04-19 22:02:13

Never mind - apparently there was a delayed reaction to the act of turning off sendfile and restarting nginx - the first time after that it was the same, but now it appears to be responding to changes.

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] How to clear gradle cache?

26 Answered Questions

[SOLVED] How to control web page caching, across all browsers?

47 Answered Questions

[SOLVED] How to force browser to reload cached CSS/JS files?

31 Answered Questions

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 318432 View
  • 898 Score
  • 11 Answer
  • Tags:   node.js nginx concept

1 Answered Questions

[SOLVED] Proper API caching with nginx

  • 2017-02-03 01:42:43
  • vaindil
  • 273 View
  • 0 Score
  • 1 Answer
  • Tags:   caching nginx

7 Answered Questions

[SOLVED] Flash CS4 refuses to let go

1 Answered Questions

[SOLVED] Nginx not deleting files cached on RAM

  • 2016-08-16 07:23:07
  • Manjunath Bhat
  • 625 View
  • 1 Score
  • 1 Answer
  • Tags:   caching nginx tmpfs

25 Answered Questions

[SOLVED] Is Safari on iOS 6 caching $.ajax results?

0 Answered Questions

Nginx is not serving updated files

Sponsored Content