a-blog cms + Varnish 触ってみる





User Request --> Nginx:80 --> Varnish:6081 --> Nginx:8080 --> php-fpm:9000


User Request --> Varnish:80 --> Nginx:8080 --> php-fpm:9000




$ sudo vim /etc/sysconfig/varnish
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"


$ sudo service varnish start


# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

import std;

# 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.
  
  set req.http.Host = req.http.host;
  set req.http.X-Real-IP = client.ip;
  set req.http.X-Forwarded-Host = req.http.host;
  set req.http.X-Forwarded-Server = req.http.host;
  set req.http.X-Forwarded-For = client.ip;

  # GET以外はキャッシュしない
  if ( req.method != "GET" ) {
    return (pass);
  }

  # ttl を抜き出し
  if ( req.url ~ "(\?|&)ttl=" ) {
    set req.http.x-varnish-ttl = regsub(req.url, "^(.*)ttl=([^&]+)(.*)?$", "\2");
  }

  # ログイン中でESIなら cookieを削除してキャッシュ処理
  if ( req.http.Cookie && req.http.Cookie ~ "sid=" && req.http.x-varnish-ttl ) {
    set req.http.x-varnish-hash = regsub(req.http.cookie, "^(.* )?sid=([^;]+)(;.*)?$", "\2");
    set req.http.x-varnish-suid = regsub(req.http.cookie, "^(.* )?suid=([^;]+)(;.*)?$", "\2");

    set req.http.x-varnish-tmpcache = req.http.Cookie; # キャッシュを利用するためcookieを一時退避
    unset req.http.Cookie; # cookie を削除

    return (hash);
  }

  # 通常ログインのアクセスならキャッシュしない
  if ( req.http.Cookie && req.http.Cookie ~ "sid=" ) {
    return (pass);
  }

  unset req.http.cookie; # cookieを削除
  return (hash);
}

sub vcl_miss {

  # cookieを再セット
  if ( req.http.x-varnish-tmpcache ) {
    set req.http.Set-Cookie = req.http.x-varnish-tmpcache;
  }
  return (fetch);
}

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.
  
  # ESI を有効化
  set beresp.do_esi = true;
  
  # TTL を設定
  if ( bereq.http.x-varnish-ttl ) {
    unset beresp.http.set-cookie;
    set beresp.ttl = std.duration(bereq.http.x-varnish-ttl, 1m);
  }
  
  # image 
  if ( bereq.url ~ "\.(png|gif|jpg)$" ) {
    unset beresp.http.set-cookie;
    set beresp.ttl = 1d;
  }
}

sub vcl_hash {
  
  # ログインユーザ毎にキャッシュを振り分け
  if ( req.http.x-varnish-suid ) {
    hash_data(req.http.x-varnish-suid);
  }
}


if ( req.url ~ "(\?|&)ttl=" ) {
  set req.http.x-varnish-ttl = regsub(req.url, "^(.*)ttl=([^&]+)(.*)?$", "\2"); # 正規表現でttlを抜き出しreq.http.x-varnish-ttlにセット
}


if ( bereq.http.x-varnish-ttl ) {
  unset beresp.http.set-cookie;
  set beresp.ttl = std.duration(bereq.http.x-varnish-ttl, 1m);
}


<body>
  <!-- ヘッダーは一週間キャッシュ -->
  <esi:include src="/include/header.html?ttl=1w">

  <div class="row">
    <div class="large-9 columns">
    <!-- Main Blog Content -->
    <!--#include file="/include/mainTop.html" -->
    </div>
    <div class="large-3 columns">
    <!-- サイドナビは1時間キャッシュ -->
    <esi:include src="/include/sidebar.html?ttl=1h">
  </div>
</div>
...


public function afterBuild(&$res)
{
  if ( SUID ) {
    setcookie('suid', SUID, REQUEST_TIME + 604800, '/', COOKIE_HOST, COOKIE_USESECURE);
  } else {
    setcookie('suid', null, REQUEST_TIME - 1, '/', COOKIE_HOST, COOKIE_USESECURE);
  }
}


# ログイン中でESIなら cookieを削除してキャッシュ処理
if ( req.http.Cookie && req.http.Cookie ~ "sid=" && req.http.x-varnish-ttl ) {
  set req.http.x-varnish-suid = regsub(req.http.cookie, "^(.* )?suid=([^;]+)(;.*)?$", "\2"); # suidを取得

  set req.http.x-varnish-tmpcache = req.http.Cookie; # キャッシュを利用するためcookieを一時退避
  unset req.http.Cookie; # cookie を削除

  return (hash);
}


sub vcl_hash {
  # ログインユーザ毎にキャッシュを振り分け
  if ( req.http.x-varnish-suid ) {
    hash_data(req.http.x-varnish-suid);
  }
}


関連記事

a-blog cmsをリバースプロキシ(nginx)で高速化

a-blog cms テンプレート解体新書

Docker + a-blog cms

Document Outliner

アウトライン生成ライブラリ、document-outliner をリリースしました

a-blog cmsでSVGを画像プレイスホルダーとして使う

Docker で作る a-blog cms ローカル開発環境

最新記事

カテゴリー

ハッシュタグ