【问题标题】:Symfony 5 and varnishSymfony 5 和清漆
【发布时间】:2021-04-10 02:01:03
【问题描述】:

我在我的服务器上安装了 varnish,它处于活动状态(通过 sudo systemctl varnish restart 命令检查)。我按照教程https://symfony.com/doc/current/http_cache/varnish.html 配置了 /etc/varnish/default.vcl 文件,但我不认为在 Symfony 上考虑了清漆(我没有在响应标头中看到 X-Varnish(但是它当我通过 http://localhost/ 直接访问我的服务器时,效果很好。

这是我的 default.vcl 文件:

#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.
 
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
 
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
 
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.
    if (req.http.X-Forwarded-Proto == "https" ) {
        set req.http.X-Forwarded-Port = "443";
    } else {
        set req.http.X-Forwarded-Port = "80";
    }
 
    # Remove all cookies except the session ID.
    if (req.http.Cookie) {
        set req.http.Cookie = ";" + req.http.Cookie;
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1=");
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
 
        if (req.http.Cookie == "") {
            // If there are no more cookies, remove the header to get page cached.
            unset req.http.Cookie;
        }
    }
 
    # Add a Surrogate-Capability header to announce ESI support.
    set req.http.Surrogate-Capability = "abc=ESI/1.0";
}
 
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.
 
    # Check for ESI acknowledgement and remove Surrogate-Control header
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}
 
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.
}

也许在 symfony 上需要配置一些东西,但我看不出是什么。谢谢。

【问题讨论】:

    标签: varnish symfony5


    【解决方案1】:

    如果您不确定 Varnish 是否正确处理请求,您应该检查 varnishd 进程是否在正确的 TCP 端口上运行。

    您可以通过运行以下命令来做到这一点:

    sudo systemctl status varnish
    

    在我的例子中,这是输出:

    ● varnish.service - Varnish Cache Plus, a high-performance HTTP accelerator
       Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled)
      Drop-In: /etc/systemd/system/varnish.service.d
               └─override.conf
       Active: active (running) since Fri 2021-04-09 17:50:00 UTC; 3min 5s ago
     Main PID: 1242 (varnishd)
        Tasks: 226
       CGroup: /system.slice/varnish.service
               ├─1242 /usr/sbin/varnishd -a :80 -a 127.0.0.1:8443,proxy -S /etc/varnish/secret -T localhost:6082 -f /etc/varnish/default.vcl -s mse,/etc/varnish/mse.conf
               └─1256 /usr/sbin/varnishd -a :80 -a 127.0.0.1:8443,proxy -S /etc/varnish/secret -T localhost:6082 -f /etc/varnish/default.vcl -s mse,/etc/varnish/mse.conf
    

    相关信息是执行的命令,以及运行时选项和参数。

    在这种情况下,varnishd 正在侦听 2 个端口上的传入连接:

    • 80 用于常规 HTTP 流量
    • 8443 用于代理流量

    在您的情况下,侦听端口的配置可能不同。

    开箱即用,这些是默认值:

    /usr/sbin/varnishd \
          -a :6081 \
          -a localhost:8443,PROXY \
          -p feature=+http2 \
          -f /etc/varnish/default.vcl \
          -s malloc,256m
    

    如您所见,端口 6081 用于传入 HTTP 流量。您可能需要将其更改为端口80

    只需运行以下命令即可编辑单元文件:

    sudo systemctl edit --full varnish
    

    调整运行时参数后,只需运行以下命令重启服务:

    sudo systemctl restart varnish
    

    警告:如果您在与网络服务器相同的服务器上运行 Varnish,您还需要更改网络服务器的侦听端口。一个不错的选择是端口8080

    【讨论】:

    • 所以我的配置是:DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc ,256 米”。我需要添加 -a 127.0.0.1:8000 吗?因为symfony的项目开启127.0.0.1:8000
    • @yinyang 我猜你有点困惑:-a 定义了来自客户端的传入请求的侦听器。您要做的是将 Varnish 连接到 Symfony。这种交互是使用 default.vcl 文件中的 backend 定义完成的。有关后端的更多信息,请参阅varnish-cache.org/docs/6.0/reference/…
    • 我的 default.vcl 中已经有一个后端定义:后端默认 { .host = "127.0.0.1"; .port = "8080"; }
    • @yinyang 太好了,只需按照我的说明确保对 Varnish 的传入请求使用正确的端口发生。我建议你在 80 端口上运行 Varnish 服务器。
    猜你喜欢
    • 2011-06-08
    • 2016-04-19
    • 1970-01-01
    • 2012-07-20
    • 2011-07-31
    • 2011-09-28
    • 1970-01-01
    • 2011-08-31
    • 2013-07-16
    相关资源
    最近更新 更多