varnish_Varnish入门

varnish

Caching is everywhere. A request from your browser to its destination will pass a cache at almost every node. Your browser has a cache, most web proxy servers cache requests, and web servers like Apache and nginx will potentially be caching.

缓存无处不在。 从浏览器到目的地的请求将在几乎每个节点上传递一个缓存。 您的浏览器具有缓存,大多数Web代理服务器都缓存请求,而Apache和Nginx等Web服务器可能会缓存。

Varnish is a reverse proxy server; it sits in front of your web server and serves content from your server and no one else’s. Reverse proxy servers are tightly coupled to the web server and can act on messages received from it. For example, a cached page can be refreshed with a purge command from the backend, something you are unable to do with caches closer to the client. This means reverse proxy servers can, in some cases, cache content longer than the other types of caches.

Varnish反向代理服务器 ; 它位于您的Web服务器的前面,并从您的服务器(而不是其他任何服务器)提供内容。 反向代理服务器与Web服务器紧密耦合,可以对从Web服务器接收到的消息进行操作。 例如,可以使用来自后端的清除命令来刷新缓存的页面,而这对于靠近客户端的缓存是无法实现的。 这意味着在某些情况下,反向代理服务器可以比其他类型的缓存更长地缓存内容。

alt

Simply put, Varnish does one thing: serve web content super fast. It keeps your web server happy by handling most of the high traffic, and serves your visitors quickly. In other words, Varnish makes sure you are prepared for spikes in traffic and helps keep your bounce rate low.

简而言之,Varnish做一件事:超快速地提供Web内容。 通过处理大多数高流量,它可使您的Web服务器保持满意状态,并Swift为访问者提供服务。 换句话说,Varnish可确保您为流量激增做好准备,并有助于将跳出率保持在较低水平。

As a caching service, Varnish is not unique, but, when it comes to performance, it really shines. The head architect of Varnish is Paul Henning Kamp (who is also a prominent FreeBSD developer). Every architectural decision made is focused on improved performance and/or better flexibility, which is why Varnish has few dependencies, its own configuration language, and is optimized for modern hardware running FreeBSD and GNU/Linux. All this results in a reverse proxy server that takes full advantage of the operating system in order to serve content extremely fast.

作为一种缓存服务,Varnish不是唯一的,但是,在性能方面,它确实令人眼前一亮。 Varnish的首席架构师是Paul Henning Kamp(他也是著名的FreeBSD开发人员)。 做出的每个体系结构决策都集中在提高性能和/或更好的灵活性上,这就是为什么Varnish具有很少的依赖关系,其自己的配置语言并针对运行FreeBSD和GNU / Linux的现代硬件进行了优化的原因。 所有这些都导致了反向代理服务器,该服务器充分利用了操作系统,以极快地提供内容。

In this article we’ll set up Varnish to cache a simple web page for two minutes. We’ll walk through setting up a web server and configuring Varnish to cache our page. To keep things simple, we’ll set up both on the same host and use a Debian-based distro. You can later customize your setup as your needs dictate.

在本文中,我们将设置Varnish以将一个简单的网页缓存两分钟。 我们将逐步设置网络服务器并配置Varnish来缓存我们的页面。 为简单起见,我们将两者都设置在同一主机上,并使用基于Debian的发行版。 您以后可以根据需要自定义设置。

您的第一个清漆设置 (Your First Varnish Setup)

Varnish fetches content from a backend server, which is the web server where your content is generated (in this case Apache). Our example will use a simple PHP Hello World page that updates every time its refreshed. So, let’s set that up first:

Varnish从后端服务器获取内容,后端服务器是生成您的内容的Web服务器(在本例中为Apache)。 我们的示例将使用一个简单PHP Hello World页面,该页面每次刷新时都会更新。 因此,让我们先进行设置:

$ sudo apt-get install apache2 php5
$ echo '<?php echo date('h:i:s') . 'Hello, world!'; | sudo tee /var/www/world.php

Test that the page works by opening it in your browser. The time should update every time you refresh the page.

通过在浏览器中打开页面来测试该页面是否正常工作。 每次刷新页面时,时间都会更新。

Let’s install Varnish next. It’s currently at version 3.x, and because it’s is developed and optimized for FreeBSD and GNU/Linux, you’ll find that all the main package systems support Varnish.

接下来让我们安装Varnish。 它目前的版本为3.x,并且由于它是针对FreeBSD和GNU / Linux开发和优化的,因此您会发现所有主要的软件包系统都支持Varnish。

$ sudo apt-get install varnish

To configure Varnish, there are two files we need to edit: /etc/default/varnish and /etc/varnish/default.vcl. The first sets the options for varnishd, the Varnish daemon, and the second configures Varnish itself.

