Based on CentOS 6.5 use varnish to implement the site static and dynamic separation _linux

Source: Internet
Author: User
Tags web services varnish

Introduction of Varnish

Varnish is a high-performance, open source reverse proxy server and caching server, its developer Poul-henning Kamp is one of the FreeBSD core developers.
Varnish mainly runs two processes: the management process and the child process (also called the cache process).
The management process mainly implements the application of new configurations, compiling VCL, monitoring varnish, initializing varnish, and providing a command line interface. The management process detects the child process every few seconds to determine whether it is working correctly, and management restarts the child process if the child process is not responded to within the specified length of time.

Second, varnish work flow


1), Varnish received the request from the client, processing by the VCL_RECV state engine, unrecognized request will pass the parameter pipe to the Vcl_pipe state engine, need to find the cache request through lookup parameters will be given to the Vcl_hash state engine, No cached data passed via parameter pass will be given to the Vcl_pass state engine;
2), the Vcl_hash state engine will look for data from the cache after receiving the request, there are two kinds of query results, one is hit cache hit, the other is Miss Cache misses;
3), the Vcl_hit state engine will hit the cached data through the parameter deliver to the Vcl_deliver state engine, the Vcl_deliver state engine will data processing, the final return to the client;
4), the Vcl_miss state engine will fetch the missed result parameters to the Vcl_fetch state engine, and the Vcl_fetch state engine will look up the data from the database;
5), the Vcl_fetch state engine will query the results from the database, return to the Vcl_deliver state engine;
6), the Vcl_deliver state engine returns the result to the master process, and finally returns to the client;

third, the use of varnish to achieve site static and dynamic separation

Experimental environment, three virtual machines
linux:centos6.5
Varnish:varnish-3.0.4-1.el6.x86_64
nginx:nginx-1.4.7
Varnish Host: Two network cards, extranet IP 172.16.36.10, intranet IP 192.168.0.10
Web server 1:ip 192.168.0.20, for use as a static file server
Web server 2:ip 192.168.0.30, used as a dynamic program server
Prerequisite Description:
Varnish's profile is the VCL suffix, located in the/etc/varnish/directory, where caching is typically used to increase response speed, in general, you can cache HTML static pages, pictures, JS scripts, CSS stylesheets, because pages written in dynamic scripting languages need to be processed using a scripting engine. Therefore, there is no need for caching; Nginx itself has caching and reverse proxy function, can completely implement the static and dynamic separation of Web services, but in contrast to the caching function, varnish cache is more professional than nginx, so to do caching server, you can try varnish, This operation will be used for experimental purposes, the use of varnish Web services to achieve static and dynamic separation;
1. Installation varnish
# RPM-IVH varnish-3.0.4-1.el6.x86_64.rpm varnish-docs-3.0.4-1.el6.x86_64.rpm varnish-libs-3.0.4-1.el6.x86_64.rpm
2. Configure Varnish
1, edit the varnish script configuration file/etc/sysconfig/varnish, the varnish listening port modified to 80;

2, new file/etc/varnish/web.vcl, edit varnish cache rules;

#定义后端服务器 backend Web1 {host= "192.168.0.20";
. port= "80";
  } backend web2 {. host= "192.168.0.30";
. port= "80";
  #只允许本机使用purgers请求方法清除缓存 ACL purgers {"127.0.0.1";
"172.16.0.0"/16;
   The Sub Vcl_recv {if (req.request== "PURGE") {if (!client.ip~purgers) {error 405 "Mothod not allow"; } #静态资源交给web1服务器 if (req.url ~) \. (
  Html|htm|shtml|css|js|jpg|png|gif|jpeg) ") {set req.backend=web1;
   #php页面交给web2服务器, and skips cache if (req.url ~ "\.php") {set req.backend=web2;
  return (pass);
return (lookup);
  } #将命中的缓存清除 Sub Vcl_hit {if (req.request = = "PURGE") {PURGE;
 Error "purged OK";
  }} #如果请求清除的资源不在缓存列表中, returns 404 Status Sub Vcl_miss {if (req.request = = "PURGE") {PURGE;
 Error 404 "not in cache";
 } #如果请求清除的资源是一个不可缓存的资源, returns 502 status sub Vcl_pass {if (req.request = = "PURGE") {Error 502 "purged on a passed object."; } #缓存对象存活时间 Sub Vcl_fetch {if (req.url ~) \. (
  Html|htm|shtml|css|js|jpg|png|gif|jpeg) ") {set beresp.ttl=7200s; }} #将结果返回给客户端并在响应头部添加两字段, explicitThe Web server sub Vcl_deliver {if (obj.hits > 0) {set resp.http.x-cache= "HIT from" + "+ server.ip" of the back-end response is shown or not;
  }else{set resp.http.x-cache= "MISS";
Set resp.http.backend-ip=req.backend; }

3), load configuration to varnish;

3.1), connect varnish

3.2, load configuration,

3.3, use configuration,

4, configure two Web servers, Install Nginx and PHP separately;
192.168.0.20 server, new two page, index.html and index.php, two page request output results are as follows:

192.168.0.30 Server, new two page, Index.html and index.php, two page request output results are as follows:

5), test results, open address: 172.16.36.10;

When we request an HTML page, no matter how we flush the request, the cache always hits, and display hit, and the back-end server has been, Web1 (192.168.0.20);



When we request a PHP page, we don't let him cache, then the cache will never hit, show miss, and the backend server is always, WEB2 ( 192.168.0.30);



6), clear cache,


Entire configuration complete, varnish implementation of static and dynamic separation;

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.