[Go] using varnish in a Windows environment

Source: Internet
Author: User
Tags browser cache varnish

Varnish version of Windows:

http://sourceforge.net/projects/cygvarnish/files/windows-zip-bundle/

Start: Varnish:

C:\varnish\bin>varnishd-a: 80-t: 3500-f c:/varnish/etc/default.vcl-s file,c:/varnish/var/cache,1000m

-a:80 says, let varnish listen on port 80.

-T is a management port specified for varnish;

-f Specifies the configuration file to use;

The following parameters just let varnish use file cache, size 1000M, of course, should be specified according to the actual size;

After starting varnish, if we request http://Port/, you can wait until the 200OK status code, that means that varnish has received the request correctly.

Configuration file for Yellow Sea:

Backend Default {
 . Host = "192.168.88.177";
 . Port = "80";
}
#定义可以清空缓存的IP段
ACL purge {
        "localhost";
        "192.168.88.0"/24;
}

 sub vcl_recv {
  #开启压缩模式, picture format de-compress
 if (req.http.accept-encoding) {
      if (req.url ~ "\. ( jpg|png|gif|jpeg|flv) {
          remove req.http.accept-encoding;
          Remove Req.http.Cookie;
     } else if (req.http.accept-encoding ~ "gzip") {
       & nbsp;  Set req.http.accept-encoding = "gzip";
     } else if (req.http.accept-encoding ~ "deflate") {
           Set req.http.accept-encoding = "deflate";
     } else {
          remove req.http.accept-encoding;
     }
 }

if (Req.restarts = = 0) {
if (req.http.x-forwarded-for) {
Set req.http.x-forwarded-for =
Req.http.x-forwarded-for + "," + client.ip;
} else {
Set req.http.x-forwarded-for = Client.ip;
}

}
#配置varnish批量刷新缓存
if (req.request = = "BAN") {
if (!client.ip ~purge) {
Error 405 "not allowed";
}
Ban ("Req.http.host = =" +req.http.host+ "&& req.url ~" +req.url);
Error "Ban added";
}

#把除了以下这些类型请求以外的访问请求全部直接管道发送到后端的服务器
if (req.request! = "GET" &&
Req.request! = "HEAD" &&
Req.request! = "PUT" &&
Req.request! = "POST" &&
Req.request! = "TRACE" &&
Req.request! = "OPTIONS" &&
Req.request! = "DELETE") {
/* non-rfc2616 or CONNECT which is weird. */
return (pipe);
}
#只有GET与HEAD方法才会使用Lookup, use the cache.
if (req.request! = "GET" && req.request! = "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
#如果请求的是jsp页面直接转发到后端服务器
if (req.url ~ "\. ( jsp|action) ($|\) ") {
return (pass);
}
return (lookup);
}

Sub Vcl_pipe {
return (pipe);
}

Sub Vcl_pass {
return (pass);
}

#使用url +host hash algorithm to find data
Sub Vcl_hash {
Hash_data (Req.url);
if (req.http.host) {
Hash_data (Req.http.host);
} else {
Hash_data (SERVER.IP);
}
return (hash);
}


# If the request is purge will clear the cache
Sub Vcl_hit {
#强制刷新后, ignore cache, request directly to background
if (req.http.cache-control~ "No-cache" | | req.http.cache-control~ "Max-age=0" | | req.http.pragma~ "No-cache")
{
Set obj.ttl=0s;
return (restart);
}

if (req.request = = "PURGE") {
set obj.ttl = 0s;
Error "purged";
}
return (deliver);
}


Sub Vcl_miss {
return (fetch);
}

Sub Vcl_fetch {
if (beresp.ttl <= 0s | |
Beresp.http.set-cookie | |
Beresp.http.Vary = = "*") {
/*
* Mark as "Hit-for-pass" for the next 2 minutes
*/
Set Beresp.ttl = + S;
return (Hit_for_pass);
}

if (beresp.http.Pragma ~ "No-cache" | |
Beresp.http.cache-control ~ "No-cache" | |
Beresp.http.cache-control ~ "Private") {
return (deliver);
}

#当url中包含servlet时, do not cache
if (req.url ~ "^/servlet/")
{
return (Hit_for_pass);
}
#当url中包含services时, do not cache
if (req.url ~ "^/services/")
{
return (Hit_for_pass);
}

if (req.request = = "GET" &&
Req.url ~ "\. (Png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg| css|js|html|htm) $ ")
{
Unset Beresp.http.set-cookie;
Set beresp.ttl = 1h;
}
#设置图片的缓存TTL为一小时
return (deliver);
}

Sub Vcl_deliver {
#下面是添加一个Header标识 to determine if the cache is hit.
if (Obj.hits > 0) {
Set Resp.http.x-cache = "hit from dsideal.com";
} else {
Set Resp.http.x-cache = "MISS from dsideal.com";
}
return (deliver);
}

Sub Vcl_error {
Set obj.http.content-type = "text/html; Charset=utf-8 ";
Set obj.http.retry-after = "5";
Synthetic {"
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<title> "} + Obj.status +" "+ Obj.response + {" </title>
<body>
<p> "} + obj.response + {" </p>
<p>xid: "} + Req.xid + {" </p>
<p>varnish Cache server</p>
</body>
"};
return (deliver);
}

Sub Vcl_init {
return (OK);
}

Sub Vcl_fini {
return (OK);
}
#

********************************************************************************************************

Online 3.0 version of the configuration instructions too little, this article is 3.0 version, good, have time to do an experiment.

http://wdj01.blog.51cto.com/1059856/660571

ASP. NET performance-optimized reverse proxy caching 2011-10-17 08:50 by Minjia, 2598 visits, collection, compilation

So far, we've talked about storing the cache in the output cache of ASP. (Memory and hard disk), and in the browser cache, and another common practice for large sites is to deploy caches on reverse proxy servers, which we often call reverse proxy caching, such as squid and varnish. These two software are usually deployed on the non-Windows platform, for the Windows platform on the net, in fact, the same can be used, we can fully deploy the reverse proxy software on Linux, and then the agent will be routed to the background of the Windows WEB (IIS) server. In short, the world of non-Windows is wonderful.

Of course, both squid and varnish have an extended version of Windows. This article is described for brevity, based on the varnish version of Windows.

Varnish's official site:https://www.varnish-cache.org/,

Varnish version of Windows:http://www.cygwin.com/, if you want to compile the directly available version, here:http://www.software112.com/products/ Cygwin-varnish-cache.html.

1: Configure varnish as the agent for IIS

First, you need to prepare the configuration file for varnish, for example, can be DEFAULT.VCL, the content is as follows:

backend default{      .host = "192.168.0.77";      .port = "80"; } sub vcl_fetch {     remove beresp.http.Set-Cookie; } sub vcl_recv  {     remove req.http.Cookie; }

In the example we're going to demonstrate, none of these 3 configurations can be as low as

Backend Default: Specifies the address and port of our IIS site;

Sub Vcl_fetch: This is a varnish function that is called varnish from the backend server, which is the data obtained from the IIS;

Sub Vcl_recv:varnish function, which indicates that the client requests the leverage to reach the reverse proxy server when it is called;

Since varnish defaults to ignoring the cache directly when it encounters a cookie-related identity in the HTTP header, we need the above two functions for special handling of cookies. Of course, at present these two functions are simply and savage delete the identity, in the actual application we may need to according to the actual situation to add some judgment condition to them.

2: Start varnish

The following command launches varnish for me:

C:\varnish\bin>varnishd-a: 8011-t: 8088-f c:/varnish/etc/default.vcl-s file,c:/varnish/var/cache,100m

-a:8011 says, let varnish listen on port 8011. Because varnish and IIS are on the same machine in my test environment, IIS has taken up 80, and I only use other ports here.

-T is a management port specified for varnish;

-f Specifies the configuration file to use;

The following parameters just let varnish use file cache, size 100M, of course, should be specified according to the actual size;

After starting varnish, if we request http://Port/, you can wait until the 200OK status code, that means that varnish has received the request correctly.

3: one instance

Create an ASP. NET page with the following:

protectedvoidPage_Load(objectsender, EventArgs e) {     this.Response.AddHeader("Cache-Control", "max-age=60");     this.Response.AddHeader("Last-Modified", DateTime.Now.ToString("U", DateTimeFormatInfo.InvariantInfo));     DateTime IfModifiedSince;     if(DateTime.TryParse(this.Request.Headers.Get("If-Modified-Since"), outIfModifiedSince))     {         if((DateTime.Now - IfModifiedSince.AddHours(8)).Seconds < 60)         {             Response.Status = "304 Not Modified";             Response.StatusCode = 304;             return;         }     }     string conn = "Data Source=192.168.0.77;Initial Catalog=luminjidb;User Id=sa;Password=sa;";     using(DataSet ds = Common.SqlHelper.ExecuteDataset(conn, CommandType.Text, "select top 1* from NameTb a, DepTb b where a.DepID = b.ID ORDER BY NEWID()"))     {         var result = ds.Tables[0].Rows[0]["name"].ToString();         Response.Write(result);     } }

Stress test on the page, 100 users, 1000 requests, the results are as follows:

If there is no cache, the result is as follows:

You can see a very large increase in throughput rates.

4: Monitoring varnish

The varnish can be monitored using the varnishstat command, and in the above stress tests, if we use monitoring, the results are as follows:

In this example, we can see a total of 1000 requests, of which 999 hits are cached, because the first time obviously must be to take the output from IIS.

5: Management varnish

There are several ways to manage varnish, including changing configurations, stopping services, starting services, cleaning up caches, and so on. Can be managed via the VARNISHADM command, and if you are remotely, you can use Telnet to manage:

Telnet 192.168.0.77 8088

8088 of these are the management ports that we just specified when we started varnish. After the connection, stop stop service, start start service, you can click Help to view all commands. The following command clears all caches:

Purge.url *$

6: Cache Changes After careful introduction of varnish

After introducing varnish, it can be found that with forced flush (CTRL+R5), the dynamic behavior has changed, that is, the client browser will go to the varnish to request data, but at this time there is already a static cache content in the cache, Varnish will first determine whether it needs to be updated based on the HTTP header of the request and the cache content, that is, because the cache content exists, the request does not go to cache negotiation on IIS. At this point, the static content in the cache is returned directly to the client browser, so that our code in Page_Load will not execute at all, because it is in IIS.

To avoid this, we have to change the varnish configuration file so that varnish encounters a forced update, ignoring the cache, directly to the request on IIS, adding the following function to the configuration file:

sub vcl_hit {     if(req.http.Cache-Control~"no-cache"||req.http.Cache-Control~"max-age=0"||req.http.Pragma~"no-cache"){         setobj.ttl=0s;         return(restart);     }     return (deliver); }

After the above modification, using force update again varnish will ignore the cache and go to IIS to get the text.

Reference:

Https://www.varnish-cache.org/docs/trunk/reference/varnishlog.html

Https://www.varnish-cache.org/trac/wiki/Introduction#TheVarnishConfigurationLanguage

Http://www.docunext.com/wiki/Varnish

http://cd34.com/blog/infrastructure/no-esi-processing-first-char-not/

Previous articles in this series:

1:ASP. NET performance optimization build custom file Cache

2:ASP. NET performance optimization allows the browser to cache dynamic Web pages

3:ASP. NET performance optimization reduction requests

[Go] using varnish in a Windows environment

Related Article

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.