如何清除nginx的缓存?

CSS

JinJinStafanL

2020-03-24

我使用nginx作为前端服务器,我修改了CSS文件,但是nginx仍在使用旧文件。

我试图重新启动nginx,但没有成功,我已经用Google搜索,但是找不到清除它的有效方法。

有些文章说我们只能删除缓存目录:var/cache/nginx,但是我的服务器上没有这样的目录。

我现在应该怎么办?

第3595篇《如何清除nginx的缓存?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

19个回答
达蒙米亚达蒙 2020.03.24

我遇到了类似的问题:

系统设置和问题:( 在virtualbox上,我使用ubuntu和nginx进行网络托管-PHP网页刷新未反映对外部CSS文件的更改)。我正在Windows计算机上开发网站,并通过共享文件夹将文件传输到nginx。似乎Nginx不会对css文件进行更改(以任何方式刷新都无济于事。更改css文件名只是有效的方法)

解决方案: 在VM上找到共享文件(在我的情况下为CSS文件)。用nano打开,然后与Windows共享中的文件进行比较(它们看起来相同)。在VM上使用nano保存共享文件。现在,所有更改都将反映在浏览器中。不知道为什么这行得通,但就我而言确实如此。

更新:重新启动VM服务器后,问题返回。遵循解决方案下的说明,使CSS再次响应更新

卡卡西Near 2020.03.24

在我的情况下,它是/etc/php/7.2/fpm/php.ini(Ubuntu)中启用的opcache:

opcache.enable=1

将其设置为0将使服务器加载最新版本的(php)文件。

猴子 2020.03.24

我们使用nginx缓存很多东西。缓存目录中有成千上万的项目。为了找到并删除项目,我们开发了一些脚本来简化此过程。您可以在下面找到此脚本的存储库:

https://github.com/zafergurel/nginx-cache-cleaner

这个想法很简单。创建缓存索引(带有缓存键和相应的缓存文件)并在该索引文件中搜索。它确实帮助我们加快了查找项目的速度(从几分钟到亚秒级)并相应地删除了它们。

2020.03.24

以我touch为例该Css文件使它看起来像资源已更改(实际上touch除了更改上次修改时间外,实际上对该文件没有任何作用),因此浏览器和Nginx将应用最新资源

理查德Harry 2020.03.24

您可以像下面这样在nginx.conf中添加配置。

...
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;

    ....
}
...
}

从上面开始,在/ tmp /中动态创建一个名为“ nginx_cache”的文件夹以存储缓存的内容。

蛋蛋宝儿 2020.03.24

在我的服务器上,nginx缓存文件夹位于 /data/nginx/cache/

所以我只删除了它: sudo rm -rf /data/nginx/cache/

希望这会帮助任何人。

StafanPro小哥 2020.03.24

对于那些尝试删除nginx缓存文件的人,或者它没有起作用或间歇性起作用,请查看open_file_cache的设置。如果启用了该功能并将其配置为长时间缓存文件描述符,那么即使从磁盘删除文件,Nginx仍可能会看到该文件的版本。我不得不将open_file_cache_valid减少为1s(我不确定这是否与完全禁用文件缓存基本相同)。

十三 2020.03.24

如果要清除特定文件的缓存,则可以使用proxy_cache_bypass指令。这就是你的做法

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

现在,如果您想绕过缓存,则可以通过传递nocache参数来访问文件

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

小卤蛋JinJin 2020.03.24
find /etc/nginx/cache_folder -type d -exec rm -rvf {} \;
mkdir /etc/nginx/cache_folder
service nginx restart

请注意正确指定正确的路径。

Pro神奇 2020.03.24

请注意,如果您的应用未在触发该请求的特定请求中返回可缓存的响应,则proxy_cache_bypass可能会给您造成很大的伤害。

例如,如果您的应用程序在每个第一个请求中发送一个cookie,那么一个通过curl触发proxy_pass_bypass的脚本可能会在响应中获取该cookie,而nginx将使用该响应来刷新缓存的项目。

Tony凯 2020.03.24

我们有一个非常大的Nginx缓存(千兆字节),我们有时需要擦除它。我制定了一个脚本,该脚本可以立即清除缓存(就Nginx而言),然后删除缓存目录,而不会导致主应用程序出现磁盘I / O短缺的情况。

综上所述:

  1. 将缓存文件夹移动到新位置(在同一文件系统上!)(这不会破坏任何打开的文件描述符)
  2. 重新创建原始的缓存文件夹,为空
  3. 重新加载Nginx(正常重新加载,nginx允许老员工完成正在进行的请求)
  4. 删除旧的缓存数据

这是为Ubuntu 16.04 LTS量身定制的脚本,缓存位于/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

如果有帮助,请使用以下Nginx配置:

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;
    }
}
阿飞 2020.03.24

我运行了一个非常简单的bash脚本,该脚本需要10秒钟来完成这项工作,并在完成后向我发送邮件。

#!/bin/bash
sudo service nginx stop
sudo rm -rf /var/cache/nginx/*
sudo service nginx start | mail -s "Nginx Purged" me@gmail.com
exit 0
老丝 2020.03.24

对于那些其他解决方案不起作用的用户,请检查您是否正在使用诸如CloudFlare之类的DNS服务在这种情况下,请激活“开发模式”或使用“清除缓存”工具。

前端A宝儿 2020.03.24

在我的nginx安装中,我发现必须去:

/opt/nginx/cache

sudo rm -rf *

在该目录中。如果您知道您的nginx安装路径,并且可以找到缓存目录,则同样可以使用。小心使用该rm -rf命令,如果您在错误的目录中,则可以删除整个硬盘。

GO宝儿 2020.03.24

我也有这个问题。

  • 找不到任何Nginx /缓存文件夹
  • sendfile已关闭

我的域使用cloudflare.com进行DNS(优质服务!)。啊哈!在那里:

cloudflare.com->缓存->清除缓存(我清除了所有内容)解决了我的问题!

逆天LEY 2020.03.24

除非您通过proxy_cache_path配置了一个缓存区域,然后使用了它(例如在一个位置块中),否则,通过: proxy_cache不会缓存任何内容。

但是,如果您这样做了,那么根据nginx的作者,只需从缓存目录中删除所有文件就足够了。

最简单的方法: find /path/to/your/cache -type f -delete

Tony西门 2020.03.24

您还可以使用逐个文件地绕过/重新缓存

proxy_cache_bypass $http_secret_header;

作为奖励,您可以返回此标头,以查看是否从缓存(将返回“ HIT”)或从内容服务器(将返回“ BYPASS”)获取它。

add_header X-Cache-Status $upstream_cache_status;

要使缓存的文件过期/刷新,请使用curl或任何其他客户端向缓存的页面发出请求。

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

这将返回该项目的新副本,并且还将替换缓存中的内容。

A西里 2020.03.24

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

It is related to this bug: https://www.virtualbox.org/ticket/12597

乐米亚 2020.03.24

这个问题有两个答案。

  • 一个用于Nginx作为反向缓存
  • 另一个用于通过标头输入清除浏览器缓存(此)

采用:

expires modified +90d;

例如:

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android