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都包含一个前导'/'。 –