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

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

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

我现在该怎么办?


当前回答

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

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

希望这对大家有所帮助。

其他回答

我也有这个问题。

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

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

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

这个问题有两个答案。

一个用于nginx作为反向缓存 另一个是通过头文件输入清理浏览器缓存的(这个)

Use:

expires modified +90d;

例如:

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

我们有一个非常大的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;
    }
}

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

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

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

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

你可以在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”的文件夹来存储缓存的内容。