【问题标题】:Angular ssr varnish usageAngular ssr 清漆使用
【发布时间】:2020-03-23 16:00:02
【问题描述】:

它工作但不影响性能。我用对了吗?

/etc/varnish/default.vcl

    backend default { 
    .host = "127.0.0.1"; 
    .port = "4000"; }

我在 nginx 配置中添加了消失端口而不是 4000

 location / {
      proxy_pass          http://localhost:6081;
}

我的 Angular 应用程序(google pagespeed)桌面性能为 99%,但移动性能为 40-60%。

【问题讨论】:

    标签: node.js angular performance nginx varnish


    【解决方案1】:

    Varnish 的开箱即用行为尊重 HTTP 缓存最佳实践。

    这意味着:

    • 仅缓存 HTTP GET 和 HTTP HEAD 调用
    • 当请求包含cookie 标头时,不提供来自缓存的响应
    • 当请求包含 authorization 标头时,不提供来自缓存的响应
    • 当存在set-cookie 标头时,不要将响应存储在缓存中
    • cache-control 标头为零 TTL 或包含以下内容时,不要将响应存储在缓存中:no-cache,或no-store,或private

    在所有情况下,Varnish 都会尝试从缓存中提供服务或存储在缓存中。

    这是用 VCL 编写的行为:https://github.com/varnishcache/varnish-cache/blob/6.0/bin/varnishd/builtin.vcl

    适应现实世界

    虽然这些缓存最佳做法很有意义,但当您查看现实世界时,它们并不现实。在现实世界中,我们一直都在使用 cookie。

    这就是为什么您可能必须编写一些VCL 代码 来更改缓存的行为。为此,您必须非常熟悉应用的 HTTP 端点,以及使用 cookie 的部分。

    • 应用中在服务器端使用 cookie 值的部分必须从缓存中排除
    • 应用中未使用 cookie 值的部分将存储在缓存中
    • 必须剥离仅在客户端使用的跟踪 cookie

    如何检查发生了什么

    varnishlog 二进制文件将帮助您了解流经 Varnish 的流量类型以及 Varnish 如何处理该流量。

    我已经写了一篇关于这个的深入博客文章,请看一下:https://feryn.eu/blog/varnishlog-measure-varnish-cache-performance/

    编写 VCL

    一旦找出导致性能下降的原因,就可以编写 VCL 来缓解问题。请查看文档站点以了解 VCL:https://varnish-cache.org/docs/6.0/index.html

    里面有参考资料、用户指南甚至教程。

    祝你好运

    【讨论】:

      猜你喜欢
      • 2015-08-01
      • 2012-06-22
      • 1970-01-01
      • 2018-03-01
      • 2021-02-18
      • 1970-01-01
      • 2022-12-20
      • 2017-07-21
      • 2018-04-07
      相关资源
      最近更新 更多