我使用nginx作为前端服务器,我已经修改了CSS文件,但nginx仍然服务于旧的。

我尝试重新启动nginx,没有成功,我已经谷歌了,但没有找到一个有效的方法来清除它。

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

我现在该怎么办?


当前回答

这个回答主要是一个总结:

Expires, Age, Cache-Control都是HTTP的概念 查看这些链接:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets_with_cache_busting

Nginx设置相同,检查下面的链接:

https://nginx.org/en/docs/http/ngx_http_headers_module.html

不缓存,下面可能会有帮助:(可以设置更少的时间缓存重新验证更快):

expires    0;
add_header Cache-Control private;

清除缓存。 . 删除缓存目录下的文件(/var/nginx/cache/) . 重新加载(不是重新启动)nginx - nginx -s重新加载 见https://forum.nginx.org/read.php?2, 2600年、2602年

其他回答

已经有很多答案了,但我认为我有一个有用的补充;

我正在用Hyper-V运行一个Homestead盒子,我有一个laravel项目,并在nginx上运行。

我在/etc/的nginx文件夹中没有缓存

当我访问我的网站时,我得到了服务器旧视图和css文件。

什么解决了它为我搜索浪费了很多时间看我的nginx配置和尝试的东西是使用PHP工匠。

在artisan安装的文件夹[laravel项目的根目录]中执行如下命令: PHP工匠优化:清除

这个命令清除所有的缓存,当我刷新我的网页,最终它更新了所有的变化。

希望这能帮助像我这样受困的灵魂:)

编辑:如果我有50个名声,我会把这条评论贴在一个已经存在的答案上。[到目前为止我只有43个]

请注意,如果你的应用程序没有为你触发的特定请求返回一个可缓存的响应,proxy_cache_bypass会给你带来很大的伤害。

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

我们有一个非常大的nginx缓存(千兆字节),我们偶尔需要擦除。我已经编写了一个脚本,可以立即清除缓存(就Nginx而言),然后删除缓存目录,而不会饿死主应用程序的磁盘I/O。

总而言之:

将缓存文件夹移动到一个新位置(在相同的文件系统上!)(这不会破坏任何打开的文件描述符) 重新创建原始缓存文件夹,为空 重载Nginx(优雅重载,Nginx让老工人完成正在进行的请求) 删除旧的缓存数据

下面是为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;
    }
}

有一个正确的方法只删除缓存文件,它匹配任何KEY。例如:

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

如果在nginx.conf中设置了KEY "yahoo/*",则删除所有与之匹配的缓存文件:

proxy_cache_key $host$uri;

我也有这个问题。

找不到nginx/cache文件夹 发送文件已关闭

我的域名使用cloudflare.com进行DNS(很棒的服务!)啊哈!就是这样:

cloudflare.com ->缓存->清除缓存(我清除了一切) 这解决了我的问题!