当用户未登录时,使Varnish仅缓存页面(Laravel 5)

Chr*_*ris 5 varnish laravel

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

我无法弄清楚如何在没有设置标头的情况下让Varnish将请求发送到后端.我非常感谢任何指导!

编辑:

X-No-Session如果用户未登录,此中间件将设置标头:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我将链接文章中的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";
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*idT 0

尝试更换

if (req.http. ~ "laravel_session") {
    return (pass);
}
Run Code Online (Sandbox Code Playgroud)

在你的sub vcl_recv函数中

if (req.http.X-No-Session ~ "yeah") {  
    unset req.http.cookie;  
}  
Run Code Online (Sandbox Code Playgroud)

这样,如果您确实有X-No-Session标头,那么您就可以转储 cookie。这又不会根据cookie传递到后端。现在你说如果有cookie,则传递到后端。正如您所知,总会有一个 cookie。

如果它也有任何帮助,这是我的 Varnish 配置