Introduction to reverse proxy caching using ASP.net performance optimization _ Practical Tips

Source: Internet
Author: User

So far, we've talked about storing caches in asp.net output cache (memory and hard disk), and browser caching, while another common practice for large sites is to deploy caching on reverse proxy servers, which we often call reverse proxy caching, such as squid and varnish. These two software are typically deployed on non-Windows platforms, and for ASP.net on Windows platforms, we can deploy reverse proxy software to Linux, and the agent will then route to a Windows WEB (IIS) server in the background. In short, the non-Windows world is wonderful.

Of course, both squid and varnish have extended versions of Windows. For simplicity, this article is based on the varnish version of Windows.

Official site of varnish: https://www.varnish-cache.org/,

Varnish version of Windows: http://www.cygwin.com/, if you want to compile a directly available version, here: http://www.software112.com/products/cygwin-varnish-cache.html.
1: Configure the varnish as a proxy for IIS
First you need to prepare the configuration file for varnish, for example, it can be DEFAULT.VCL, as follows:

Copy Code code 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 this example we are going to demonstrate, none of these 3 configurations can be less, like the following,
Backend Default: Specifies the address and port of our IIS site;
Sub Vcl_fetch: This is a varnish function that is invoked varnish from the back-end server, which is the data obtained from IIS;
Sub Vcl_recv:varnish function, which indicates that the client requests the lever to be invoked when it arrives at the reverse proxy server;
Because varnish defaults to ignoring the cache directly when it encounters a cookie-related identity in the HTTP header, we need the two functions above to treat cookies specifically. Of course, these two functions are simply and savage deletion of the identity, in practical applications we may need to add some judgment conditions to them.
2: Start varnish
The following command starts 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 8011 ports. Since varnish and IIS are on the same machine in my test environment, IIS already occupies 80, and I only use other ports.
-T is to specify a management port for the varnish;
-f Specifies the configuration file to use;
The following parameters just let varnish use file caching, size of 100M, of course, should be specified according to the actual size;
After starting varnish, if we request the http://address: port/, we can wait until the 200OK status code, which means that varnish has accepted the request in the correct drop.
3: An instance
Create a ASP.net page that reads as follows:
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
this. Response.AddHeader ("Cache-control", "max-age=60");
this. Response.AddHeader ("Last-modified", DateTime.Now.ToString ("U", Datetimeformatinfo.invariantinfo));
DateTime ifmodifiedsince; The
if (datetime.tryparse) (this. Request.Headers.Get ("If-modified-since"), out ifmodifiedsince))
{
If (datetime.now-ifmodifiedsince.addhour S (8)). Seconds <)
{
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" 1* from Nametb A, DEPTB b wher E A.depid = b.id ORDER by NEWID ())
{
var result = ds. Tables[0]. rows[0]["Name"]. ToString ();
Response.Write (Result);
}
}

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

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

You can see a very large increase in throughput rates.

4: Monitoring varnish

You can use the Varnishstat command to monitor varnish, and in the stress tests above, 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 it is clear that the output drops from IIS are definitely being taken.
5: Management varnish
There are several ways to manage varnish, including changing configurations, stopping services, starting services, cleaning caches, and so on. Can be managed through the VARNISHADM command, if you are remotely, you can use Telnet to manage:
Telnet 192.168.0.77 8088
8088 of these are the admin 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 brought after careful introduction of varnish
After the introduction of varnish, it can be found that the dynamic behavior has changed after using forced flush (CTRL+R5), that is, the client browser is going to request data on the varnish, but there is already static cached content in the cache. Varnish will first determine whether or not to update the cached content based on the HTTP headers requested, that is, because the cached content exists, the request does not go to the IIS for caching negotiation. 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 mandatory update, ignores caching, goes directly to IIS, and adds the following function to the configuration file:

Copy Code code as follows:

Sub Vcl_hit {
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);
}
return (deliver);
}

After the above modifications, using the force update again varnish will ignore the cache and go to IIS to get the body.
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/

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.