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

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

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

这将返回一个新的项目副本,它也将替换缓存中的内容。

其他回答

我运行了一个非常简单的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

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

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

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

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

除非你通过proxy_cache_path配置了一个缓存区,然后使用它(例如在一个位置块中),通过: Proxy_cache不会缓存任何东西。

然而,如果你这样做了,那么根据nginx的作者,简单地从缓存目录中删除所有文件就足够了。

最简单的方法:find /path/to/your/cache -type f -delete

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

好吧,在常见的缓存问题的情况下(浏览器缓存,代理缓存,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一样。:)