解決更新頁面版本後用戶需CTRL+F5強刷才能應用最新頁面

  • 2021 年 10 月 22 日
  • 筆記

設置文件永遠不從緩存讀取

  • 第一步:在html文件設置文件不緩存
<!DOCTYPE html>
<html lang="en" class="theme-light">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="./logo.ico">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!--緩存設置相關start-->
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <!--緩存設置相關end -->
    <title>xxxxx</title>
  </head>
  <body >
    <div id="webRis"></div>
  </body>
</html>
  • 第二步:在nginx配置文件里設置文件不緩存
    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    pid        logs/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       mime.types;
        default_type  application/octet-stream;

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        #access_log  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
            listen       17001;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;
            #緩存設置相關start
            location / {
                root   html;
                index  index.html;
                expires -1s;
            }
            #設置所有yml、HTML類型文件不緩存
            location ~ .*\.(yml|html)$ {
                add_header Cache-Control no-store;
            }
            #緩存設置相關end


            #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   html;
            }

            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   //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;
            #}
            #gzip模塊設置
            gzip            off;    #開啟gzip壓縮輸出
            # gzip_static     on;    #nginx對於靜態文件的處理模塊,該模塊可以讀取預先壓縮的gz文件,這樣可以減少每次請求進行gzip壓縮的CPU資源消耗。該模塊啟用後,nginx首先檢查是否存在請求靜態文件的gz結尾的文件,如果有則直接返回該gz文件內容。為了要兼容不支持gzip的瀏覽器,啟用gzip_static模塊就必須同時保留原始靜態文件和gz文件。這樣的話,在有大量靜態文件的情況下,將會大大增加磁盤空間。我們可以利用nginx的反向代理功能實現只保留gz文件。
            # gzip_disable    "MSIE [1-6]\.(?!.*SV1)";    #禁用IE6的gzip壓縮,IE6的某些版本對gzip的壓縮支持很不好,會造成頁面的假死
            # gzip_min_length 10k;    #設置允許壓縮的頁面最小位元組,頁面位元組數從header頭的Content-Length中獲取,默認值是0,不管頁面多大都進行壓縮。建議設置成大於1K。如果小於1K可能越壓越大
            # gzip_buffers    4 16k; #壓縮緩衝區,設置系統獲取幾個單位的緩存用於存儲gzip的壓縮結果數據流。4 16k代表以16k為單位,安裝原始數據大小以16k為單位的4倍申請內存 。如果沒有設置,默認值是申請跟原始數據相同大小的內存空間去存儲gzip壓縮結果
            # gzip_comp_level 4;     #壓縮s級別 1-9,默認值:1(建議選擇為4),級別越高壓縮率越大,當然壓縮時間也就越長(傳輸快但比較消耗cpu)
            # gzip_types      text/xml text/css application/javascript; #壓縮類型,匹配MIME類型進行壓縮,設置哪壓縮種文本文件可參考 conf/mime.types,(無論是否指定)text/html默認已經壓縮,默認不對js/css文件進行壓縮,不能用通配符 text/*
            # gzip_proxied    off;    #作為反向代理的時候啟用,開啟或者關閉後端服務器返回的結果,匹配的前提是後端服務器必須要返回包含"Via"的 header頭。
            #                         #默認值:off [off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any]
            #                         #off - 關閉所有的代理結果數據的壓縮
            #                         #expired - 啟用壓縮,如果header頭中包含 "Expires" 頭信息
            #                         #no-cache - 啟用壓縮,如果header頭中包含 "Cache-Control:no-cache" 頭信息
            #                         #no-store - 啟用壓縮,如果header頭中包含 "Cache-Control:no-store" 頭信息
            #                         #private - 啟用壓縮,如果header頭中包含 "Cache-Control:private" 頭信息
            #                         #no_last_modified - 啟用壓縮,如果header頭中不包含 "Last-Modified" 頭信息
            #                         #no_etag - 啟用壓縮 ,如果header頭中不包含 "ETag" 頭信息
            #                         #auth - 啟用壓縮 , 如果header頭中包含 "Authorization" 頭信息
            #                         #any - 無條件啟用壓縮
            # gzip_vary       on;  
        }


        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}


        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

    }
  • 第三步:重啟nginx
1.在nginx根目錄放置文件stop.bat,並執行此文件以殺掉所有nginx進程,內容如下:
cd /d %~dp0
taskkill /f /im nginx.exe
del logs\*.log
nginx -s stop
ping 127.0.0.1 -n 10 >nul

2.執行nginx.exe重啟nginx