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

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

一些文章说我们可以删除缓存目录:var/cache/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;
    }
}

其他回答

好吧,在常见的缓存问题的情况下(浏览器缓存,代理缓存,web服务器缓存),你可以使用常见的缓存问题的决定,如CSS或JS文件的很少变化的内容-通过添加一个URI参数到他们的链接:

不<link rel="stylesheet" type="text/css" href="https://example.com/stacks.css">

但是<link rel="stylesheet" type="text/css" href="https://example.com/stacks.css?v=3b16a418cc4c">

就像StackOverflow一样。:)

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

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

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年

我也遇到过类似的问题:

系统设置和问题: (在virtualbox上,我使用ubuntu和nginx - PHP网页刷新没有反映到外部css文件的更改)。我正在windows机器上开发网站,并通过共享文件夹将文件传输到nginx。nginx似乎没有接收css文件的更改(以任何方式刷新都没有帮助。改变css文件名是唯一的工作)

解决方案: 在VM上找到共享文件(在我的情况下是css文件)。用nano打开并与windows共享中的文件进行比较(它们看起来是相同的)。在VM上使用nano保存共享文件。所有更改现在都反映在浏览器中。不知道为什么这样做,但它确实在我的情况下。

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

我们使用nginx缓存很多东西。缓存目录中有成千上万的项。为了查找并删除项目,我们开发了一些脚本来简化这个过程。你可以在下面找到包含这些脚本的代码库的链接:

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

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