Install and configure Varnish-5.8 in CentOS 2.1.5

Source: Internet
Author: User
Tags varnish automake

Varnish is a powerful reverse proxy acceleration software. For details about how Varnish works, see its specific procedures and VCL syntax, you can also refer to its official website and the Varnish Chinese authoritative guide.

Varnish Chinese authoritative guide PDF download

Free in http://linux.bkjia.com/

The username and password are both www.bkjia.com

The installation and configuration of Varnish-2013 under CentOS September/July 6/July 10/July 10/July 5.8/CentOS 2.1.5

For the download method, see

Using Varnish to build Cache Server notes

Preparations for Varnish compilation and Installation

Install and configure the cache service Varnish

Use Varnish and Nginx to use WebSocket

Configuration Optimization of Varnish cache in Linux

1. Install dependency in CentOS5.8 System Environment

12 yum install gcc-c ++
Yum install automake autoconflibtool ncurses-devel libxslt groff pcre-devel pkgconfig libtool-y

2. Download the varnish-2.1.5 source code package and compile and install it.

Cd/usr/local/src
Wget http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
Tar zxvf varnish-2.1.5.tar.gz
Cd varnish-2.1.5.
./Autogen. sh

# The autogen. sh command is used to check whether the dependency of the software is satisfied. If an error is reported, it should be as follows:
Normally:

+ Aclocal
+ Libtoolize -- copy -- force
+ Autoheader
+ Automake -- add-missing -- copy -- foreign
+ Autoconf

Continue compilation and installation:

12./configure -- prefix =/usr/local/varnish -- enable-dependency-tracking -- enable-debugging-symbols -- enable-developer-warnings-enable-extra-warnings
Make & make install & cd ../

3. Create varnish users and groups, as well as the varnish cache file and log storage directory:

/Usr/sbin/groupadd varnish
/Usr/sbin/useradd-s/sbin/nologin-g varnish
Mkdir-p/data/varnish/{cache, log}
Chown-R varnish: varnish/data/varnish/{cache, log}

4. My test environment is two Web machines. The varnish machine with IP address 192.168.1.103 (domain name is http://www.bkjia.net) performs reverse proxy acceleration on machines with backend IP address 192.168.1.104 and 192.168.1.105, its Configuration File/usr/local/varnish/etc/varnish/better. vcl is as follows:

Backend rserver1
{
. Host = "192.168.1.104 ";
. Port = "80 ";
. Probe = {
. Timeout = 5S; # How long wait timeout
. Interval = 2 s; # Check interval
. Window = 10; # varnish will maintain 10 sliding windows results
. Threshold = 8; # If the windows check is successful, the backend Web machine is declared.
Is healthy
}
}
Backend rserver2
{
. Host = "192.168.1.105 ";
. Port = "80 ";
. Probe = {
. Timeout = 5S;
. Interval = 2 s;
. Window = 10;
. Threshold = 8;
}
}
# Specify a realserver group. The higher the weight, the more access is allocated.
Server performance. However, the round-robin mechanism cannot specify weight.
Director realserver random {
{
. Backend = rserver1;
. Weight = 5;
}
{
. Backend = rserver2;
. Weight = 6;
}
}
# Define a machine that can clean up the cache. Here, only the local machine can be cleaned up using purge.
Acl purge {
"Localhost ";
"127.0.0.1 ";
}
Sub vcl_recv
{
If (req. http. host ~ "^ (. *) .Bkjia.net ")
{
Set req. backend = realserver;
}
Else
{
Error 200 "Nocahce for this domain ";
}
If (req. request = "PURGE ")
{
If (! Client. ip ~ Purge)
{
Error 405 "Not allowed .";
}
Else
{
Return (pipe );
}
}
# Obtain the real IP address of the client
If (req. http. x-forwarded-)
{
Set req. http. X-Forwarded-For =
Req. http. X-Forwarded-For "," client. ip;
}
Else
{
Set req. http. X-Forwarded-For = client. ip;
}
# Cache the GET and HEAD requests in the HTTP protocol and allow the POST requests to directly access the backend Web Server
Server. The reason for this configuration is that the POST request generally sends data to the server, and the server needs
Receiving and Processing, so no cache;
If (req. request! = "GET" & req. request! = "HEAD ")
{
Return (pipe );
}
If (req. http. Exact CT)
{
Return (pipe );
}
If (req. http. Authenticate | req. http. Cookie)
{
Return (pass );
}
If (req. http. Cache-Control ~ "No-cache ")
{
Return (pass );
}
# Do not cache JSP or PHP files
If (req. url ~ "\. Jsp" | req. url ~ "\. Php ")
{
Return (pass );
}
Else
{
Return (lookup );
}
} Sub vcl_pipe
{
Return (pipe );
} Sub vcl_pass
{
Return (pass );
} Sub vcl_hash
{
Set req. hash + = req. url;
If (req. http. host)
{
Set req. hash + = req. http. host;
}
Else
{
Set req. hash + = server. ip;
}
Return (hash );
} Sub vcl_hit
{
If (req. request = "PURGE ")
{
Set obj. ttl = 0 s;
Error 200 "Purged .";
}
If (! Obj. cacheable)
{
Return (pass );
}
Return (deliver );
} Sub vcl_miss
{
If (req. request = "PURGE ")
{
Error 404 "Not incache .";
}
If (req. http. user-agent ~ "Spider ")
{
Error 503 "Notpresently in cache ";
}
Return (fetch );
}
Sub vcl_fetch
{
If (req. request = "GET" & req. url ~ "\. (Txt | js) $ ")
{
Set beresp. ttl = 3600 s;
}
Else
{
Set beresp. ttl = 30d;
}
If (! Beresp. cacheable)
{
Return (pass );
}
If (beresp. http. Set-Cookie)
{
Return (pass );
}
Return (deliver );
}
Sub vcl_deliver {
If (obj. hits> 0 ){
Set resp. http. X-Cache = "hit from www.bkjia.net ";
} Else {
Set resp. http. X-Cache = "miss from www.bkjia.net ";
}
Return (deliver );
}

  • 1
  • 2
  • Next Page

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.