要配置Varnish,我们需要编辑两个文件: /etc/default/varnish/etc/varnish/default.vcl 。 第一个设置varnishd的选项,Varnish守护程序,第二个配置Varnish本身。

The location of the varnishd options file depends on your choice of operating system. For Debian based systems, make sure the DAEMON_OPTS variable in /etc/default/varnish is:

清漆选项文件的位置取决于您选择的操作系统。 对于基于Debian的系统,请确保/etc/default/varnishDAEMON_OPTS变量为:

DAEMON_OPTS="-a :6081  
             -T localhost:6082  
             -f /etc/varnish/default.vcl  
             -S /etc/varnish/secret  
             -s malloc,256m"

The -a option tells varnishd to listen on port 6081 for requests. You can change this to port 80 when you are finally ready to let Varnish handle requests. The option -f tells varnishd the location of the second configuration file. The -T sets the location of the management interface. The management interface is where you can make changes to Varnish runtime. The -S options sets the location of the authentication secret for the management interface. The option -s decides how cached objects are stored.

-a选项告诉varnished在端口6081上侦听请求。 最终准备好让Varnish处理请求时,可以将其更改为端口80。 选项-f告诉varnishd第二个配置文件的位置。 -T设置管理界面的位置。 您可以在管理界面上更改Varnish运行时。 -S选项设置管理界面的身份验证密码的位置。 选项-s决定如何存储缓存的对象。

Make sure the uncommented lines in /etc/varnish/default.vcl are as follows:

确保/etc/varnish/default.vcl中未注释的行如下:

backend default {
    .host = "127.0.0.1"; 
    .port = "80"; 
}

Varnish communicates with the backend using HTTP, it also supports multiple backends if you should need that. This setup will fetch content from localhost on port 80.

Varnish使用HTTP与后端通信,如果您需要的话,它还支持多个后端。 此设置将从端口80上的本地主机获取内容。

Now we can start varnishd:

现在我们可以开始上漆了:

$ sudo service varnish start

