问题原文
原帖链接 · paul23 · CC BY-SA 4.0
I am creating a spa website, now for the static data (javascript) I wish to set up the headers in such a way it only expires after a certain amount of time.
However to allow for easy updating I wish to not do this on the main page. (Each new version will rename the bundled javascript, so cache will be invalidated that way).
My site data has the following snippet:
location /api {
proxy_pass http://${DYNAMIC_SERVER}:${DYNAMIC_PORT};
}
location / {
# redirect everything to show the main SPA application
try_files $uri $uri/ /index.html;
expires 7d;
}
This caches everything, including index.html - how do I prevent this?
已采纳解答
原帖链接 · Richard Smith · CC BY-SA 4.0
To disable cache on any URI that does not resolve to a static file, but is instead, internally rewritten to /index.html, you will need to add a separate location block to process that one URI.
For example:
location / {
expires 7d;
try_files $uri $uri/ /index.html;
}
location = /index.html {
expires -1;
}
Nginx selects a location block to process the original requested URI. The try_files statement causes that URI to be internally rewritten if it does not match a static file or directory. Nginx then restarts the search for a location block to process the rewritten URI.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
