nginx 配置vue項目緩存


一、vue的所有資源修改后打包出來的名稱都會改變,所以可以使用強緩存,對css、js、png、ttf、jpg等 

  

location ~* \.(css|js|png|jpg|jpeg|gif|gz|svg|mp4|ogg|ogv|webm|htc|xml|woff)$ {
            access_log off;
            add_header Cache-Control max-age=604800;
        }

 

二、html文件因為名稱不會改變,所以使用協商緩存,html文件有改動就會立即更新,max-age=no-cache代表進入協商緩存,文件改動會自動更新,不改動會返回304

location ~* \.(html)$ {
            access_log off;
            add_header  Cache-Control  max-age=no-cache;
        }

三、完整代碼 + gzip壓縮

#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 {
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        #gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        gzip_vary off;
        gzip_disable "MSIE [1-6]\.";
        listen       8010;
        server_name  localhost;

        location /wallet_admin_test {
            proxy_pass http://suiyi.columbu.world/wallet_admin_test;
        }
        error_page   500 502 503 504  /50x.html;
         root   html/dist;
        location / {
           
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
        location ~* \.(html)$ {
            access_log off;
            add_header  Cache-Control  max-age=no-cache;
        }
        location ~* \.(css|js|png|jpg|jpeg|gif|gz|svg|mp4|ogg|ogv|webm|htc|xml|woff)$ {
            access_log off;
            add_header Cache-Control max-age=604800;
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM