【问题标题】:Single page app (SPA) cache config for nginxnginx 的单页应用程序 (SPA) 缓存配置
【发布时间】:2022-02-05 03:38:57
【问题描述】:

我有一个在服务器上运行的小型单页应用程序,每次我对其进行更改时,一些用户都会在他们的浏览器上报告错误。这些错误在清除历史记录后消失,这意味着 SPA 的部分内容已被缓存。我在主 SPA 页面上添加了 no-cache 标签。

server {
  listen 80;
  client_max_body_size 25M;
  server_name [COMMENTED_OUT];
  access_log /var/log/nginx/access_log.log;
  error_log /var/log/nginx/error_log.log;
  server_tokens off;
  location / {
    try_files $uri /static/index.html;
    add_header Cache-Control no-cache;
  }
  location /static/ {
    autoindex off;
    alias /static/;
  }
}

我在这方面做错了什么吗?

【问题讨论】:

    标签: nginx single-page-application


    【解决方案1】:

    您可以选择性地设置过期时间:

    map $sent_http_content_type $expires {
      default                     off;
      text/html                   epoch;
      text/css                    1d;    # based on mime type
      application/javascript      1d;
      ~image/                     max;   # based on path
    }
    

    在您的站点配置之上声明它,在服务器指令之外。然后在服务器配置中添加:

    server {
       ...
       expires $expires;
       ...
    }
    

    【讨论】:

    • 谢谢,目前正在获取 - nginx: [emerg] /etc/nginx/conf.d/dev.conf:5 中映射参数的数量无效
    • 你删除了cmets吗?
    • 我现在做到了,我的错。让它运行一会儿,看看这是否有效。谢谢。
    • 好像成功了,谢谢
    猜你喜欢
    • 2015-12-14
    • 2015-12-12
    • 2020-03-07
    • 2019-01-15
    • 2021-10-07
    • 2020-01-14
    • 2013-07-31
    • 2020-07-22
    • 1970-01-01
    相关资源
    最近更新 更多