Using Varnish as the cache server for Discuz Forum

Source: Internet
Author: User
Tags varnish

Using Varnish as the cache server for Discuz Forum

Tutorial Background: The company has a BBS server built on the LNMP architecture. There was an idle Virtual Machine on hand, so I wanted to add a cache server to the BBS front-end. As a result, varnish was selected and many tutorials were searched to complete the configuration. I am not very familiar with the functions of many of these configurations. Here we will give a general configuration, and I will discuss the configuration principles and specific functions later.

Experimental System: CentOS 6.4 _ x86_64

Prerequisites: Firewall and selinux are both disabled.

Tutorial Description: There are two hosts in this experiment, and IP addresses are allocated as topology.

Lab software: varnish-3.0.7-1 varnish-libs-3.0.7-1

: Http://pan.baidu.com/s/1pJOOc1X

Or:

------------------------------------------ Split line ------------------------------------------

Free in http://linux.bkjia.com/

The username and password are both www.bkjia.com

The specific download directory is in/July 6/July 18/July 18/July 18/July 18/use Varnish as the cache server for Discuz Forum/

For the download method, see

------------------------------------------ Split line ------------------------------------------

Tutorial topology:

Ii. Access test:

1. Open http: // 192.168.70.186 in the browser and randomly access some content:

2. view the varnish status:

varnishstat

3. View charts:

varnishhist

4. The front-end client uses the F12 function of the browser to check whether the cache is hit. Here, chrome is used for Demonstration:

At this point, the cache server has been set up, which may cause some problems. Today, we will give a rough demonstration.

1. install and configure varnish

1. directly use yum for installation on 70.186:

Yum-y install varnish-3.0.7-1.el6.x86_64.rpm varnish-libs-3.0.7-1.el6.x86_64.rpm

2. Edit the/etc/sysconfig/varnish file:

 

Vim/etc/sysconfig/varnish
------------------------------------------------>
# Alternative 3, Advanced configuration

VARNISH_VCL_CONF =/etc/varnish/default. vcl // vcl file used
VARNISH_LISTEN_PORT = 80 // the port that varnish listens.
VARNISH_ADMIN_LISTEN_ADDRESS = 127.0.0.1 // ip address used by the admin Management Interface
VARNISH_ADMIN_LISTEN_PORT = 6082 // port used by the admin Management Interface
VARNISH_SECRET_FILE =/etc/varnish/secret // Password File
VARNISH_MIN_THREADS = 50 // minimum number of processes
VARNISH_MAX_THREADS = 1000 // maximum number of processes
VARNISH_THREAD_TIMEOUT = 120 // The idle process is recycled after this time
VARNISH_STORAGE_SIZE = 1 GB // storage file size
VARNISH_STORAGE = "malloc, $ {VARNISH_STORAGE_SIZE}" // specify the storage type and file
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 }"

 

3. Edit the vcl file:

 

Vim/etc/varnish/default. vcl
--------------------------------------------------------->
Backend default {
. Host = "192.168.90.186 ";
. Port = "80 ";
}

Acl local {
"Localhost ";
"127.0.0.1 ";
}

Sub vcl_recv {
If (req. restarts = 0 ){
If (req. http. x-forwarded-){
Set req. http. X-Forwarded-For =
Req. http. X-Forwarded-For + "," + client. ip;
} Else {
Set req. http. X-Forwarded-For = client. ip;
}
}

If (req. request = "PURGE "){
If (! Client. ip ~ Local ){
Error 405 "Not Allowed .";
}
Return (lookup );
}

If (req. request = "GET" & req. url ~ "\. (Jpg | png | gif | swf | jpeg | flv | bmp | gz | tgz | bz2 | tbz | js | css | ico) $ "){
Unset req. http. cookie;
}

If (req. request = "GET" & req. url ~ "\. (Js | css). * $ "){
Unset req. http. cookie;
}

If (req. url ~ "^/Images "){
Unset req. http. cookie;
}

If (req. request! = "GET "&&
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 "){
Return (pass );
}

If (req. http. Authorization | req. http. Cookie ){
Return (pass );
}

If (req. request = "GET" & req. url ~ "\. (Php) ($ | \?) "){
Return (pass );
}

Return (lookup );
}

Sub vcl_pipe {
Return (pipe );
}

Sub vcl_pass {
If (req. request = "PURGE "){
Error 502 "PURGE on a passed object ";
}
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 {
If (! Obj. ttl> 0 s ){
Return (pass );
}
If (req. request = "PURGE "){
Purge;
Error 200 "Purged .";
}
Return (deliver );
}

Sub vcl_miss {
If (req. request = "PURGE "){
Purge;
Error 404 "Not in cache ";
}
Return (fetch );
}

Sub vcl_fetch {
If (beresp. ttl <= 0 s |
Beresp. http. Set-Cookie |
Beresp. http. Vary = "*"){
Set beresp. ttl = 120 s;
Return (hit_for_pass );
}

If (beresp. http. Pragma ~ "No-cache" |
Beresp. http. Cache-Control ~ "No-cache" |
Beresp. http. Cache-Control ~ "Private "){
Return (hit_for_pass );
}

If (req. request = "GET" & req. url ~ "\. (Bmp | jpeg | jpg | png | gif | svg | png | ico | txt | css | js | html | htm) $ "){
Unset beresp. http. set-cookie;
Set beresp. ttl = 1 h;
} Else {
Set beresp. ttl = 1800 s;
}

Return (deliver );
}

Sub vcl_deliver {
If (obj. hits> 0 ){
Set resp. http. X-Cache = "Hit Via" + "" + server. hostname;
} Else {
Set resp. http. X-Cache = "Miss Via" + "" + server. hostname;
}
Return (deliver );
}

Sub vcl_init {
Return (OK );
}

Sub vcl_fini {
Return (OK );
}

 

4. Start varnish, view the port, and open the browser to test:

Service varnish start
Ss-tnl

Ii. Access test:

1. Open http: // 192.168.70.186 in the browser and randomly access some content:

2. view the varnish status:

Varnishstat

3. View charts:

Varnishhist

4. The front-end client uses the F12 function of the browser to check whether the cache is hit. Here, chrome is used for Demonstration:

At this point, the cache server has been set up, which may cause some problems. Let's give a brief demonstration today.

Concepts of Cache Server Varnish

Concepts of Cache Server Varnish

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 details: click here
Varnish: click here

This article permanently updates the link address:

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.