2016-03-21 32 views
1

我跟着一個教程,得到了一個運行在Vagrant虛擬機上的nginx服務器(使用Chef作爲提供者)。我只有一個HTML頁面,index.html,如果我對其進行更改並運行vagrant provision,那麼當我轉到瀏覽器中的頁面時會反映這些更改。不過,我也有一個nginx正在服務的css和js文件,如果我對它們進行了更改,即使在重新加載虛擬機之後,更改也不會反映在頁面上。儘管我可以通過SSH連接到虛擬機並確認文件的較新版本確實在虛擬機上,我無法弄清虛擬機甚至可以訪問舊文件,因爲我在進行更改時覆蓋了它們。也許我的nginx配置文件有問題嗎?這裏是:對nginx服務器上未顯示的CSS和.js文件的更新

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 

    root /home/vagrant/project/webroot; 
    index index.html index.htm; 

    # Make site accessible from http://localhost/ 
    server_name localhost; 

    location/{ 
     # First attempt to serve request as file, then 
     # as directory, then fall back to displaying a 404. 
     try_files $uri $uri/ =404; 
     # Uncomment to enable naxsi on this location 
     # include /etc/nginx/naxsi.rules 
    } 

我都3個文件(JS,CSS和HTML)文件存儲在webroot上的VM。就像我說的,我可以通過SSH進行驗證,確認它們都是它們應該在的位置,所以我認爲它一定是一個配置問題。我需要爲css和js文件做些特別的事情嗎?

回答

6

1)您可以將緩存控制標題添加到Nginx配置中。

location ~* ^.+\.(js|css)$ { 
    #old style: add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; 
    #old style: add_header Pragma "no-cache"; 
    expires -1; 
    sendfile off; #if you're using virtualbox (it is not about cache, but you will need it) https://www.vagrantup.com/docs/synced-folders/virtualbox.html 
} 
+0

這樣做!非常感謝,真的把我的頭撞在這個牆上。 – Adam

+0

上面的意思是永遠不會緩存靜態文件。每個瀏覽器調用都會發送靜態文件。正如@Kilizo所提到的,這個問題應該在瀏覽器端進行緩存。上述修復可能已經解決了您的問題,但它可能會讓您的nginx服務器在每次瀏覽器請求時發送文件(無緩存)。更多細節在這裏。 https://css-tricks.com/strategies-for-cache-busting-css/和http://stackoverflow.com/questions/30921598/what-does-expires-1-mean-in-nginx-location-directive – bgth

1

你確定你的瀏覽器沒有緩存舊的CSS和JS文件嗎?

+0

我該如何檢查?我想通過清除緩存?如果這有所幫助,我正在使用Chrome。 – Adam

+1

嘗試使用隱身模式加載網站或先清除緩存。您可以使用開發工具檢查緩存的CSS文件何時到期。這個鏈接可能有助於http://www.devcurry.com/2012/03/how-to-determine-resources-coming-from.html – Kilizo

+0

沒有運氣,但好主意。謝謝 – Adam

相關問題