鍍金池/ 問答/PHP  Linux/ Nginx下設(shè)置不緩存靜態(tài)文件不起作用?

Nginx下設(shè)置不緩存靜態(tài)文件不起作用?

現(xiàn)在開發(fā)中,調(diào)整css,js,img,css中的img時(shí)會(huì)一直使用緩存文件,無法顯示改動(dòng)!Nginx配置靜態(tài)文件add_header Cache-Control no-cache;也不起作用,很苦惱!求大神指點(diǎn)迷津!下面上代碼

Nginx配置

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

這是具體配置的server

server {
    listen 80 ;
    server_name tp.co admin.tp.co;
    root /code/think/public;

    index index.php;
    location ~.*\.(js|css|png|jpg)$
    {
          root /code/think; 
      add_header Cache-Control no-cache;
    }    
    location / {
             if (!-d $request_filename) {
                 rewrite ^/(.*)/(.*)/*$  /index.php?m=$1&a=$2  last;
            rewrite ^(.*)$ /index.php?s=$1 last;
                 break;
             }
        }
    location ~ \.php { #去掉$
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
            
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_split_path_info ^(.+\.php)(.*)$;     #添加
            fastcgi_param PATH_INFO $fastcgi_path_info;    #添加
        }

}

項(xiàng)目css文件,img,js全在public/static下面
css路徑

<link href="/public/static/css/admin/index/login.css" rel="stylesheet" type="text/css" media="all"/>

css樣式中的img的url

body{
   background: url('public/static/img/admin/index/login/banner.jpg')repeat;
   padding:101px 0px 30px 0px;
   font-family: 'Roboto', sans-serif;
   font-size: 100%;
}

這是瀏覽器報(bào)錯(cuò)

圖片描述

這是查看源代碼得到的css文件內(nèi)容

圖片描述

有木有大神知道為什么呀?!快瘋了 ...

回答
編輯回答
默念

MD,終于解決了!使用virtualBox虛擬機(jī),安裝的nginx,不管怎么修改js,css文件,怎么強(qiáng)制刷新設(shè)置不緩存,都不管用,原因在于sendfile on;;這個(gè)配置在nginx.conf,將sendfile on;改為sendfile off;;再ctrl+F5,OK!完美解決!!

2018年4月7日 07:40
編輯回答
負(fù)我心

更好的做法是加版本號(hào),而是控制緩存頭
如:

<link href="/public/static/css/admin/index/login.css?v=1.2" rel="stylesheet" type="text/css" media="all"/>

每當(dāng)css做出修改時(shí),增加版本號(hào), 版本號(hào)可以定義成全局的變量,一外修改即可。

2018年8月1日 08:33