Varnish + Nginx implement single-and double-Web Server Cache

Source: Internet
Author: User
Tags varnish

Varnish + Nginx implement single-and double-Web Server Cache

Purpose:

Varnish caches the backend single-and double-static web Servers

Varnish package download path: http://repo.varnish-cache.org/redhat/varnish-3.0/el6 can download to varnish rpm package

Download:

Varnish-3.0.5-1.el6.x86_64

Varnish-docs-3.0.5-1.el6.x86_64

Varnish-libs-3.0.5-1.el6.x86_64

Varnish official site address: https://www.varnish-cache.org/

Lab environment:

Web1: 172.16.18.3 Nginx

Web2: 172.16.17.12 Nginx

Varnish: 172.16.18.1 Varnish

Lab content:

1. Install the varnish package and configure the web Server

[Root @ node1 ~] # Rpm-ql varnish

/Etc/rc. d/init. d/varnish # varnish Startup Program

/Etc/rc. d/init. d/varnishlog # log

/Etc/rc. d/init. d/varnishncsa # log

/Etc/sysconfig/varnish # configuration file, varnish defines its own attributes

/Etc/varnish # configuration file directory

/Etc/varnish/default. vcl # default configuration file, which defines

/Usr/bin/varnish_reload_vcl # Load vcl,

/Usr/bin/varnishadm # client program

/Usr/bin/varnishstat # Status Monitoring


2. Edit the configuration file

[Root @ node1 ~] # Vim/etc/sysconfig/varnish

NFILES = 131072

MEMLOCK = 82000

NPROCS = "unlimited"

RELOAD_VCL = 1 # Whether to reload the VCL File

# Alternative 3, Advanced configuration

VARNISH_VCL_CONF =/etc/varnish/default. vcl # vcl file path

VARNISH_LISTEN_PORT = 80 # varnish is working on that port. The default value is 6081.

VARNISH_ADMIN_LISTEN_ADDRESS = 127.0.0.1 # Management Interface

VARNISH_ADMIN_LISTEN_PORT = 6082 # management interface listening port

VARNISH_SECRET_FILE =/etc/varnish/secret # key file

VARNISH_MIN_THREADS = 50 # minimum number of Idle threads

VARNISH_MAX_THREADS = 1000 # maximum thread startup

VARNISH_THREAD_TIMEOUT = 120 # work timeout duration

# VARNISH_STORAGE_FILE =/var/lib/varnish/varnish_storage.bin # store files

VARNISH_STORAGE_SIZE = 64 M # storage file size

# VARNISH_STORAGE = "file, $ {VARNISH_STORAGE_FILE}, $ {VARNISH_STORAGE_SIZE}" # storage mode file

VARNISH_STORAGE = "malloc, $ {VARNISH_STORAGE_SIZE}" # Memory-based mode

VARNISH_TTL = 120

DAEMON_OPTS = "-a $ {VARNISH_LISTEN_ADDRESS }:: {VARNISH_LISTEN_PORT }\

-F $ {VARNISH_VCL_CONF }\

-T $ {VARNISH_ADMIN_LISTEN_ADDRESS }:$ {VARNISH_ADMIN_LISTEN_PORT }\

-T $ {VARNISH_TTL }\

-W $ {VARNISH_MIN_THREADS}, $ {VARNISH_MAX_THREADS}, $ {VARNISH_THREAD_TIMEOUT }\

-U varnish-g varnish \

-S $ {VARNISH_SECRET_FILE }\

-S $ {VARNISH_STORAGE }"

[Root @ node1 ~] # Vim/etc/varnish/default. vcl

Backend default {

. Host = "172.16.18.3 ";

. Port = "80 ";

}


Now varnish can be started. It is the most important way to compile a vcl file.

------------------------------------------------------------

Structural notes for Varnish Cache

Install and configure Varnish-5.8 in CentOS 2.1.5

The RedHat script uses the CentOS source to update and install Nginx, PHP 5.3, and Varnish.

