Install

Envrionment

$ uname -a && cat /etc/redhat-release 
Linux 34e3d501f93d 3.16.0-38-generic #52~14.04.1-Ubuntu SMP Fri May 8 09:43:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
CentOS release 6.6 (Final)

Install script

rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

yum install -y varnish

Check installation

$ varnishd -V && type varnishd
varnishd (varnish-4.0.3 revision b8c4a34)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2014 Varnish Software AS
varnishd is hashed (/usr/sbin/varnishd)

after installation, some important file/service:

  • varnish service /etc/init.d/
  • configuration file /etc/sysconfig/varnish
  • default vcl file /etc/varnish/default.vcl

Test Varnish

Sample modify default.vcl

Edit file /etc/varnish/default.vcl, and just modify default backend server as following:

16 backend default {
17     .host = "10.10.10.xx";
18     .port = "80";
19 }

Then, start varnish service

$ service varnish start
Starting Varnish Cache: /etc/init.d/varnish: line 57: ulimit: max locked memory: cannot modify limit: Operation not permitted
/etc/init.d/varnish: line 61: ulimit: max user processes: cannot modify limit: Operation not permitted
[ OK ]

Visit url

[root@148a0303ab23 workspace]# curl -I localhost:6081
HTTP/1.1 302 Moved Temporarily
X-Powered-By: Express
Location: /login
Vary: Accept
Content-Type: text/plain; charset=utf-8
Content-Length: 40
set-cookie: connect.sid=s%3A_rI8_NP6DgvxhkjTzN55ISwHiWcgbb-X.Gi91q0PhD0XcClvor9A9mwnWiloyC4LBpOVCIVZffck; Path=/; HttpOnly
Date: Wed, 17 Jun 2015 05:28:15 GMT
X-Varnish: 378160941
Age: 0
Via: 1.1 varnish
Connection: keep-alive

Check real-time log

[root@34e3d501f93d workspace]# varnishlog 
*   << BeReq    >> 65542     
-   Begin          bereq 65541 pass
-   Timestamp      Start: 1434518184.337495 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /login
-   BereqProtocol  HTTP/1.1
-   BereqHeader    Host: localhost:3000
-   BereqHeader    Cache-Control: max-age=0
-   BereqHeader    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   BereqHeader    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36
-   BereqHeader    Accept-Encoding: gzip, deflate, sdch
-   BereqHeader    Accept-Language: en-US,en;q=0.8
-   BereqHeader    Cookie: connect.sid=s5IjIv2vyugOdUJadSBo8Zj20d.8AzeiNPe7cwpbjStvYI3Bd2sz18NQLwECB6vyutMpAY
-   BereqHeader    If-None-Match: W/"R9Yuo2NBFsA=="
-   BereqHeader    X-Forwarded-For: 172.17.42.1
-   BereqHeader    X-Varnish: 65542
....
....
[root@148a0303ab23 workspace]# varnishncsa
172.17.42.1 - - [17/Jun/2015:05:55:36 +0000] "POST http://localhost:6081/login HTTP/1.1" 304 0 "http://localhost:6081/login" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
172.17.42.1 - - [17/Jun/2015:05:55:36 +0000] "GET http://localhost:6081/stylesheets/dashboard.css HTTP/1.1" 304 0 "http://localhost:6081/login" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
172.17.42.1 - - [17/Jun/2015:05:55:37 +0000] "GET http://localhost:6081/stylesheets/bootstrap.min.css HTTP/1.1" 304 0 "http://localhost:6081/login" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
172.17.42.1 - - [17/Jun/2015:05:55:37 +0000] "GET http://localhost:6081/images/logo2.png HTTP/1.1" 304 0 "http://localhost:6081/login" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"

Even, you can cache POST response

Change vcl file, like following example:

sub vcl_recv { 
    set req.http.x_method = req.request;
    if (req.http.x-info == "error-recv") {
        error 403 "error in recv function";
    }
    return (lookup);
}
[root@148a0303ab23 workspace]# curl -s -d'username=a&password=b' -H "x-info: error-recv" -D- -o/dev/null http://localhost:6081/login
HTTP/1.1 403 error in recv function
Server: Varnish
Retry-After: 0
Content-Type: text/html; charset=utf-8
Content-Length: 427
Date: Wed, 17 Jun 2015 06:25:16 GMT
X-Varnish: 157252118
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS
x-test: POST

[root@148a0303ab23 workspace]# curl -s -d'username=a&password=b' -H "x-info: error-recv1" -D- -o/dev/null http://localhost:6081/login
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
ETag: W/"R9cuBlSmrLd3fYuo2NBFsA=="
set-cookie: connect.sid=s%3An_AWCqv9JirHR81Pb-aBYke8xaL_X6eB.Ns8YElYH30lXvh%2BML%2BYuoXcZ6T3k4GCPfdKMeqNhrTw; Path=/; HttpOnly
Content-Length: 2286
Date: Wed, 17 Jun 2015 06:25:22 GMT
X-Varnish: 157252119
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS
x-test: POST

References