没有用户上下文 - 有FOSHttpCacheBundle + Varnish

时间:2015-01-27 14:09:11

标签: symfony caching varnish

我是法国人,对不起我的英语。

我的配置: 80号港口的清漆 端口8080上的Apache2 Symfony:2.5.9

我不能拥有X-User-Context-Hash我的响应标头。你可以帮助我或指点我,因为我阻止了。

config.vcl:

同样在这里:http://foshttpcache.readthedocs.org/en/latest/varnish-configuration.html#varnish-user-context,除了我用“/ user-context-hash”改变了“/user_context_hash.php”

sub vcl_recv {

    # Prevent tampering attacks on the hash mechanism
    if (req.restarts == 0
        && (req.http.accept ~ "application/vnd.fos.user-context-hash"
            || req.http.x-user-context-hash
        )
    ) {
        return (synth(400));
    }

    # Lookup the context hash if there are credentials on the request
    # Only do this for cacheable requests. Returning a hash lookup discards the request body.
    # https://www.varnish-cache.org/trac/ticket/652
    if (req.restarts == 0
        && (req.http.cookie || req.http.authorization)
        && (req.method == "GET" || req.method == "HEAD")
    ) {
        # Backup accept header, if set
        if (req.http.accept) {
            set req.http.x-fos-original-accept = req.http.accept;
        }
        set req.http.accept = "application/vnd.fos.user-context-hash";

        # Backup original URL
        set req.http.x-fos-original-url = req.url;
        set req.url = "/user-context-hash";

        # Force the lookup, the backend must tell not to cache or vary on all
        # headers that are used to build the hash.
        return (hash);
    }

    # Rebuild the original request which now has the hash.
    if (req.restarts > 0
        && req.http.accept == "application/vnd.fos.user-context-hash"
    ) {
        set req.url = req.http.x-fos-original-url;
        unset req.http.x-fos-original-url;
        if (req.http.x-fos-original-accept) {
            set req.http.accept = req.http.x-fos-original-accept;
            unset req.http.x-fos-original-accept;
        } else {
            # If accept header was not set in original request, remove the header here.
            unset req.http.accept;
        }

        # Force the lookup, the backend must tell not to cache or vary on the
        # user hash to properly separate cached data.

        return (hash);
    }
}

sub vcl_backend_response {
    if (bereq.http.accept ~ "application/vnd.fos.user-context-hash"
        && beresp.status >= 500
    ) {
        return (abandon);
    }
}

sub vcl_deliver {
    # On receiving the hash response, copy the hash header to the original
    # request and restart.
    if (req.restarts == 0
        && resp.http.content-type ~ "application/vnd.fos.user-context-hash"
    ) {
        set req.http.x-user-context-hash = resp.http.x-user-context-hash;

        return (restart);
    }

    # If we get here, this is a real response that gets sent to the client.

    # Remove the vary on context user hash, this is nothing public. Keep all
    # other vary headers.
    set resp.http.Vary = regsub(resp.http.Vary, "(?i),? *x-user-context-hash *", "");
    set resp.http.Vary = regsub(resp.http.Vary, "^, *", "");
    if (resp.http.Vary == "") {
        unset resp.http.Vary;
    }

    # Sanity check to prevent ever exposing the hash to a client.
    unset resp.http.x-user-context-hash;
}

config_cache.yml

fos_http_cache:
    user_context:
        enabled: true
        role_provider: true
    cache_control:
        defaults:
            overwrite: true
        rules:
            -
                match:
                    path: ^/
                headers:
                    overwrite: true
                    cache_control:
                        public: true
                        max_age: 60000
                        s_maxage: 56000
                    last_modified: "-1 hour"
                    vary: [Accept-Encoding, Accept-Language]

routing_cache.yml:

user_context_hash:
    path: /user-context-hash

app_dev.php:

/*require_once __DIR__.'/../app/AppCache.php';*/
/*$kernel = new AppCache($kernel);*/

composer.json:

"friendsofsymfony/http-cache-bundle": "1.2.0"

Réponse标题:

headers 
Date: Mon, 26 Jan 2015 20:11:48 GMT 
Server: Apache/2.2.22 (Debian) 
X-Powered-By: PHP/5.4.35-0+deb7u2
Cache-Control: max-age=60000, public, s-maxage=56000 
X-Cache-Debug: 1
Last-Modified: Mon, 26 Jan 2015 19:11:48 GMT 
x-url: /
x-host: datav1.XXX.com
X-Varnish: 229440 229433
Age: 84 
Via: 1.1 varnish-v4 
Vary: Accept-Encoding,Accept-Language 
Content-Encoding: gzip 
Content-Type: text/html; charset=UTF-8 
Accept-Ranges: bytes 

THX!

1 个答案:

答案 0 :(得分:0)

Varnish在内部发出多个子请求(称为“重启”)来处理捆绑包提供的x-user-context-hash功能。

正如您在vcl_deliver的最后一行中所看到的那样,您取消了该标头,因此它不会在响应标头中公开。您可以在进行开发时注释掉该行,但不建议在生产时使用。