Using Varnish to build Cache Server notes

Install and configure the cache service Varnish

Preparations for Varnish compilation and Installation

Configuration Optimization of Varnish cache in Linux

Varnish Basic Concepts

Bytes -----------------------------------------------------------------------------------

Then we should be familiar with the table, the variables corresponding to each status engine.


A Brief Introduction to vcl syntax

The Design of VCL is based on the C and Perl languages. Therefore, it is very easy to understand for those who have experience in C or Perl programming. The basic syntax is described as follows:

(1) //, # Or/* comment */for comment

(2) sub $ name definition function

(3) loops and built-in variables are not supported

(4) The termination statement is used and no return value is returned.

(5) domain-specific

(6) operators: = (Value assignment), = (equivalent comparison ),~ (Pattern matching ),! (Reverse), & (logical and), | (logical or)

The order of the Status engine in the vcl file is determined by the order of the default configuration file. The Order also conforms to the basic order of request processing. Of course, some changes will be made to match the experiment. Let's look at a figure to clearly understand the request process:


1. First, write the vcl_recv segment,

Vcl_recv is the first subroutine used to access varnish After decoding request packets. It allows you to perform access control, query cache, and determine unidentifiable data.

Copy the default. vcl file and rename it test1.vcl.

Acl purgers {# define an acl

"127.0.0.1 ";

"172.16.0.0"/16;

}

Sub vcl_recv {

If (req. url ~ "^/Test.html $") {configure the URL of the first part of the request to test.html,

Return (pass); # Skip the cache

}

If

If (req. request! = "GET" & # send requests to pipe if the request method is not known.

Req. request! = "HEAD "&&

Req. request! = "PUT "&&

Req. request! = "POST "&&

Req. request! = "TRACE "&&

Req. request! = "OPTIONS "&&

Req. request! = "DELETE "){

Return (pipe );

}

If (req. request! = "GET" & req. request! = "HEAD") {# Skip the cache instead of getting all resources to reduce useless cache queries

Return (pass );

}

If (req. request = "PURGE "){

If (! Client. ip ~ Purgers) {# If the requested IP address is not defined in the ACL, the error 405 page is displayed.

Error 405 "Method not allowed ";

}

Return (lookup );

}

Return (lookup );

}

So how can I load test1 to make it take effect?

First, modify the configuration file.

Second, use the varnishadm client tool.

Varnishadm

[Root @ node1 ~] # Varnishadm-S/etc/varnish/secret-T 127.0.0.1: 6082 # Go To The management tool
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux, 2.6.32-431. el6.x86 _ 64, x86_64,-sfile,-smalloc,-hcritbit
Varnish-3.0.5 revision 1a89b1f
Type 'help' for command list.
Type 'quit' to close CLI session.
Varnish> vcl. load test1 test1.vcl # load the vcl File

200

VCL compiled.

Varnish> vcl. list

200

Active 2 boot

Available 0 test1

Varnish> vcl. use test1

200

Varnish> vcl. list

200

Available 2 boot

Active 0 test1

In this way, the setting is successful.

To display the results, we can configure the deliver segment and match the cache information when returning the result to the client so that we can view the experiment results.

Sub vcl_deliver {

If (obj. > 0) {# determine that the number of condition cache matches is greater than 0

Set resp. http. X-Cache = " via" + "" + server. hostname; # Add 

} Else {

Set resp. http. X-Cache = "MISS via" + "" + server. hostname; # Add MISS via if no match exists.

}

}

Access cache 172.16.18.1. The first access after miss via is via within the cache Validity Period


When test.html is matched by the pass defined by vcl_recv, the cache is skipped. Therefore, the X-cache status is always MISS.


2. Edit vcl_hash to customize the data source generated by hash.

Sub vcl_hash {

Hash_data (req. url); # Matching Based on req. url

If (req. http. host ){

Hash_data (req. http. host); # cache the request header host.

} Else {

Hash_data (server. ip );

}

Return (hash );

}

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page


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.