laravel - 使 Varnish 仅在用户未登录时缓存页面(Laravel 5)

标签 laravel varnish

即使用户未登录,Laravel 也会设置 cookie。这会强制 Varnish 将每个请求发送到后端。我遇到过一些人( http://abeak.blogspot.co.uk/2014/12/caching-laravel-with-varnish.html )使用 Session Monster包,它设置了 X-No-Session header ,如果用户未通过身份验证。这是一个 Laravel 4 包,所以我创建了一个中间件,如果用户未通过身份验证,它会设置 header 。

我不知道如何让 Varnish 仅在未设置 header 时将请求发送到后端。我将不胜感激任何指导!

编辑:

这个中间件设置了 X-No-Session如果用户未登录,则 header :

<?php

namespace App\Http\Middleware;

use Closure;
use Session;

class StripSessionsIfNotAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(auth()->check()) {
            return $next($request);
        }

        return $next($request)->header('X-No-Session', 'yeah');
    }
}

然后我将链接文章中的 VCL 转换为 VCL V4:
#
# 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.url ~ "^/auth" || req.method == "POST" || req.http.Authorization) {
        return (pass);
    }

    if (req.url ~ "\.(png|gif|jpg|css|js|ico|woff|woff2|svg)$") {
        unset req.http.cookie;

        return (hash); 
    }

    if (req.http.X-No-Session ~ "yeah" && req.method != "POST") {
        unset req.http.cookie;
    }

    return (hash);
}

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.

    set beresp.ttl = 1d;

    if (bereq.method == "GET" && bereq.url ~ "\.(png|gif|jpg|css|js|ico|woff|woff2|svg)$") {  
        unset beresp.http.set-cookie;

        set beresp.ttl = 5d;
    }

    return (deliver);
}

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.

    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

最佳答案

尝试更换

if (req.http. ~ "laravel_session") {
    return (pass);
}

在您的 sub vcl_recv功能与
if (req.http.X-No-Session ~ "yeah") {  
    unset req.http.cookie;  
}  

所以如果你有 X-No-Session header 然后你转储cookies。这反过来不会基于 cookie 传递到后端。目前您要说的是,如果有 cookie,请传递给后端。正如您已经知道的那样,总会有一个 cookie。

如果它也有任何帮助,here is my Varnish configuration .

关于laravel - 使 Varnish 仅在用户未登录时缓存页面(Laravel 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856233/

相关文章:

php - Laravel Eloquent 嵌套关系仅返回第一个元素的数据

wordpress - 使用 Laravel 将 Nginx 反向代理到 Wordpress

restart - Varnish 重启跟踪

apache - 如何在具有多个 IP 和域的专用服务器中设置 Varnish?

wordpress - Varnish 的第一个请求的最大年龄= 0

php - Laravel 5.3 中的搜索栏以过滤表格

php - 收到错误 "Typed property must not be accessed before initialization"- Laravel Livewire

php - 处理程序类错误 - Laravel

php - Varnish:如何根据特定 cookie 的值单独缓存页面

Varnish vcl_recv的默认行为