Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Varnish to accelerate web sites

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use Varnish to accelerate the web website, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Use Varnish to speed up the website

Overview of varnish

Varnish is a high-performance open source HTTP accelerator, Norway's largest online newspaper Verdens Gang (http://www.vg.no) uses 3 Varnish instead of 12 squid, and the performance is better than before. Poul-Henning Kamp, author of Varnish and one of the kernel developers of FreeBSD, believes that computers are much more complex today than they were in 1975. In 1975, there were only two storage media: memory and hard disk. But now, in addition to the main memory, the memory of the computer system also includes L1, L2 and even L3 cache in cpu. There is also a cache device on the hard disk, so the architecture in which squid cache handles object replacement itself cannot know about these situations and optimize them, but the operating system can know about them, so this part of the work should be left to the operating system, which is the Varnish cache design architecture.

Monitoring port number: 6081

Detailed explanation of basic concepts of varnish

1) the characteristics of Varnish

1) can be cached based on memory, or cached on disk. If memory size is expected to exceed dozens of gigabytes, such as picture server, pure memory may not have good performance. At this time, you can use disk for caching, or use SSD X 2 to do RAID1 to avoid disk damage. Ssd hard disk is much better than mechanical hard disk in achieving random access. If it must be cached on disk, it is recommended to use ssd disk.

2) Virtual memory can be used, and IO performance will be very good.

3) support setting the accurate cache time of 0-60 seconds

4) support VCL, which is configured through the vcl programming language. Its configuration needs to be converted to C code first, so the configuration written by vcl needs to be converted to C language code first, so it depends on GCC to temporarily compile the vcl configuration before it can be run.

5) unique log storage and management mechanism. Since the log is stored in memory and can be accessed by multiple applications, it is generally necessary to check the hit rate, how many get post methods are available for the current request, and so on. For example, command tools such as varnishshtopvarnishlog are used to view log information.

6) support the use of the varnish state engine, through the ingenious design of the state engine, different engines deal with user requests and cache proxy mechanisms, use configuration files to provide state rules for the state engine, complete cache processing, complete proxy processing, and so on.

2) varnish cache data mechanism:

3) Varnish Agent VCL processing flow chart

The processing process is roughly divided into the following steps:

(1) Receive status, that is, the entry status of request processing. According to VCL rules, it is determined that the request should be Pass or Pipe, or enter Lookup (local query).

(2) Lookup status: look for the requested object in the cache. If there is no requested object in the cache, subsequent operations are likely to cache the requested object. After entering this state, it will look up the data in the hash table. If found, it will enter the Hit state, otherwise it will enter the miss state.

(3) Pass status. In this state, the backend request is entered, that is, the fetch fetching status is entered.

(4) Fetch status. In the Fetch fetching state, the backend acquires the request, sends the request, obtains the data, and stores it locally.

(5) Deliver provides the status, sends the obtained data to the client, and then completes this request.

Note:

Pass: bypass the cache, that is, do not query or store content in the cache

Pipe: instead of checking or taking any action on the client, a dedicated "pipeline" is established between the client and the back-end server, and the data is transferred directly between the two. In this case, the subsequent data transferred in the keep-alive connection will also be transferred directly through this pipeline and will not appear in any log.

4) Summary: when users accelerate through varnish, there are 4 lines to get data.

One: experimental objectives

Practice 1: cache a website

Practice 2: using varnish to cache multiple websites

Second, the experimental environment

Varnish proxy server xuegod63 192.168.1.63

Web server xuegod64 192.168.1.64

Web server xuegod62 192.168.1.62

Three: experimental code

1. Install varnish

1) install the varnish package

[root@xuegod63] # rpm-ivh varnish-libs-3.0.6-1.el6.x86_64.rpm

[root@xuegod63] # rpm-ivh varnish-3.0.6-1.el6.x86_64.rpm

Parameters:

-- nosignature wants to skip the check of digital signatures and does not check for validity. Because this rpm package is not released by redhat.

-I install installation

2) vainish configuration file location

Vcl configuration file:

[root@xuegod63 ~] # ls / etc/varnish/default.vcl

Varnish main configuration file

[root@xuegod63 ~] # vim / etc/sysconfig/varnish

3) start the varnish service:

[root@xuegod63 ~] # / etc/init.d/varnish start

2. Configure xuegod63 to become a varnish acceleration proxy server

1) configure xuegod63 to become a varnish server

[root@xuegod63 ~] # vim / etc/varnish/default.vcl

# configure a backend server

Change:

7 backend default {

8. Host = "127.0.0.1"

9. Port = "80"

10}

Are:

Backend web1 {

.host = "192.168.1.64"

.port = "80"

}

# View cache hits

In:

90 # sub vcl_deliver {

91 # return (deliver)

92 #}

Add:

Sub vcl_deliver {

If (obj.hits > 0) {

Set resp.http.X-Cache = "HIT fromxuegodcache"

}

Else {

Set resp.http.X-Cache = "MISS from xuegodcache"

}

Return (deliver)

}

Save exit.

2) configure varnish service port

[root@xuegod63 ~] # vim / etc/sysconfig/varnish

Change:

66 VARNISH_LISTEN_PORT=6081

Are:

VARNISH_LISTEN_PORT=80

3) start the varnish server

[root@xuegod63 ~] # / etc/init.d/varnish start

Starting Varnish Cache: [OK]

3. Configure xuegod64 as the backend web server

