大网数据广告活动
服务器运维问答

Set expires for all static data, except home url

· 52CE 我爱测

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 pag…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。