LoginSignup
7
8

More than 5 years have passed since last update.

CentOS 6.5でvanish 4.0.1を動かす為の最低限の設定

Last updated at Posted at 2014-06-26

実運用するにはほど遠いのですが、varnish 4.0.1の設定をしてみたので作業メモを。

前提条件

まず、varnishには余り細かい設定を追加せずに、基本的にはリバースプロキシとしての動作に専念させたいと思っています。その方がVCLが綺麗に保てるし、チューニングなどをする場合のメンテナンス性も高いのでは無いかなと。

そのため、前段にnginxなどのHTTPサーバが居て、varnishなどへの振り分け処理を行う前提とします。後段には実際のコンテンツを保持しているHTTPサーバが居るので、varnishに期待している役割はキャッシュやロードバランスなどになります。

※ということで、フロントに置く場合に必要そうな設定などは想定していません。

作業手順

yumリポジトリ設定

参考:Installation on RedHat

yumリポジトリ追加&インストール
$ sudo rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
$ sudo yum -y install varnish
$ varnishd -V
varnishd (varnish-4.0.1 revision 4354e5e)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2011 Varnish Software AS

公式の最新版からインストールします。

インストールが終わったら/etc/yum.repos.d/varnish.repoenable=0にしておいてもいいかもしれません。

初期設定

まずは起動パラメータ等を変更するために/etc/sysconfig/varnishを変更します。

/etc/sysconfig/varnish.diff
@@ -62,7 +62,7 @@
 # # Default address and port to bind to
 # # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
 # # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
-# VARNISH_LISTEN_ADDRESS=
+VARNISH_LISTEN_ADDRESS=127.0.0.1
 VARNISH_LISTEN_PORT=6081
 #
 # # Telnet admin interface listen address and port

フロントにnginxが居る想定、ということで、ローカルホストからのみ受信するように設定を絞ります。
不特定のアドレスに対して公開する場合などであれば、ここの設定変更は不要かもしれません。

※因みに、iptablesなどで締めている場合は当然そちらの設定変更も必要になります。

その他に気になるところとしては、ストレージサイズの設定変更があるのですが、初期値がmalloc,256Mだったので、ここはこれで充分かなと思いそのままにしました。

/etc/varnish/default.vcl
vcl 4.0;

backend www_host {
    .host = "xxx.xxx.xxx.xxx";
    .port = "80";
}
backend com_host {
    .host = "xxx.xxx.xxx.xxx";
    .port = "80";
}
backend net_host {
    .host = "xxx.xxx.xxx.xxx";
    .port = "80";
}

sub vcl_recv {
    if (req.http.host == "www.example.org") {
        set req.backend_hint = www_host;
    }
    else if (req.http.host == "www.example.com") {
        set req.backend_hint = com_host;
    }
    else if (req.http.host == "www.example.net") {
        set req.backend_hint = net_host;
    }
    else {
        return (synth(403, "Forbidden"));
    }
}

現時点では細かいチューニングなどは実施していませんし、ディレクターの設定などもしておらず、単にドメイン毎にバックエンドを切り替えるだけの設定でしかありません。vcl_recvでreq.http.hostのチェックをして、適切な場合にバックエンドの設定をし、当て嵌まらない場合は403でリジェクトするポリシーになります。

  • 先頭のvcl記述
  • バックエンドの指定はreq.backend_hintで設定
  • errorじゃなくてsynthを使う

↑ VCL 4.0に合わせて記載してみたのですが、この辺がこれまでとの違いなのかな…
参考:Version statement, Directors have been moved to the vmod_directors, error() is now synth()

VCLを作成したら構文チェックをしておきたいのですが、/var/lib/varnish/$HOSTNAMEあたりのディレクトリ書き込み権限が無くてエラーになってしまうのでsudoしています(^_^;)

※適切なディレクトリのパーミッション設定をすればいいのだろうか…

vclスクリプトの構文チェック
$ sudo varnishd -C -f default.vcl > /dev/null
Message from VCC-compiler:
Backend host '"xxx.xxx.xxx.xxx"' could not be resolved to an IP address:
    Name or service not known
(Sorry if that error message is gibberish.)
('input' Line 4 Pos 13)
    .host = "xxx.xxx.xxx.xxx";
------------#################-

Running VCC-compiler failed, exit 1

VCL compilation failed
$ vi default.vcl
$ sudo varnishd -C -f default.vcl > /dev/null
$ 

何も考えずに作ってみた物をコピペして構文チェックしてみると、上記の様にエラーになりますw

構文エラーと言われたIPアドレス部分を適切に設定すれば構文エラー無しで何も表示されません(/dev/nullに捨てていれば)

varnishサービスの起動

という感じで動作確認します。

varnish起動
$ sudo service varnish start
Starting Varnish Cache: 0
                                                           [  OK  ]
$ curl --head --header "Host: www.example.org" http://localhost:6081/
HTTP/1.1 200 OK
Server: Apache
Via: 1.1 varnish (v4)
()
$ curl --head --header "Host: www.example.com" http://localhost:6081/
HTTP/1.1 200 OK
Server: Apache
Via: 1.1 varnish (v4)
()
$ curl --head --header "Host: www.example.net" http://localhost:6081/
HTTP/1.1 200 OK
Server: Apache
Via: 1.1 varnish (v4)
()
$ curl --head http://localhost:6081/
HTTP/1.1 403 Forbidden
Server: Varnish
()

各ドメイン毎の挙動確認と未指定時のエラー確認ができました。

varnishncsaサービスの起動

大した事はしていないので、敢えてログサービス使わないという選択肢もいいのではないかと思うのですが、一応varnishncsaだけは動かしておくことにします。

varnishncsa起動
$ rpm -ql varnish | grep logrotate
/etc/logrotate.d/varnish
$ sudo service varnishncsa start
Starting varnish ncsa logging daemon:                      [  OK  ]
$ cd /var/log/varnish
$ tail -F varnishncsa.log
()

一応/etc/logrotate.d/varnishがパッケージに含まれて(rotateされて)居ることを確認する程度には気が小さいですw

自動起動設定

chkconfig設定
$ chkconfig --list | grep varnish
varnish         0:off   1:off   2:off   3:off   4:off   5:off   6:off
varnishlog      0:off   1:off   2:off   3:off   4:off   5:off   6:off
varnishncsa     0:off   1:off   2:off   3:off   4:off   5:off   6:off
$ sudo chkconfig varnish on
$ sudo chkconfig varnishncsa on
$ chkconfig --list | grep varnish
varnish         0:off   1:off   2:on    3:on    4:on    5:on    6:off
varnishlog      0:off   1:off   2:off   3:off   4:off   5:off   6:off
varnishncsa     0:off   1:off   2:on    3:on    4:on    5:on    6:off

自動起動の設定としても、varnishlogは動かさないままにしておきます。

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8