我使用nginx作为前端服务器,我已经修改了CSS文件,但nginx仍然提供旧的文件。
我尝试重新启动nginx,但没有成功,并且我搜索了谷歌,但没有找到有效的清除方法。
一些文章说我们可以删除缓存目录:var/cache/nginx
,但是在我的服务器上没有这样的目录。
现在我该怎么办?
我使用nginx作为前端服务器,我已经修改了CSS文件,但nginx仍然提供旧的文件。
我尝试重新启动nginx,但没有成功,并且我搜索了谷歌,但没有找到有效的清除方法。
一些文章说我们可以删除缓存目录:var/cache/nginx
,但是在我的服务器上没有这样的目录。
现在我该怎么办?
sendfile
在nginx.conf
中设置为on
,这就引起了问题。@kolbyjack在评论中提到了这一点。sendfile
时,它可以正常工作。
这是因为:
这与此错误相关: https://www.virtualbox.org/ticket/12597Sendfile用于“在一个文件描述符和另一个文件描述符之间复制数据”,而显然在虚拟机环境下或者至少通过Virtualbox运行时会有一些麻烦。在nginx中关闭此配置将以不同的方法提供静态文件,您的更改将立即且无需疑问地反映出来。
你也可以通过逐个文件地绕过/重新缓存文件来实现
proxy_cache_bypass $http_secret_header;
另外,你可以返回此标题以检查是否从缓存中获取它(将返回“HIT”),还是从内容服务器获取它(将返回“BYPASS”)。
add_header X-Cache-Status $upstream_cache_status;
要使缓存的文件过期/刷新,请使用curl或任何REST客户端向缓存页面发出请求。
curl http://abcdomain.com/mypage.html -s -I -H "secret-header:true"
这将返回该项的新副本,并替换缓存中的内容。
如果您没有通过proxy_cache_path配置缓存区并在其内使用(例如在位置块中),那么通过proxy_cache将不会缓存任何内容。
如果您已经配置了缓存区,那么根据nginx作者的说法,只需从缓存目录中删除所有文件即可。
最简单的方法:find /path/to/your/cache -type f -delete
[crit] 1640#0: unlink() "/path/to/cache/85/1cc5328db278b328f2c200c65179ad85" failed (2: No such file or directory)
。 - Collin Andersonproxy_cache_path
,以查找缓存路径,例如grep -r proxy_cache_path /etc/nginx/
。我的设置在/etc/nginx/conf.d/proxy_cache.conf
中,路径为/var/lib/nginx/proxy
。 - here您可以删除nginx的缓存目录,或者您可以搜索特定文件:
grep -lr 'http://mydomain.pl/css/myedited.css' /var/nginx/cache/*
删除一个文件即可刷新nginx。
grep -lr 'http://mydomain.pl/css/myedited.css$' /var/nginx/cache/*
- Jifeng Zhanggrep: /var/nginx/cache/*: No such file or directory
我正在使用Ubuntu 14.04.3 LTS和nginx/1.8.1。有什么想法吗? - b00r00x0sudo find /var/nginx/cache -type f -exec grep -l '/css/myedited.css' {} \;
- jaybrau我运行一个非常简单的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
我也遇到了这个问题。
我的域名使用了cloudflare.com的DNS(非常好的服务!)。啊哈!它就在那里:
cloudflare.com -> 缓存 -> 清空缓存(我清空了所有的缓存)这解决了我的问题!
这个问题有两个答案。
使用方法:
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;
}
我觉得这很有用
grep -lr 'jquery.js' /path/to/nginx/cache/folder/* | xargs rm
搜索,如果找到则删除。
在我的nginx安装中,我发现我必须进入:
sudo rm -rf /opt/nginx/cache
如果你知道你的nginx安装路径,并能找到缓存目录,那么也可以这样做。在执行rm -rf
命令时要非常小心,如果在错误的目录中,可能会删除整个硬盘。
我们有一个非常大的nginx缓存(几个GB),偶尔需要清除。我已经编写了一个脚本,可以即时清除缓存(就Nginx而言),然后删除缓存目录,而不会使主应用程序饥饿并进行磁盘I/O。
总之:
这是针对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;
}
}
proxy_cache
? - Alexander Azarov