Varnish4.1 では最初の `Set-Cookie` しか read/write できない

先に結論

  • Varnish4.1 では最初の Set-Cookie しか read/write できないし、writeすると他の Set-Cookie は消える
  • varnish-modulesのcookieやheaderを使えば、他の Set-Cookie もread/writeできる

サンプルコード

vcl

vcl 4.0;

import std;
import directors;

sub vcl_init {
}

sub vcl_recv {
  return (hash);
}

sub vcl_pipe {
  return (pipe);
}

sub vcl_pass {
}

sub vcl_hash {
  hash_data(req.url);
}

sub vcl_hit {
  return (fetch);
}

sub vcl_miss {
  return (fetch);
}

sub vcl_backend_response {
  return (deliver);
}

sub vcl_deliver {
  if ( resp.http.set-cookie ~ "abc=123" ){
    set resp.http.set-cookie = "zzz=123";
  }

  return (deliver);
}

sub vcl_purge {
}

sub vcl_synth {
  return (deliver);
}


sub vcl_fini {
  return (ok);
}

varnishtest

varnishtest "example"

server s1 {
    rxreq
    expect req.url == "/"
    txresp -hdr "set-cookie: abc=123"  -hdr "set-cookie: def=456"
}
server s1 -start

varnish v1 -vcl+backend {
    include "/etc/varnish/default.vcl";
} -start

client c1 {
    txreq -url "/"
    rxresp
    expect resp.status == 200
    expect resp.http.set-cookie == "zzz=123"
    expect resp.http.set-cookie == "def=456"

} -run

テストの実行結果(一部)

**** c1    1.7 http[ 0] | HTTP/1.1
**** c1    1.7 http[ 1] | 200
**** c1    1.7 http[ 2] | OK
**** c1    1.7 http[ 3] | Content-Length: 0
**** c1    1.7 http[ 4] | Date: Mon, 06 May 2019 11:15:49 GMT
**** c1    1.7 http[ 5] | X-Varnish: 1001
**** c1    1.7 http[ 6] | Age: 0
**** c1    1.7 http[ 7] | Via: 1.1 varnish-v4
**** c1    1.7 http[ 8] | set-cookie: zzz=123
**** c1    1.7 http[ 9] | Accept-Ranges: bytes
**** c1    1.7 http[10] | Connection: keep-alive

vcl_deliver にある set resp.http.set-cookie = "zzz=123"; が実行されると、 set-cookie: def=456" が消えてしまう...!

結論

複数のcookieをread/writeしたい時には、varnish-modules の cookie を使いましょう

参考

labs.gree.jp

labs.gree.jp