Varnish configuration notes

Source: Internet
Author: User
Tags varnish

Varnish is an open-source reverse proxy software and HTTP accelerator. Compared with the traditional Squid, Varnish has many advantages such as higher performance, faster speed, and more convenient management, many large operating websites are trying to replace Squid with Varnish, which has prompted Varnish to develop rapidly.

1. Prepare and download the source code package

 

yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfigwget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
2. Installation
tar zxf varnish-3.0.3.tar.gzcd varnish-3.0.3./autogen.sh./configure --prefix=/usr/local/varnishmake && make install


3. Add the Varnishd process user www, User Group www, and create the/var/vcache directory so that the www user has the permission to read and write.

 
 
groupadd wwwuseradd www -g wwwmkdir /home/vcachechown -R www:www /home/vcachechmod -R 750 /home/vcache

 

4. edit/etc/sysctl. conf to optimize several kernel parameters.

 
net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 300net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.ip_local_port_range = 5000    65000
 

Run sysctl-p and set kernel parameters according to the configuration file.

5. Start Varnishd.

 
/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500


Parameter description:

-What is the purpose of u?
-G
-F varnish configuration file
-A: bind the IP address and port
-S varnish cache file location and size
-W minimum, maximum thread and timeout
-T varnish Management port, mainly used to clear the cache
-P client_http11 = on supports http1.1 Protocol
-P (large P)/usr/local/varnish/var/varnish. pid specifies the location of the Process Code file for management

6. Start varnishncsa to write Varnish access logs to the log file:

 /usr/ local /varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &

7. Varnish cache cleanup

 /usr/ local /varnish/bin/varnishadm -T 192.168.1.180:3500 purge  "req.http.host ~ www.5013.org$ && req.url ~ /static/image/tp.php"

Note:

192.168.1.180: 3000 indicates the address of the cache server to be cleared.
Www.5013.org is the domain name to be cleared
/Static/image/tp. php is the list of cleared URLs

Clear all caches
/Usr/local/varnish/bin/varnishadm-T 192.168.1.180: 3500 url. purge * $

Clear all caches in the image directory
/Usr/local/varnish/bin/varnishadm-T 192.168.1.180: 3500 url. purge/image/

8. A startup item will be added.
Vi/etc/rc. local


ulimit -SHn 51200/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500/usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &
 

9. Kill the varnishd Process
Pkill varnishd

10. View varnishd hit rate

 /usr/local/varnish/bin/varnishstat



 

11. Update system time


yum install -y ntpntpdate time.nist.govecho "00 01 * * * ntpdate time.nist.gov" >> /etc/crontab
 

Attachment multi-host multi-domain varnish. conf configuration


Backend blog {. host = "198.56.193.190 ";. port = "80";} backend www {. host = "192.168.1.170 ";. port = "80";} sub vcl_recv {if (req. http. host ~ "^ (Www .)? 5013. org $ ") {set req. backend = blog;} elsif (req. http. host ~ "^ (Www .)? (Test1.com | test2.com) $ ") {set req. backend = www;} else {error 404 "Unknown virtual host" ;}} sub vcl_recv {if (req. restarts = 0) {if (req. http. x-forwarded-for) {set req. http. x-Forwarded-For = req. http. x-Forwarded-For + "," + client. ip;} else {set req. http. x-Forwarded-For = client. ip ;}# send all access requests except the following types of requests directly to the backend server if (req. request! = "GET" & req. request! = "HEAD" & req. request! = "PUT" & req. request! = "POST" & req. request! = "TRACE" & req. request! = "OPTIONS" & req. request! = "DELETE") {/* Non-RFC2616 or CONNECT which is weird. */return (pipe);} # Only GET and HEAD methods use Lookup and cache. If (req. request! = "GET" & req. request! = "HEAD") {/* We only deal with GET and HEAD by default */return (pass);} # if (req. http. authorization | req. http. cookie) {#/* Not cacheable by default */# return (pass) ;#}# if the requested php page is directly forwarded to the backend server if (req. url ~ "\. (Php | cgi) ($ | \?) ") {Return (pass);} return (lookup);} sub vcl_pipe {return (pipe);} sub vcl_pass {return (pass);} sub vcl_hash {hash_data (req. url); if (req. http. host) {hash_data (req. http. host);} else {hash_data (server. ip);} return (hash);} sub vcl_hit {return (deliver);} sub vcl_miss {return (fetch);} sub vcl_fetch {if (beresp. ttl <= 0 s | beresp. http. set-Cookie | beresp. http. vary = "*"){/** Mark as "Hit-For-Pass" for the next 2 minutes */set beresp. ttl = 120 s; return (hit_for_pass);} if (req. url ~ "\. (Png | gif | jpg) $ ") {unset beresp. http. set-cookie; set beresp. ttl = 1 h ;}# set the image cache TTL to one hour return (deliver);} sub vcl_deliver {return (deliver);} sub vcl_error {set obj. http. content-Type = "text/html; charsets = UTF-8"; set obj. http. retry-After = "5"; synthetic {"<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd "> <Html> 


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.