Varnish - okashoi/isucon-cheat-sheet GitHub Wiki

リバースプロキシ

ドキュメント

https://varnish-cache.org/docs/6.2/

エラーが起こるので解消方法がわかるまで、古いバージョンを使う。

https://varnish-cache.org/docs/5.2/

[参考]エラーの内容

# systemctl start varnish
Failed to restart varnish.service: Unit varnish.service is masked.

systemctl status varnish で確認すると Manager got SIGINT って出てる

パッケージマネージャからインストール

apt-get install varnish

バージョンの確認

varnishd -V

ソースからコンパイル(最新版)

以下の通りにやる。

https://varnish-cache.org/docs/6.2/installation/install.html#build-dependencies-on-debian-ubuntu

apt-get install \
    make \
    automake \
    autotools-dev \
    libedit-dev \
    libjemalloc-dev \
    libncurses-dev \
    libpcre3-dev \
    libtool \
    pkg-config \
    python3-docutils \
    python3-sphinx \
    autoconf-archive \
    git
git clone https://github.com/varnishcache/varnish-cache 
cd varnish-cache
sh autogen.sh
./configure
make
make install

確認

which varnishd

使い方

起動

systemctl start varnish

または

varnishd -a :6081 -T localhost:6082 -b localhost:8080

忘れないうちに enable しとこう

systemctl enable varnish

設定ファイル

/etc/varnish/default.vcl

基本は以下の箇所を目的に合わせて編集すればよい。

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

再起動

systemctl restart varnish

80 番ポートで LISTEN させる

https://varnish-cache.org/docs/6.2/tutorial/putting_varnish_on_port_80.html

その前に nginx (等の web サーバ)を適当な別のポートで LISTEN するようにしておく。

  server {
    listen 10080;
    location / {
      proxy_set_header Host $host;
      proxy_pass http://app;
    }
  }

/etc/systemd/system/varnish.service.d/customexec.conf を作成し、以下の内容を記述。

mkdir -p /etc/systemd/system/varnish.service.d
vim /etc/systemd/system/varnish.service.d/customexec.conf

最新版の場合

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s default,256m

v5.2 の場合

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

-F の根拠は下記 issue を参照。

/etc/systemd/system/varnish.service を編集すればよい?

再起動

systemctl daemon-reload
systemctl restart varnish

ログの設定

Varnish NCSAという別プロセスで動いている。

設定ファイルは

/lib/systemd/system/varnishncsa.service にある。

Kataribe 用設定

https://github.com/matsuu/kataribe#varnishncsa