我有一个案例,我使用 Nginx 在
/var/www/html
中提供静态文件。这是我的配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ /index.html ;
}
}
我使用 crontab 任务每 5 分钟更新一次
var/www/html
内容:
*/5 * * * * /usr/bin/aws s3 sync s3://my_bucket /var/www/html --delete
文件已正确更新。新文件正在传入,已删除的文件正在被删除。
在 crontab 任务之后,我看到服务器上文件系统的更改,但在通过 www 访问的客户端上看不到。我认为这可能是 Nginx 缓存问题,但重新启动 Nginx 甚至整个服务器都没有帮助。清除浏览器缓存对我们不起作用。
只有使用
/var/www/html
命令清空 rm -rf *
目录并再次手动与 S3 同步时,我才能看到最新更改 - 尽管文件与 s3 同步之前完全相同。
什么可能导致此行为?
您的 cronjob 命令似乎是正确的。
尝试禁用 nginx 缓存。
server {
...
proxy_cache off; #This directive turns off proxy caching, which disables NGINX's built-in caching mechanism.
proxy_cache_bypass $http_cache_control;# This directive bypasses caching based on the Cache-Control header in the response. It ensures that responses containing Cache-Control headers with specific values are not cached.
add_header Cache-Control "no-cache, no-store, must-revalidate"; #This line sets the Cache-Control header in NGINX responses to ensure that clients (browsers) do not cache the content. The values "no-cache," "no-store," and "must-revalidate" instruct the client not to cache the content and to revalidate it with the server for each request.
expires off; #This directive turns off the use of the Expires header, which would otherwise specify a date in the future for when the content can be considered stale and should be re-fetched from the server.
...
}