Simple setup of Varnish in Red Hat Linux 7
Host environment rhel6 selinux and iptables disabled
Lab host 172.25.254.2 varnish
172.25.254.3 apache
172.25.254.4 apache
First obtain the two packages
Varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
Install
Yum install varnish-*-y
### Configure a backend server
Vim/etc/varnish/default. vcl
Backend xp1 {
. Host = "172.25.254.3 ";
. Port = "80 ";
}
3 is the backend. Create a homepage for testing.
### Configure the varnish service port
VARNISH_LISTEN_PORT = 80
/Etc/init. d/varnish restart
Add the following code in vcl to make the test display clearer:
Sub vcl_deliver {
If (obj. hits> 0 ){
Set resp. http. X-Cache = "HIT from xp1 cache ";
}
Else {
Set resp. http. X-Cache = "MISS from xp1 cache ";
}
Return (deliver );
}
/Etc/init. d/varnish restart
### Test cache hit
On 4
[Root @ vm4 ~] # Curl 172.25.254.2
Haha-vm3
Age is followed by time. The default value is 120.
HIT indicates that if MISS is HIT, the query is performed at the backend.
### Manually clear the cache through varnishadm
# Varnishadm ban. url. * $
# Clear all
# Varnishadm ban. url/index.html
# Clear index.html page Cache
# Varnishadm ban. url/admin/$
# Clear admin directory Cache
### Define backend servers for multiple sites with different domain names
Backend xp1 {
. Host = "172.25.39.3 ";
. Port = "80 ";
}
Backend xp2 {
. Host = "172.25.39.4 ";
. Port = "80 ";
}
# When accessing the domain name www.bkjia.org, retrieve data from xp1 and access the domain name bbs.bkjia.org to retrieve data from xp2. An error is reported when accessing other pages.
Sub vcl_recv {
If (req. http. host ~ "^ (Www .)? Bkjia.com "){
Set req. http. host = "www.bkjia.com ";
Set req. backend = xp1;
} Elsif (req. http. host ~ "^ Bbs.bkjia.com "){
Set req. backend = xp2;
} Else {error 404 "bkjia cache ";
}
}
### Define Server Load balancer
Director lb round-robin {
{. Backend = xp1 ;}
{. Backend = xp2 ;}
}
The rr algorithm is used above. There are other algorithms such as hash, fallback, and random.
Sub vcl_recv {
If (req. http. host ~ "^ (Www .)? Bkjia.com "){
Set req. http. host = "www.bkjia.com ";
Set req. backend = lb;
Return (pass); # Do not cache for test convenience.
} Elsif (req. http. host ~ "^ Bbs.bkjia.com "){
Set req. backend = xp2;
} Else {
Error 404 "bkjia cache ";
}
}
Test Results
### Varnish cdn push Platform
Http://code.google.com/p/varnish-php-bansys/
# Install php support
Unzip bansys.zip-d/var/www/html
Vim/var/www/html/bansys/config. php # Only keep the following settings, and comment out the remaining Settings <? Php
$ Var_group1 = array (
'Host' => array ('2017. 25.254.2 '),
'Port' => '123 ',
);
// Varnish group definition
// Bind the host list
$ VAR_CLUSTER = array (
'Www .bkjia.com '=> $ var_group1,
);
// Varnish version // 2.x and 3.x push commands are different
$ VAR_VERSION = "3 ";
?>
Bansys has two working modes: telnet and http.
In telnet mode, disable the varnish Service Management port verification and comment out "-S $" in the/etc/sysconfig/varnish file.
{VARNISH_SECRET_FILE} ", restart the varnish service.
For http mode, the following settings are required:
Vim/etc/varnish/default. vcl
Acl bkjia {
# Set access control
"127.0.0.1 ";
"172.25.254.0"/24;
}
Sub vcl_recv {
If (req. request = "BAN "){
If (! Client. ip ~ Bkjia ){
Error 405 "Not allowed .";
}
Ban ("req. url ~ "+ Req. url );
Error 200 "ban added ";
}
}
Then you can view it in the browser.
Concepts of Cache Server Varnish
Concepts of Cache Server Varnish
Structural notes for Varnish Cache
Install and configure Varnish-5.8 in CentOS 2.1.5
The RedHat script uses the CentOS source to update and install Nginx, PHP 5.3, and Varnish.
Using Varnish to build Cache Server notes
Install and configure the cache service Varnish
Preparations for Varnish compilation and Installation
Configuration Optimization of Varnish cache in Linux
Varnish details: click here
Varnish: click here
This article permanently updates the link address: