The construction of varnish proxy server

Keywords: vim RPM Firefox DNS

Varnish is a high-performance open source HTTP Accelerator, Open source reverse proxy and cache servers, Norway Verdens Gang, the largest online newspaper, replaced 12 squids with three varnishs, with better performance than before

Basic access to the website

firefox - > hosts/dns - > varnish - > server - > varnish - > httpd

The configuration of varnish

  • Note add domain name resolution

1. Installation and configuration of varnish

yum install -y varnish-libs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm

[root@server1 varnish]# sysctl -a |grep file   #View the maximum number of open files of virtual machine
fs.file-nr = 480    0    98861
fs.file-max = 98861

vim /etc/sysconfig/varnish    #modify

# Maximum number of open files (for ulimit -n)
NFILES=98861

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000

# Maximum number of threads (for ulimit -u)
NPROCS="unlimited"

 

vim /etc/security/limits.conf

 51 varnish         -       nofile          98861
 52 varnish         -       memlock         82000
 53 varnish         -       nproc           unlimited

 

2. Modify the port of varnish

vim /etc/sysconfig/varnish     
66 VARNISH_LISTEN_PORT=80

3. The configuration file of varnish

cd /etc/varnish/

vim default.vcl

The script configuration file / etc/sysconfig/varnish is used to specify configuration parameters. It can modify the cache save time, size, etc

4: Cache (through the cache mechanism, people in different regions can access the municipal varnish server, which can improve the access speed)

vim /etc/varnish/default.vcl

backend default {
     .host = "172.25.12.2";
     .port = "80";
  }

You can access 172.25.12.2 information by visiting 172.25.12.1 (the varnish server). The server is equivalent to caching the information in 2, which can be accessed directly in the server

5: Modify cache hit prompt

 7 backend default {
  8   .host = "172.25.12.2";
  9   .port = "80";
 10 }
 11
 12 sub vcl_deliver {
 13 if (obj.hits > 0) {
 14 set resp.http.X-Cache = "HIT from westos cache";
 15 }
 16 else {
 17 set resp.http.X-Cache = "MISS from westos cache";
 18 }
 19 return (deliver);
 20 }

Through curl 172.25.12.1 -I, you can see the feedback message HIT from westos cache (representing the cache message obtained) or MISS from westos cache (no cache message obtained)

6. Varnish polling

(polling can relieve the pressure on the primary server)

backend web1 {
  .host = "172.25.12.2";
  .port = "80";
}

backend web2 {
  .host = "172.25.12.3";
  .port = "80";
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
#varlish load balancing

# lb polling mechanism configuration
director westos round-robin {
{       .backend = web1; }
{       .backend = web2; }
}


sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = westos;    #Modify received as westos
return(pass);        #For testing convenience, do not cache
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}

Here you can use the virtual host function of http

You can visit different pages by visiting different addresses

1. Install httpd

2.vim /etc/httpd/conf/httpd.conf

990 NameVirtualHost *:80

1010 <VirtualHost *:80>
1011         ServerName bbs.westos.org
1012         DocumentRoot    /var/www/html
1013 </VirtualHost>
1014 <VirtualHost *:80>
1015         ServerName www.westos.org
1016         DocumentRoot    /www1
1017 </VirtualHost>

 

Posted by purpendicular on Fri, 31 Jan 2020 01:40:49 -0800