1
我在nginx反向代理後面託管了一個angular2應用程序。 Angular2在技術上是一個單頁的應用程序,在index.html的主持,但它是用正常的路線,所以index.html文件沒有直接請求,而不是一個正常的路徑尋找可以要求這樣的:錯誤地執行了nginx位置塊
http://my.angular.app/
http://my.angular.app/users
http://my.angular.app/users/john
http://my.angular.app/resource123
http://my.angular.app/anything
取決於,如果路徑上有文件,我想提供該文件(並緩存它)。否則,我想爲index.html提供服務。這是我的3塊位置:
# don't cache index.html file
location/{
index index.html;
expires -1;
try_files $uri $uri/ /index.html;
}
# Cache everything with a hash for a year
location ~* "\.[0-9a-f]{16,999}(?:\.chunk|\.bundle|)\.(?:jpg|jpeg|gif|css|png|js|ico|html)$" {
add_header Pragma public;
add_header Cache-Control "public";
access_log off;
expires 31536000; # 1 year
}
# Cache all other assets for a day
location ~* ^(?!index\.html$).*\.(?:jpg|jpeg|gif|css|png|js|ico|html)$ {
add_header Pragma public;
add_header Cache-Control "public";
access_log off;
expires 86400; # 1 day
}
我的意圖是絕不讓瀏覽器緩存中的index.html文件,但一切應儘可能長時間地緩存。散列文件的部分工作正常。問題是對於普通URL
如果我請求my.angular.app/module/sub-route
儘管3.位置不應該匹配(沒有定義擴展名)並且第一個位置已設置expires -1
,但我仍然收到1天的緩存標頭。
如何將'index.html'填充到自己的位置並相應地設置緩存設置。在最後使用'='而不是'〜*'之前定義該位置。像'location =/index.html'。如果該塊與'='匹配,那麼在此之後將不再有其他位置被評估。 –
@ansi_lumen sry我放棄了詳細介紹Angular2託管問題。由於Angular2使用的是正常的URL(沒有像許多其他SPA框架使用它的散列),客戶端很少請求index.html,但通常是服務器上不存在的隨機路由。我需要檢測並提供index.html。 (查看更新後的問題) – peter
第三個地址匹配'/ index.html',因爲你錯過了領先的'/'。所有'nginx' URI都包含一個前導'/'。 –