Reverse Proxy Varnish Cache With Apache On CentOS 6
TAG : How to Install Varnish Cache With Apache On CentOS 6.x
Before Varnish :
client –http port tcp 80 –> Apache Web Server
After Install Varnish :
client –http port tcp 80 –> Varnish – http port tcp 8088 -> Apache Web Server
Setup varnish repo :
[root@centos ~]# wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm
[root@centos ~]# rpm –nosignature -i varnish-release-3.0-1.el6.noarch.rpm
Install Varnish and Apache :
[root@centos ~]# yum install varnish -y
[root@centos ~]# yum install httpd -y
chkconfig for start at boot :
[root@centos ~]# chkconfig –level 345 varnish on
[root@centos ~]# chkconfig –level 345 httpd on
Configure apache to listen to port 8088 :
[root@centos ~]# vi /etc/httpd/conf/httpd.conf
Modify below :
change :
Listen 80
to
Listen 8088
Configuring Varnish Cache:
[root@centos ~]# vim /etc/sysconfig/varnish
========================== Config Varnish Start =================
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#
# Maximum number of open files (for ulimit -n)
NFILES=131072
# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000
# Set this to 1 to make init script reload try to switch vcl without restart.
# To make this work, you need to set the following variables
# explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS,
# VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE, or in short,
# use Alternative 3, Advanced configuration, below
RELOAD_VCL=1
# This file contains 4 alternatives, please use only one.
DAEMON_OPTS=”-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,1G”
========================== Config Varnish End =================
Add the following in /etc/varnish/default.vcl :
[root@centos ~]# vim /etc/varnish/default.vcl
===== config default.vcl start =======
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = “127.0.0.1”;
.port = “8088”;
}
# Respond to incoming requests
sub vcl_recv {
# Set the director to cycle between web servers.
#set req.backend = web_director;
if (req.url ~ “^/server_status\.php$”) {
return (pass);
}
# Always cache the following file types for all users.
if (req.url ~ “(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9]+)?$”) {
unset req.http.Cookie;
}
}
sub vcl_hash {
}
# Code determining what to do when serving items from the Apache servers.
sub vcl_fetch {
# Don’t allow static files to set cookies.
if (req.url ~ “(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9]+)?$”) {
# beresp == Back-end response from the web server.
unset beresp.http.set-cookie;
}
# Allow items to be stale if needed.
set beresp.grace = 6h;
}
## Deliver
sub vcl_deliver {
## We’d like to hide the X-Powered-By headers. Nobody has to know we can run PHP and have version xyz of it.
remove resp.http.X-Powered-By;
}
===== config default.vcl End =======
Start Varnish and Apache
[root@centos ~]# service varnish start
Starting Varnish Cache: [ OK ]
[root@centos ~]# service httpd restart
Starting httpd: [ OK ]
verify Port Service varnish and Apache :
[root@centos ~]# netstat -plunt | grep :80
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 6054/httpd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8507/varnishd
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 6054/httpd
tcp 0 0 0.0.0.0:8088 0.0.0.0:* LISTEN 6054/httpd
tcp 0 0 :::80 :::* LISTEN 8507/varnishd
Verify the Varnish run Reverse Proxy for pache the following command.
[root@centos ~]# curl -I http://blog.thaieasydns.com
HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
X-Pingback: http://blog.thaieasydns.com/xmlrpc.php
Content-Type: text/html; charset=UTF-8
Content-Length: 32942
Date: Wed, 10 Jun 2015 06:25:01 GMT
X-Varnish: 1112037056
Age: 0
Via: 1.1 varnish
Connection: keep-alive
ApacheBench performance test without Varnish Cache
[root@centos ~]# ab -k -n 1000 -c 50 http://blog.thaieasydns.com:8088/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking blog.thaieasydns.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Apache/2.2.15
Server Hostname: blog.thaieasydns.com
Server Port: 8088
Document Path: /
Document Length: 0 bytes
Concurrency Level: 50
Time taken for tests: 7.485 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 1000
Total transferred: 336512 bytes
HTML transferred: 0 bytes
Requests per second: 133.59 [#/sec] (mean)
Time per request: 374.270 [ms] (mean)
Time per request: 7.485 [ms] (mean, across all concurrent requests)
Transfer rate: 43.90 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 5
Processing: 30 163 493.4 97 6860
Waiting: 30 163 493.4 97 6859
Total: 30 163 493.9 97 6864
Percentage of the requests served within a certain time (ms)
50% 97
66% 105
75% 114
80% 122
90% 139
95% 156
98% 1758
99% 2905
100% 6864 (longest request)
ApacheBench performance test with Varnish Cache
[root@centos ~]# ab -k -n 1000 -c 50 http://blog.thaieasydns.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking blog.thaieasydns.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Apache/2.2.15
Server Hostname: blog.thaieasydns.com
Server Port: 80
Document Path: /
Document Length: 32942 bytes
Concurrency Level: 50
Time taken for tests: 0.408 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 1000
Total transferred: 33494360 bytes
HTML transferred: 33205075 bytes
Requests per second: 2449.02 [#/sec] (mean)
Time per request: 20.416 [ms] (mean)
Time per request: 0.408 [ms] (mean, across all concurrent requests)
Transfer rate: 80105.74 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.2 0 7
Processing: 1 13 32.6 4 251
Waiting: 0 10 32.7 1 247
Total: 1 13 33.7 4 256
Percentage of the requests served within a certain time (ms)
50% 4
66% 4
75% 4
80% 5
90% 25
95% 46
98% 182
99% 183
100% 256 (longest request)
Complete !!!!