1. 개요

Varnish Cache를 설치한다.


2. 환경

우분투 리눅스이다.


3. 설치

3-1. apt-get update

$ sudo apt-get update

3-2. apt-get install

$ sudo apt-get install varnish
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  binutils cpp cpp-5 gcc gcc-5 gcc-5-base libasan2 libatomic1 libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libgcc-5-dev libgomp1 libisl15 libitm1 libjemalloc1 liblsan0 libmpc3 libmpx0 libquadmath0
  libstdc++6 libtsan0 libubsan0 libvarnishapi1 linux-libc-dev manpages-dev
Suggested packages:
  binutils-doc cpp-doc gcc-5-locales gcc-multilib make autoconf automake libtool flex bison gdb gcc-doc gcc-5-multilib gcc-5-doc libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan2-dbg
  liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libmpx0-dbg libquadmath0-dbg glibc-doc varnish-doc
The following NEW packages will be installed:
  binutils cpp cpp-5 gcc gcc-5 libasan2 libatomic1 libc-dev-bin libc6-dev libcc1-0 libcilkrts5 libgcc-5-dev libgomp1 libisl15 libitm1 libjemalloc1 liblsan0 libmpc3 libmpx0 libquadmath0 libtsan0 libubsan0
  libvarnishapi1 linux-libc-dev manpages-dev varnish
The following packages will be upgraded:
  gcc-5-base libstdc++6
2 upgraded, 26 newly installed, 0 to remove and 22 not upgraded.
Need to get 28.6 MB of archives.
After this operation, 102 MB of additional disk space will be used.
Do you want to continue? [Y/n]

진행을 하면 한참 설치를 하고..

Setting up libvarnishapi1 (4.1.1-1ubuntu0.2) ...
Setting up manpages-dev (4.04-2) ...
Setting up libjemalloc1 (3.6.0-9ubuntu1) ...
Setting up varnish (4.1.1-1ubuntu0.2) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for ureadahead (0.100.0-19) ...

4. Back-end 설정

4-1. 파일

/etc/varnish/default.vcl

4-2. 설정

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

위 부분을 실제 Back-end 서버 정보로 변경한다.


5. Daemon 설정

5-1. 파일

  • /etc/default/varnish
  • /lib/systemd/system/varnish.service

5-2. /etc/default/varnish 파일 설정

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

-a 부분의 포트를 변경한다.

5-3. /lib/systemd/system/varnish.service 파일 설정

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

-a 부분의 포트를 변경한다.

5-4. 반영

$ sudo systemctl daemon-reload
$ sudo service varnish restart

6. 오류

6-1. 503

Error 503 Backend fetch failed
Backend fetch failed

Guru Meditation:
XID: 32773
  • Back-end  서버가 기동되어 있거나 정상적으로 포트를 바인딩하고 있는지 확인한다.

6-2. ERR_CONNECTION_CLOSED

사이트에 연결할 수 없음
*** 와(과)의 연결이 예기치 않게 종료되었습니다.
  • Back-end (Apache HTTP Server)의 Access log에도 Varnish가 접속한 로그가 존재한다.
  • 확인 결과 302로 처리되고 있었고, 더 확인 결과 http -> https로 리다이렉트 처리되고 있었다. http -> https 리다이렉트를 제거하여 정상 처리되었다.