Varnish base configuration for dynamic and static separation of Web sites

Source: Internet
Author: User
Tags apm varnish

Because a Web site's program Access has local characteristics: temporal locality: After a data has been accessed, it may soon be accessed again; spatial locality: When a data is accessed, the surrounding data may be accessed; varnish can cache this part of the data.
The data that is cached is accessed more frequently and can be called a hot zone: The cache also has locality; timeliness: If the cache space is exhausted: LRU, least recently used algorithm; clean out outdated cache

The basic working principle of varnish:

Varnish caches data that can be used to cache data directly in response to the client's cache through a way similar to the HTPP reverse proxy, if the cache
Without the corresponding data, it will point the request to the backend machine and get the response data to respond to the client.
When Varnish has a cache, the response usually takes only a very short amount of time, usually a few steps faster than the direct access to the backend machine, so cache pages are cached to varnish as much as possible.


Varnish working mode diagram:

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9A/A3/wKiom1lY4Prj5ZoeAABT40sH6oY379.png "title=" capture. PNG "alt=" Wkiom1ly4prj5zoeaabt40sh6oy379.png "/>

Varnish diagram of the processing mechanism:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9A/A3/wKiom1lY4UPCRsMWAAYYShK1BV8578.jpg-wh_500x0-wm_ 3-wmp_4-s_3429070054.jpg "title=" 30212356_146129273143jh.jpg "alt=" wkiom1ly4upcrsmwaayyshk1bv8578.jpg-wh_50 "/ >

which data can be cached or not cacheable : 1. Try to cache the public data of the site as much as possible; 2. Use to exclude private data from the user.

Configure a Wordperss site to use the Varnish cache server and do static and dynamic separation

Base topology diagram:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9A/A3/wKioL1lY4WeBqB0LAAB1ycfFd5o851.png "title=" Varnish Foundation. PNG "alt=" Wkiol1ly4webqb0laab1ycffd5o851.png "/>

Varnish configuration (based on CENOTS7, the entire configuration process should avoid the effects of iptables and SELinux):
Ntpdata 172.16.0.1 Sync Time

To install the varnish program:

Yum Install varnish

To edit the varnish master profile:

Vim/etc/varnish/varnish.params Add last line: daemon_opts= "-P thread_pools=3-p thread_pool_min=5-p thread_pool_max=1000 -P thread_pool_timeout=300 "

To start the varnish program:

Systemctl Restart Varnish

To edit the VCL configuration file for varnish:

Vim/etc/varnish/default.vcl

Make the following basic configuration:

vcl 4.0;# default backend definition. set this to point to  your content server.backend default {       #定义后端动态主机 (APM)     .host =  "192.168.5.109";     .port =  "80" ;} backend nginxsrvs {     #定义后端静态主机 (NIGNX)   .host =  " 192.168.5.108 ";   .port = " 80 ";} Sub vcl_recv {    # happens before we check if we  have this in cache already.    #    #  Typically you clean up the request here, removing cookies you  don ' t need,    # rewriting the request, etc.     if  (req.method ==  "PURGE")  {        #指明何时可以用PURGE请求裁剪缓存数据          return (purge);}     if  (req.url ~  "(? i) ^/(login|admin)")  {   # Specifies that some private data does not look for cached data                                           return (pass);                                  }    if  (req.url ~  "(? i) \. ( html|htm|css|svg|js|jpg|jpeg|png|gif|pdf)  {  #指定的内容由静态服务器响应, others for dynamic server response           set req.backend_hint = nginxsrvs;    #指明响应的后端主机     } else {         set req.backend_hint =   default;     #指明响应的前端主机     }}sub vcl_purge {         return  (synth, "purged");    #指明可以执行的PURGE操作}sub vcl_deliver {                                                        if  ( obj.hits>0)  {        #可以用来做缓存是否被命中, Obj.hits used to count the number of cache hits                                           set resp.http.x-cache =  "hit via "  + server.ip;                } else {                                                                  set resp.http.x-cache =  "miss via "  + server.ip;           }

Load the default.vcl file into the varnish program:

Varnishadm-s secret into the CLI interface of the varnish configuration to perform the compiled load: vcl.load test1 default.vcl #编译 vcl.use test1 #加载 quit #退出
To configure the APM dynamic server:
Yum Install Httpdmkdir-p/apps/data (requires consideration of permissions)

The WordPress source file CP to the directory

Edit the httpd site configuration file

Vim/etc/httpd/conf.d/wordpress.conf

<virtualhost *:80> directoryindex index.php index.html ServerName www.abc.com documentroot/apps/data/wordp  Ress <directory "/apps/data/" > Options followsymlinks allowoverride None Require All granted </Directory></VirtualHost>

To start the HTTPD program:

Systemctl Restart httpd
Configure NFS Services for sharing web site files
Yum Install Nfs-utilsvim/etc/exports/apps/data * (rw,all_squash,anonuid=48)
Configure Nginx Static server:
Yum Install Nginx #这里要用到nginx官方提供的yum源

Configure the/etc/nginx/conf.d/default.conf file to make the following changes

Vim/etc/nginx/conf.d/default.conflocation/{root/apps/data/wordpress; #指明web程序的路径 index index.html index.htm;}

Start Nginx Service Program

Systemctl Restart Nginx

Change the Hosts file to do access testing.

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9A/A3/wKiom1lY4xDBVUKOAAYFqcw1XUI122.png-wh_500x0-wm_ 3-wmp_4-s_3694132590.png "title=" ABC. PNG "alt=" Wkiom1ly4xdbvukoaayfqcw1xui122.png-wh_50 "/>
The varnish basic configuration implementation is complete.


This article is from the "Old Town" blog, please be sure to keep this source http://cityx.blog.51cto.com/9857477/1943841

Varnish base configuration for static and dynamic separation of Web sites

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.