vue项目上线后服务器资源更新而浏览器有缓存不更新

本文介绍如何通过Nginx配置文件禁止特定页面的缓存,包括使用no-cache和no-store指令的区别,并提供了一个在HTML头部加入元标签来控制缓存的方法。

原因

浏览器的缓存机制(分为强缓存和协商缓存)
强缓存:即不向后端发送请求,直接从缓存中读取数据
协商缓存:向后端发起请求,看服务器资源是否更新,如果没有更新就返回304,如果更新了就返回200

解决方法:
/etc/nginx/conf.d/default.conf在服务器的这个文件夹找到配置文件,并下载下来修改,下载下来之后请先复制一份这个配置文件,避免修改出错导致服务器不能正常使用
1.nginx 配置,在在nginx.conf文件做设置,让 index.html 不缓存

location = /index.html {
	root	 /usr/share/nginx/html;
    add_header Cache-Control "no-cache, no-store";
}

或者直接在 location / 这里增加
add_header Cache-Control "no-cache, no-store";

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        add_header Cache-Control "no-cache, no-store";
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

no-cache, no-store可以只设置一个
no-cache浏览器会缓存,但刷新页面或者重新打开时 会请求服务器,服务器可以响应304,如果文件有改动就会响应200
no-store浏览器不缓存,刷新页面需要重新下载页面。
推荐两个都设置设置

关于浏览器缓存原因及解决方法相关连接
2.在 html 页面前面加 meta 标签。

<meta http-equiv="pragram" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
<meta name="viewport" content="width=device-width,initial-scale=1.0">

相关参考连接

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值