Try opening our Hello World page through Varnish by adding the port we set varnishd to listen to to the URL (ie: http://localhost:6081/world.php). When you refresh the page, you’ll notice that the time only updates once every two minutes (a default Varnish setup will cache a page for two minutes given that it is a HEAD or GET request without a set Authorization or Cookie header).

尝试通过添加添加我们设置为varnishd的端口以通过URL来打开Varnish的Hello World页面(即: http:// localhost:6081 / world.php )。 刷新页面时,您会注意到时间仅每两分钟更新一次(默认的Varnish设置会将页面缓存两分钟,因为它是没有设置授权或Cookie标头的HEAD或GET请求)。

配置清漆 (Configuring Varnish)

With our current setup, requests to localhost without a specified port will default to port 80 and are routed directly to Apache. When you feel comfortable that Varnish is caching what you want, you can switch Apache to listen on a different port, say 8080, and Varnish to listen on port 80 instead of 6081 in /etc/default/varnish. Remember to change the backend configuration in /etc/varnish/default.vcl too; it should be 8080 and not 80.

使用我们当前的设置,没有指定端口的对localhost的请求将默认为端口80,并直接路由到Apache。 当您对Varnish正在缓存所需的内容感到满意时,可以将Apache切换为侦听其他端口(例如8080),而Varnish切换为侦听端口80而不是/etc/default/varnish的6081。 记住也要在/etc/varnish/default.vcl更改后端配置; 它应该是8080,而不是80。

To configure Varnish further you need to know VCL, the Varnish configuration language (you might have noticed the file extension of the second file was .vcl). Having its own configuration language is a significant factor for achieving Varnish’s goals of flexibility and performance.

要进一步配置Varnish,您需要了解Varnish配置语言VCL(您可能已经注意到第二个文件的文件扩展名为.vcl)。 拥有自己的配置语言是实现Varnish的灵活性和性能目标的重要因素。

When Varnish processes a request, the request will go through a set of states. VCL code is how you decide what Varnish does with the request in each of these states, and this is why VCL is called a state engine. Each state has its own function in VCL which runs by default. You can edit the behavior of each state by redefining its function in default.vcl. Note that unless you add a return statement, redefining a function prepends the default VCL function. If you want to know what a default VCL function does, there is a copy of all the functions in a commented section at the bottom of the file.

当Varnish处理请求时,该请求将经过一组状态。 VCL代码是您决定在每种状态下Varnish如何处理请求的方法,这就是为什么VCL被称为状态引擎的原因。 每个状态在VCL中都有自己的功能,默认情况下会运行。 您可以通过在default.vcl重新定义每个状态的功能来编辑每个状态的行为。 请注意,除非添加return语句,否则重新定义函数会附加默认的VCL函数。 如果您想知道默认的VCL函数的作用,则在文件底部的带注释的部分中会列出所有函数。

It helps to see an example, so let’s edit vcl_fetch and change the time a page is cached in Varnish to 5 seconds. In default.vcl, add the following function:

它有助于查看示例,因此让我们编辑vcl_fetch并将页面在Varnish中的缓存时间更改为5秒。 在default.vcl ,添加以下功能:

sub vcl_fetch { 
  set beresp.ttl = 5s; 
} 

The varnish daemon will need to be reloaded for this to take effect, so restart the service with:

需要重新加载清漆守护程序才能生效,因此请使用以下命令重新启动服务:

$ sudo service varnish restart

When you refresh your Hello World page now, the time will update every 5 seconds.

立即刷新Hello World页面时,时间将每5秒更新一次。

When Varnish looks for a page in the cache but can’t find it, it’s a miss. if it does find it, it’s a hit. The function vcl_fetch controls the state your request will eventually be in after a miss. In vcl_fetch Varnish has fetched the page from the backend and now has to decide how to cache the page, if at all. Each function in VCL has a set of objects available to it, vcl_fetch has several, including the backend response object beresp. By setting ttl (time to live) on the beresp object, we are telling Varnish that the page should be stored in cache for 5 seconds before it is invalid. Once 5 seconds have passed, a new request to the page means Varnish will need to fetch the page from the backend again.

当Varnish在缓存中查找页面但找不到页面时,那就是miss 。 如果找到它,那就很成功了 。 函数vcl_fetch控制您的请求在未命中后最终将处于的状态。 在vcl_fetch Varnish已从后端获取了页面,现在必须决定如何缓存页面(如果有的话)。 VCL中的每个函数都有一组可用的对象, vcl_fetch有多个对象,包括后端响应对象beresp 。 通过在beresp对象上设置ttl (生存时间),我们告诉Varnish,该页面在无效之前应该存储在缓存中5秒钟。 5秒后,对页面的新请求意味着Varnish将需要再次从后端获取页面。

Note that in the example above, there is no return statement in vcl_fetch, this means the default function will run afterwards. This is a good idea when writing VCL, the default VCL is there for a reason and you should have a good reason to skip it.

请注意,在上面的示例中, vcl_fetch没有return语句,这意味着默认函数将在之后运行。 编写VCL时,这是一个好主意,默认VCL在那里是有原因的,您应该有充分的理由跳过它。

Varnish also has tools for analyzing and viewing its results and performance. These are helpful for fine tuning your configuration for high-load scenarios. To see one of them in action, try running varnishlog while you refresh your Hello World page with varnishlog -c. The -c option filters the varnish log for entries from your users as the requests arrive. To filter on entries to and from the backend, use the -b option.

Varnish还具有用于分析和查看其结果和性能的工具。 这些有助于在高负载情况下微调配置。 要查看其中之一,请尝试使用varnishlog -c刷新Hello World页面时尝试运行varnishlog。 -c选项在请求到达时过滤掉来自用户的条目的清漆日志。 要过滤来自后端的条目,请使用-b选项。

结论 (Conclusion)

That’s it, you now have a simple setup to experiment increasing your site’s performance. In this article you’ve learned the basics of Varnish, what it is and what the default setup will give you. We’ve also had a quick look at VCL, Varnish’s configuration language. VCL might be a little hard to wrap your head around, mostly because it is an unusual way of doing configuration – it is however surprisingly simple to use once you accept this approach, and it’s also more flexible than a configuration file.

就是这样,您现在有了一个简单的设置,可以尝试提高网站的性能。 在本文中,您学习了Varnish的基础知识,它是什么以及默认设置将为您提供什么。 我们还快速浏览了Varnish的配置语言VCL。 VCL可能很难缠起来,主要是因为它是一种不寻常的配置方法-但是,一旦接受这种方法,它使用起来就非常简单,而且比配置文件更灵活。

If you want to continue on your path learning Varnish, visit varnish-cache.org for more information. You can also ask for help on IRC, the #varnish channel on the freenode server is full of skilled and helpful people. And, something all web developers should do at one point, is read RFC2616. The section on caching headers is relevant for those who want to get a better understanding of Varnish and caching. Good luck!

如果您想继续学习Varnish,请访问varnish-cache.org获取更多信息。 您也可以在IRC上寻求帮助,freenode服务器上的#varnish频道充满了熟练和乐于助人的人。 而且,所有Web开发人员在某一时刻都应该做的事情是阅读RFC2616 。 缓存头部分与想要更好地了解Varnish和缓存的人有关。 祝好运!

翻译自: https://www.sitepoint.com/getting-started-with-varnish/

varnish

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值