Simple setup of Varnish in RedHatLinux7

Source: Internet
Author: User
Tags varnish
Host environment rhel6selinuxandiptablesdisabled lab host 172.25.254.2varnish172.25.254.3apache &

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 www.linuxidc.org domain name, retrieve data from xp1 and access the bbs.linuxidc.org domain name to xp2. An error is returned when accessing other pages.
Sub vcl_recv {
If (req. http. host ~ "^ (Www .)? Linuxidc.com "){
Set req. http. host = "www.linuxidc.com ";
Set req. backend = xp1;
} Elsif (req. http. host ~ "^ Bbs.linuxidc.com "){
Set req. backend = xp2;
} Else {error 404 "linuxidc 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 .)? Linuxidc.com "){
Set req. http. host = "www.linuxidc.com ";
Set req. backend = lb;
Return (pass); # Do not cache for test convenience.
} Elsif (req. http. host ~ "^ Bbs.linuxidc.com "){
Set req. backend = xp2;
} Else {
Error 404 "linuxidc 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. $ Var_group1 = array (
'Host' => array ('2017. 25.254.2 '),
'Port' => '123 ',
);
// Varnish group definition
// Bind the host list
$ VAR_CLUSTER = array (
'Www .linuxidc.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 linuxidc {
# Set access control
"127.0.0.1 ";
"172.25.254.0"/24;
}
Sub vcl_recv {
If (req. request = "BAN "){
If (! Client. ip ~ Linuxidc ){
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 VarnishHttp://www.linuxidc.com/Linux/2014-05/101389.htm

Cache Server Varnish concept Article http://www.linuxidc.com/Linux/2014-05/101389.htm

Varnish Cache architecture note http://www.linuxidc.com/Linux/2013-10/91016.htm

Installation configuration http://www.linuxidc.com/Linux/2013-09/89916.htm for Varnish-5.8 Under CentOS 2.1.5

RedHat scripts use CentOS source updates to install Nginx, PHP 5.3, Varnish http://www.linuxidc.com/Linux/2012-07/65801.htm

Build Cache Server note http://www.linuxidc.com/Linux/2012-07/65234.htm using Varnish

The cache service Varnish installation configuration http://www.linuxidc.com/Linux/2012-07/65228.htm

Varnish compilation and installation preparation http://www.linuxidc.com/Linux/2012-07/65230.htm

Configuration Optimization http://www.linuxidc.com/Linux/2012-03/56435.htm for Varnish caching in Linux

Varnish: Click here
Varnish's: Click here

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2016-03/129284.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.