1) configure xuegod64 as the backend web server

[root@xuegod64 ~] # yum install httpd-y

[root@xuegod64 ~] # echo 192.168.1.64 > / var/www/html/index.html# create Home Page

2) to solve the problem, you need to enable the apache long link function.

[root@xuegod64 ~] # vim / etc/httpd/conf/httpd.conf

Change: 76 KeepAliveOff

Is: KeepAlive On

2) start Apache server

[root@xuegod64 ~] # service httpd restart# starts the web server on xuegod64

4. Practice 1: cache a single web website

1) Test source site: visit: http://192.168.1.64/ in the browser

2) Test acceleration: visit http://192.168.1.63/ in the browser

It can be accessed normally, which means that the proxy setting is successful.

Test cache hit:

Extending: curl command

Curl is a tool software that uploads or downloads files under the command line through url syntax. It supports many protocols such as http,https,ftp,ftps,telnet and is often used to crawl web pages and monitor the status of Web servers.

Parameters:

-I only take the information of the http response header, not the web page content

3) View the type of web server started

[root@xuegod63 ~] # curl-I 192.168.1.6 View the types of web servers started by xuegod64 on xuegod63

HTTP/1.1 403 Forbidden

Date: Sat, 08 Aug 2015 11:17:49 GMT

Server:Apache/2.2.15 (Red Hat)

Accept-Ranges: bytes

Content-Length: 3985

Connection: close

Content-Type: text/html; charset=UTF-8

4) Test cache hit

[root@xuegod63] # curl-I 192.168.1.63

HTTP/1.1 503 Service Unavailable

Server: Varnish

Content-Type: text/html; charset=utf-8

Retry-After: 5

Content-Length: 419

Accept-Ranges: bytes

Date: Sat, 08 Aug 2015 11:12:20 GMT

X-Varnish: 1141298100

Age: 0

Via: 1.1 varnish

Connection: close

X-Cache:MISS from linuxidc cache# missed

5) Test again:

[root@xuegod63] # curl-I 192.168.1.63

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Sat, 08 Aug 2015 11:21:11 GMT

ETag: "25f61-d-51ccaf318dbb9"

Content-Type: text/html; charset=UTF-8

Content-Length: 13

Accept-Ranges: bytes

Date: Sat, 08 Aug 2015 11:28:37 GMT

X-Varnish: 1141298121 1141298120

Age: 1

Via: 1.1 varnish

Connection: keep-alive

X-Cache:HIT from linuxidc cache# hit

6) basic operation of varnish server:

[root@xuegod63 ~] # varnishadm ban.url. * $# clear all caches

[root@xuegod63] # curl-I 192.168.1.63

. . .

X-Cache: MISS from linuxidc cache

[root@xuegod63 ~] # varnishadm ban.url / index.html # clear index.html page cache

7) Test:

[root@xuegod63] # curl-I 192.168.1.63/index.html

[root@xuegod63 ~] # varnishadm ban.url / admin/$ # clear the admin directory cache

Practice 2: using varnish to accelerate web servers at multiple different domain name sites

1) configure xuegod62 as a web2 server

[root@xuegod62 ~] # yum install httpd-y

[root@xuegod62 ~] # echo 192.168.1.62 > / var/www/html/index.html

[root@xuegod62 ~] # vim / etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

:: 1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.62 xuegod62.cn

[root@xuegod62 ~] # service httpd restart

2) configure the varnish server on xuegod63:

[root@xuegod63 ~] # vim / etc/varnish/default.vcl

Change:

Backend web1 {

.host = "192.168.1.64"

.port = "80"

}

Are:

Backend web1 {

.host = "192.168.1.64"

.port = "80"

}

Backend web2 {

.host = "192.168.1.62"

.port = "80"

}

[root@xuegod63 ~] # vim / etc/varnish/default.vcl# in the previous sub vcl_deliver {. } before, insert the following:

Sub vcl_recv {

If (req.http.host ~ "^ (www.)? xuegod.cn") {

Set req.http.host = "www.xuegod.cn"

Set req.backend = web1

} elsif (req.http.host ~ "^ bbs.xuegod.cn") {

Set req.backend = web2

} else {error 404 "xuegodcache"

}

}

Sub vcl_deliver {

# get data from web1 when accessing www.xuegod.cn domain name, get data from web2 when accessing bbs.xuegod.cn domain name, and report an error when visiting other pages.

3) reload the varnish configuration file:

[root@xuegod63 ~] # service varnish reload

Loading vcl from / etc/varnish/default.vcl

Current running config name is boot

Using new config name reload_2015-08-08T19:42:11

VCL compiled.

Available 2 boot

Active 0 reload_2015-08-08T19:42:11

Done

4) testing: Web page visits to www.xuegod.cn and bbs.xuegod.cn will show different content.

Test on xuegod62:

[root@xuegod62 ~] # vim/etc/hosts# add hosts file

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

:: 1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.62 xuegod62.cn

192.168.1.63 www.xuegod.cn

192.168.1.63 bbs.xuegod.cn

[root@xuegod62 ~] # yum install elinks-y

[root@xuegod62 ~] # elinks www.xuegod.cn--dump

192.168.1.64

[root@xuegod62 ~] # elinks bbs.xuegod.cn--dump

192.168.1.62

5) Test error message: http://192.168.1.63/

The above content is how to use Varnish to speed up the web website. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 202

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report