Detailed deployment and configuration of Web cache server varnish-4.1.6

Source: Internet
Author: User

Detailed deployment and configuration of Web cache server varnish-4.1.6


1. Installing varnish4.1.6
Installation dependencies

Yum Install -y autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig Python-docutils Python-sphinx

Installing the varnish Yum Warehouse

# RPM--nosignature-i https://yuminstall varnish-y

2. Setting the varnish parameter

#grep "^[a-z]"/etc/varnish/Varnish.params RELOAD_VCL=1# whether to re-read VCL and recompile varnish_vcl_conf when restarting the service=/etc/varnish/DEFAULT.VCL # VCL file read by default Varnish_listen_port= the# Set the listening port (default listening 6081 port) varnish_admin_listen_address=127.0.0.1# Management Interface Listener address Varnish_admin_listen_port=6082# port Varnish_secret_file for management interface listening=/etc/varnish/Secret # key file used Varnish_storage="malloc,256m"# The size of the storage file Varnish_user=Varnish # varnish Default User Varnish_group=varnish # varnish Default Group

Defining VCL Backend

Cat /ETC/VARNISH/DEFAULT.VCL | grep " # "  4.0, backend web01 {    "192.168.3.12";     "  the " ;}

Start varnish
# Systemctl Start varnish
# Systemctl Enable varnish

3. Define the set of VCL backend director
VCL can synthesize a group of multiple backends, these groups are called Director, which can enhance the performance and elasticity, when one backend in the group hangs off, can choose another healthy backend. VCL has a variety of directors, different directors use different algorithms to choose backend, mainly have the following several:
A. The random Director
The Random director selects the Backend,.retries parameter according to the weight set (weight) to indicate the maximum number of times an attempt is made to find a backend. The weight parameter represents the weight value
B. The Round-robin Director
Round-robin director selects backend in a circular fashion.
C. The client Director
Client director chooses backend according to Client.identity, and you can set the value of Client.identity to the session cookie to identify the backend.

# vim/etc/varnish/DEFAULT.VCLVCL4.0; # indicates varnish version backend WEB01 {# defines back-end server 1. Host="192.168.3.12"; . Port=" the";} Backend WEB02 {# define back-end server 2. Host="192.168.3.13"; . Port=" the";}                Import directors; # define directorssub vcl_init {# definition Vcl_init subroutine new Cluster1=Directors.round_robin ();        Cluster1.add_backend (WEB01); Cluster1.add_backend (WEB02);} Sub Vcl_recv {# definition Vcl_recv subroutine set Req.backend_hint=cluster1.backend (); #指定后端directors}

Details: Https://www.varnish-cache.org/docs/4.1/reference/vmod_directors.generated.html#object-hash

4. Set whether the response is hit

Sub Vcl_deliver {# Definition subroutineif(Obj.hits >0) {Set resp.http.x-cache ="Hit via"+" "+Server.ip; } Else{Set resp.http.x-cache ="MISS via"+" "+Server.ip; } # To determine if the hit is in the HTTP response header setting x-cache is hit, otherwise the HTTP response header is set to Xthe cache is miss. }

Then go to the page to see if it is already in effect, you can see the first time the visit is miss the second time is hit


5. Specify that some files cannot be cached

Sub Vcl_recv {        if"^/test.html$") {                return (pass);        }            # #定义请求的文件中如果匹配test. HTML pass, no cache}

By testing you can see that continuous access http://192.168.3.198/test.html are miss

6. Conduct health Checks
Varnish can detect the health status of the backend host and automatically remove it from the list of available back-end hosts when the backend host fails, and automatically set it to available once it becomes available again. In order to avoid miscalculation, varnish when detecting the health state of a backend host (such as when a backend host suddenly becomes unavailable during a probe), it is often necessary to perform several successive probes in a new state before marking it as a converted state.
The health state detection method currently probed by each backend server is set by the. Probe, and the results can be obtained from the req.backend.healthy variable or through the Backend_ in Varnishlog Health View or Varnishadm debug.health view.

backend web01 {. Host="192.168.10.132"; . Port=" the"; . Probe={. URL="/"; # Specifies which URL requires a varnish request. Timeout=2s; # Specify timeout wait time. Interval=5s; # Specify the check interval. Window=5; # try up to 5 times. Threshold=3; # at least 3 successes declared backend health}}backend WEB02 {. Host="192.168.3.13"; . Port=" the"; . Probe={. URL="/"; . Timeout=2s; . Interval=5s; . Window=5; . Threshold=3; }}

7. Set the cache duration

Sub Vcl_backend_response {    if'\. ( Jpg|jpeg|gif|png) ($")        {= 2h;    }                 # If the URL is cached at the end of the picture    format for 2 hours if "\. ( html|css|js|jsp) ($")        {= 30m;    }                  # If the URL is in HTML|css|js| JSP end of cache 30 minutes}

Detailed deployment and configuration of Web cache server varnish-4.1.6

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.