0

わかりましたので、Varnish を適切に動作させようとしていますが、説明できない奇妙な動作があります (理解不足が原因である可能性があります)。

これが私がやろうとしていることです:

GET http://scm.dev:6081/articles
If-None-Match: "50a0794aaca51fce202e86da428b7dca9b4bbff93"

そして私は得る:

HTTP/1.1 200 OK
Age: 3                                    // Correctly using a cached result
Content-Length: 3878
Content-Type: application/json
Date: Fri, 14 Dec 2012 10:59:34 GMT
Via: 1.1 varnish
X-Cacheable: YES                          // It is correct
Cache-Control: max-age=10, public, s-maxage=20
Etag: "50a0794aca51fce202e86da428b7dca9b4bbff93"
Vary: Accept,Accept-Encoding

ここで、リクエストに Cookie を追加すると、次のようになります。

GET http://scm.dev:6081/articles
If-None-Match: "50a0794aaca51fce202e86da428b7dca9b4bbff93"
Cookie: foo=bar

キャッシュされていない応答とキャッシュ不可の警告が表示されます。

HTTP/1.1 200 OK
Age: 0                                    // Always set to 0
Content-Length: 3878
Content-Type: application/json
Date: Fri, 14 Dec 2012 10:59:34 GMT
Via: 1.1 varnish
X-Cacheable: NO:Not Cacheable             // It is not correct
Cache-Control: max-age=10, public, s-maxage=20
Etag: "50a0794aca51fce202e86da428b7dca9b4bbff93"
Vary: Accept,Accept-Encoding

後の vcl 構成を確認するとX-Cacheable: NO:Not Cacheable、beresp.ttl が 0 以下の場合にヘッダーが設定されます。

なんで?

また、バックエンドが必要なときにパラメーターを正しく設定する責任を負う限り、 aCookieを返す a を持つ Request はキャッシュする必要があると想定しています。Cache-Control: publicpublic

Cookie を使用してリクエストを行うときに が設定されていると想定していましたX-Cacheable: NO:Got Sessionが、それ以上のことはありません。

varnish デーモンを起動するコマンド ライン:

/usr/sbin/varnishd -P /var/run/varnishd.pid -a :6081 -T 192.168.1.100:6082 -f /etc/varnish/custom.vcl -S /etc/varnish/secret -s malloc,256m

これが私のcustom.vcl

backend scm {
.host = "scm.dev";
.port = "80";
}

sub vcl_recv {
    set req.http.Surrogate-Capability = "abc=ESI/1.0";
}

sub vcl_fetch {
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }

    # Varnish determined the object was not cacheable
    if (beresp.ttl <= 0s) {
        set beresp.http.X-Cacheable = "NO:Not Cacheable";

    # You don't wish to cache content for logged in users
    } elsif (req.http.Cookie ~ "(foo)") {
        set beresp.http.X-Cacheable = "NO:Got Session";

        return(hit_for_pass);
    # You are respecting the Cache-Control=private header from the backend
    } elsif (beresp.http.Cache-Control ~ "private") {
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";

        return(hit_for_pass);
    # Varnish determined the object was cacheable
    } else {
        set beresp.http.X-Cacheable = "YES";
    }

    return(deliver);
}

sub vcl_hit {
    if (req.request == "PURGE") {
        set obj.ttl = 0s;
        error 200 "Purged";
    }
}

sub vcl_miss {
    if (req.request == "PURGE") {
        error 404 "Not purged";
    }
}
4

1 に答える 1

1

設計上、Cookie が存在する場合、Varnish はキャッシュからリクエストをフェッチしません。通常、Cookie は、応答が特定のユーザーに固有であることを示しています。

Cookie を気にしない場合、または特定の Cookie を保持したい場合はunset req.http.Cookie;、完全に許可することができます。vcl_recv()

if(req.http.Cookie) {
  if (req.http.Cookie !~ "(important_cookie|php_session)" ) {
    remove req.http.Cookie;
  }
}
于 2012-12-14T11:15:07.127 に答える