Nginx缓存文件从S3使用crontab更新

y1aodyip  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(146)

我有一个案例,我使用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甚至整个服务器都没有帮助。清除浏览器缓存并不能很好地为我们工作。
只有当我用rm -rf *命令清空/var/www/html目录并再次用S3手动同步它时,我才能看到最新的更改-尽管文件与S3同步之前完全相同。
是什么导致了这种行为?

33qvvth1

33qvvth11#

你的cronjob命令似乎是正确的。
尝试全局禁用nginx缓存。

server {
    ...
    proxy_cache off; # It turns off proxy caching, which disables NGINX's built-in caching mechanism.
    proxy_cache_bypass $http_cache_control;# It 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; #It 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.
    ...
}

相关问题