Using varnish instead of squid to do a detailed solution to the website cache accelerator

Source: Internet
Author: User
Tags chmod varnish

Source: http://blog.s135.com/post/313/


[Article Author: Zhang Yi This version: v1.2 last modified: 2008.01.02 reprint Please specify the Source: http://blog.s135.com]

I once wrote an article, "Preliminary trial squid alternative products ──varnish Cache website accelerator", but was only used to play, did not do in-depth research.

Today's article on varnish, is already a complete alternative to squid to do the Web site caching accelerator details of the solution. There is little information about varnish on the internet, the Chinese material is very small, I hope this article can attract more people to study and use varnish.

In my opinion, there are three reasons to use varnish instead of squid:
1, Varnish adopted the "Visual Page Cache" technology, in the use of memory, varnish than squid has advantages, it avoids squid frequently in memory, disk Exchange files, performance than squid high.
2, varnish stability is also good, I managed a picture server running varnish has been one months, no failure, and the same work of squid server has been poured several times.
3, through the varnish management port, you can use regular expression fast, bulk cleaning part of the cache, this is squid can not have.

  

The following installs the Varnish Web cache accelerator (Linux system):
1, create WWW users and groups, and varnish cache file storage directory (/var/vcache):
/usr/sbin/groupadd Www-g 48
/usr/sbin/useradd-u 48-g www www
Mkdir-p/var/vcache
chmod +w/var/vcache
Chown-r Www:www/var/vcache

2. Create varnish log directory (/var/logs/):
Mkdir-p/var/logs
chmod +w/var/logs
Chown-r Www:www/var/logs

3, compile and install varnish:
wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.2.tar.gz
Tar zxvf varnish-1.1.2.tar.gz
CD varnish-1.1.2
./configure--prefix=/usr/local/varnish
Make && make install

4. Create Varnish configuration file:
Vi/usr/local/varnish/vcl.conf
Enter the following content:
Reference backend Myblogserver {
Set backend.host = "192.168.0.5";
Set backend.port = "80";
}

ACL Purge {
"LocalHost";
"127.0.0.1";
"192.168.1.0"/24;
}

Sub Vcl_recv {
if (req.request = = "PURGE") {
if (!client.ip ~ purge) {
Error 405 "not allowed."
}
Lookup
}

if (req.http.host ~ "^blog.s135.com") {
Set req.backend = Myblogserver;
if (req.request!= "get" && req.request!= "Head") {
Pipe
}
else {
Lookup
}
}
else {
Error 404 "Zhang Yan Cache Server";
Lookup
}
}

Sub Vcl_hit {
if (req.request = = "PURGE") {
set obj.ttl = 0s;
Error "purged."
}
}

Sub Vcl_miss {
if (req.request = = "PURGE") {
Error 404 "not in cache.";
}
}

Sub Vcl_fetch {
if (req.request = = "Get" && req.url ~ "\. TXT|JS) $ ") {
Set obj.ttl = 3600s;
}
else {
Set obj.ttl = 30d;
}
}
Here, I explain this configuration file:
(1), the varnish through the reverse proxy request the backend IP is 192.168.0.5, the port is 80 Web server;
(2), varnish allows localhost, 127.0.0.1, 192.168.0.*** three source IP through the Purge method to clear the cache;
(3), varnish to the domain name for the blog.s135.com request processing, the request of the blog.s135.com domain name returns "Zhang Yan Cache Server";
(4) The varnish caches the get and head requests in the HTTP protocol and accesses the POST request directly to the back-end Web server. This configuration is because post requests are generally sent to the server, the server needs to receive, processing, so do not cache;
(5), varnish set the URL cache time to the end of. txt and. js for 1 hours, and set the other URL cache time to 30 days.

5, start varnish
Ulimit-shn 51200
/usr/local/varnish/sbin/varnishd-n/var/vcache-f/usr/local/varnish/vcl.conf-a 0.0.0.0:80-s file,/var/vcache/ Varnish_cache.data,1g-g www-u www-w 30000,51200,10-t 127.0.0.1:3500-p client_http11=on

6. The startup VARNISHNCSA is used to write the varnish access log to the log file:
/usr/local/varnish/bin/varnishncsa-n/var/vcache-w/var/logs/varnish.log &

7, configure the boot automatically start varnish
Vi/etc/rc.local
Add the following at the end:
Quote Ulimit-shn 51200
/usr/local/varnish/sbin/varnishd-n/var/vcache-f/usr/local/varnish/vcl.conf-a 0.0.0.0:80-s file,/var/vcache/ Varnish_cache.data,1g-g www-u www-w 30000,51200,10-t 127.0.0.1:3500-p client_http11=on
/usr/local/varnish/bin/varnishncsa-n/var/vcache-w/var/logs/youvideo.log &

8, optimize the Linux kernel parameters
Vi/etc/sysctl.conf
Add the following at the end:
Reference Net.ipv4.tcp_fin_timeout = 30
Net.ipv4.tcp_keepalive_time = 300
Net.ipv4.tcp_syncookies = 1
Net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_tw_recycle = 1
Net.ipv4.ip_local_port_range = 5000 65000

and see how to manage varnish:
1, view the number of varnish server connections and hit rate:
/usr/local/varnish/bin/varnishstat
  

2, through the Varnish management port management:
Use Help to see which varnish commands you can use:
/usr/local/varnish/bin/varnishadm-t 127.0.0.1:3500 Help
Reference Available Commands:
ping [timestamp]
Status
Start
Stop
Stats
Vcl.load
Vcl.inline
Vcl.use
Vcl.discard
Vcl.list
Vcl.show
Param.show [-l] []
Param.set
Help [command]
Url.purge
Dump.pool

3, through the varnish management port, the use of regular expressions to clear the bulk of the cache:
(1), example: clear a similar http://blog.s135.com/a/zhangyan.html URL address):
/usr/local/varnish/bin/varnishadm-t 127.0.0.1:3500 url.purge/a/
(2), example: Clear URL address similar to Http://blog.s135.com/tech:
/usr/local/varnish/bin/varnishadm-t 127.0.0.1:3500 Url.purge w*$
(3), example: Clear all caches:
/usr/local/varnish/bin/varnishadm-t 127.0.0.1:3500 Url.purge *$

4, a clear squid cache of PHP functions (clear varnish cache can also use this function, do not make any changes, very convenient):
View plain print? <?php   Function purge ($ip,  $url)    {       $ errstr =  ';        $errno  =  ';         $FP  = fsockopen  ($ip, 80,  $errno,  $errstr,  2);        if  (! $fp)        {             return false;       }        else       {             $out  =  "purge  $url  http/1.1\r\n";             $out  .=  "host:blog.s135.com\r\n";             $out  .=  "connection: close\r\n\r\n";           fputs  ($fp,  $out);            $out  =  fgets ($fp  , 4096);           fclose  ($fp );           return true;        }  }      Purge ("192.168.0.4",  "/index.php");  ?>   

Attached 1:varnish official website: http://www.varnish-cache.org/

Appendix 2: December 10, 2007, I wrote a 0-point run every day, cutting varnish logs by day, generating a compressed file, while deleting the script for last month's old log (/var/logs/cutlog.sh):
The contents of the/var/logs/cutlog.sh file are as follows:

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.