【问题标题】:Varnish default.vcl清漆 default.vcl
【发布时间】:2018-03-01 17:20:10
【问题描述】:

我刚刚在我的开发服务器上安装了 varnish,它在没有更改任何配置的情况下运行。所以现在它只是向 Apache 询问响应并将其传回。

嗯,我是一个新手,我正在尝试缓存 javascript、css 和图像来测试清漆。我的问题是,如果我写返回(查找);在 vcl_recv 中给我服务清漆重启错误!

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
  .host = "127.0.0.1";
  .port = "80";
}

sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
#hash_data(req.url);
#if (req.http.host) {
#    hash_data(req.http.host);
#} else {
#    hash_data(server.ip);
#}
return (lookup);
}

sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.

 }

sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}

default.vcl 中的这个配置在重启时给了我下一个错误:

Job for varnish.service failed. See 'systemctl status varnish.service' and 'journalctl -xn' for details.

请帮帮我!!

【问题讨论】:

    标签: varnish varnish-vcl varnish-4


    【解决方案1】:

    你可以这样做:

    sub vcl_recv {
      if (req.url ~ "(?i)\.(jpeg|jpg|png|gif|ico|js|css)$") {
          unset req.http.Cookie;
          return (hash);
      } else {
          return (pass);
      }
    }
    

    对于扩展答案,您可能需要查看 https://serverfault.com/a/551283/426146 中 Varnish 3 的答案。

    【讨论】:

    • 如果有人尝试加载 style.css?v=something 会发生什么? :)
    • 嗨弗朗西斯,它对我不起作用。也许我做错了什么,我没有正确重启......我写了你发布的内容,但它失败了。我用“return (lookup)”注释该行,重启就OK了。 WTF??
    • @JonZangitu,我的回答是错误地试图返回lookup,但它应该返回hash。我已经更新了答案中的代码。
    【解决方案2】:

    问题在于 default.vcl 中的端口是 80。 端口必须是您的 Web 服务器正在侦听的端口,例如 8080。 Web 服务器必须在 8080 中配置。 在 Debian 中,您的 /etc/systemd/system/varnish.service 文件具有您必须更改为 80 的端口

    ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc ,512m

    然后使用您的浏览器连接到 80 in varnish 和 varnish 从 8080 请求 Web 服务器的页面和文件(第一次点击后缓存)。

    如果文件不存在运行:

    cp /lib/systemd/system/varnish.service /etc/systemd/system/

    systemctl daemon-reload && systemctl status varnish

    并且开始使用该文件,之后您可以在 /etc/varnish/default.vcl 中输入您的代码

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 2021-09-30
      • 2012-07-20
      • 2011-07-31
      • 2011-09-28
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多