我使用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年

其他回答

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

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

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

我发现这很有用

grep -lr 'jquery.js' /path/to/nginx/cache/folder/* | xargs rm

搜索,如果找到就删除。

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

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

希望这对大家有所帮助。

这个问题有两个答案。

一个用于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;
}