CSS 如何清除nginx的缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6236078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear the cache of nginx?
提问by Freewind
I use nginx to as the front server, I have modified the CSS files, but nginx is still serving the old ones.
我使用nginx作为前端服务器,我已经修改了CSS文件,但nginx仍然为旧的服务。
I have tried to restart nginx, to no success and I have Googled, but not found a valid way to clear it.
我试图重新启动 nginx,但没有成功,我已经谷歌搜索,但没有找到有效的方法来清除它。
Some articles say we can just delete the cache directory: var/cache/nginx
, but there is no such directory on my server.
有些文章说我们可以删除缓存目录:var/cache/nginx
,但我的服务器上没有这样的目录。
What should I do now?
我现在该怎么办?
采纳答案by Deepan Chakravarthy
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. @kolbyHyman mentioned it above in the comments.
我遇到了完全相同的问题 - 我在 Virtualbox 中运行我的 nginx。我没有打开缓存。但看起来像是sendfile
设置为on
innginx.conf
而这导致了问题。@kolbyHyman 在上面的评论中提到了它。
When I turned off sendfile
- it worked fine.
当我关闭时sendfile
- 它工作正常。
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
Sendfile 用于“在一个文件描述符和另一个文件描述符之间复制数据”,并且在虚拟机环境中运行时,或者至少在通过 Virtualbox 运行时,显然有一些真正的麻烦。在 nginx 中关闭此配置会导致通过不同的方法提供静态文件,您的更改将立即毫无疑问地反映出来
It is related to this bug: https://www.virtualbox.org/ticket/12597
它与此错误有关:https: //www.virtualbox.org/ticket/12597
回答by Jason Wiener
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').
作为奖励,您可以返回此标头以查看您是从缓存(将返回“HIT”)还是从内容服务器(将返回“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 或任何其他客户端向缓存页面发出请求。
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.
这将返回该项目的新副本,并且还将替换缓存中的内容。
回答by Gnarfoz
Unless you configured a cache zone via proxy_cache_pathand then used it (for example in a location block), via: proxy_cachenothing will get cached.
除非你通过proxy_cache_path配置了一个缓存区然后使用它(例如在一个位置块中),通过: proxy_cache什么都不会被缓存。
If you did, however, then according to the author of nginx, simply removing all files from the cache directory is enough.
但是,如果您这样做了,那么根据 nginx 的作者,只需从缓存目录中删除所有文件就足够了。
Simplest way: find /path/to/your/cache -type f -delete
最简单的方法: find /path/to/your/cache -type f -delete
回答by ?ukasz Sokolik
You can delete cache directory of nginx or You can search specific file:
您可以删除 nginx 的缓存目录,或者您可以搜索特定文件:
grep -lr 'http://mydomain.pl/css/myedited.css' /var/nginx/cache/*
And delete only one file to nginx refresh them.
并且只删除一个文件到 nginx 刷新它们。
回答by deyes
There's two answers in this question.
这个问题有两个答案。
- One for nginx as reverse cache
- Another for cleaning the browser cache by header input (this one)
- 一个用于 nginx 作为反向缓存
- 另一个用于通过标头输入清理浏览器缓存(这个)
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;
}
回答by agustik
I found this useful
我发现这很有用
grep -lr 'jquery.js' /path/to/nginx/cache/folder/* | xargs rm
Search, and if found then delete.
搜索,如果找到则删除。
回答by Ganesh Shankar
In my nginx install I found I had to go to:
在我的 nginx 安装中,我发现我必须去:
/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 carefulwith the rm -rf
command, if you are in the wrong directory you could delete your entire hard drive.
在那个目录中。如果您知道 nginx 安装的路径并且可以找到缓存目录,那么它可能对您有用。使用该命令时要非常小心rm -rf
,如果您位于错误的目录中,则可能会删除整个硬盘驱动器。
回答by MitchellK
I run a very simple bash script which takes all of 10 seconds to do the job and sends me a mail when done.
我运行了一个非常简单的 bash 脚本,它需要 10 秒的时间来完成这项工作,并在完成后给我发送一封邮件。
#!/bin/bash
sudo service nginx stop
sudo rm -rf /var/cache/nginx/*
sudo service nginx start | mail -s "Nginx Purged" [email protected]
exit 0
回答by Asle
I had this problem also.
我也有这个问题。
- Could not find any nginx/cache folder
- sendfile was off
- 找不到任何 nginx/cache 文件夹
- 发送文件已关闭
My domain uses cloudflare.comfor DNS (great service!). Aha! There it was:
我的域使用cloudflare.com作为 DNS(很棒的服务!)。啊哈!那里是:
cloudflare.com -> caching -> Purge Cache(I purged everything) That solved my problem!
cloudflare.com -> 缓存 -> 清除缓存(我清除了所有内容)这解决了我的问题!
回答by David Eyk
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.
我们有一个非常大的 nginx 缓存(千兆字节),我们偶尔需要擦除它。我已经制定了一个脚本,可以立即清除缓存(就 Nginx 而言),然后删除缓存目录而不会使主应用程序缺乏磁盘 I/O。
In summary:
总之:
- Move the cache folder to a new location (on the same filesystem!) (this doesn't disrupt any open file descriptors)
- Recreate the original cache folder, empty
- Reload Nginx (gracefulreload, where nginx lets old workers finish in-progress requests)
- Remove old cached data
- 将缓存文件夹移动到一个新位置(在同一个文件系统上!)(这不会破坏任何打开的文件描述符)
- 重新创建原始缓存文件夹,为空
- 重新加载 Nginx(优雅的重新加载,nginx 让老员工完成正在进行的请求)
- 删除旧的缓存数据
Here's the script, tailored to Ubuntu 16.04 LTS, with the cache located at /mnt/nginx-cache
:
这是为 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
And in case it's helpful, here's the Nginx config we use:
如果有帮助,这里是我们使用的 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;
}
}