Introduction to reverse proxy cache for ASP. NET performance optimization

Source: Internet
Author: User
Tags varnish

So far, we have discussed storing cache in ASP. NET output cache (memory and hard disk), and browser cache. Another common practice for large websites is to deploy the cache on the reverse proxy server, this type of cache is generally called reverse proxy cache, such as Squid and Varnish. These two software are usually deployed on non-WINDOWS platforms. For Asp.net on Windows, we can actually use the same software. We can completely deploy the reverse proxy software on LINUX, then, the proxy routes the data to the background windows web (IIS) server. In short, the non-WINDOWS world is wonderful.

Of course, both squid and varnish have Windows extension versions. This article describes varnish-based Windows for simplicity.

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

Varnish Windows: Windows.
1: Configure varnish as a proxy for IIS
First, you must prepare a configuration file for varnish, for example, default. vcl. The content is as follows:
Copy codeThe Code 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 instance we want to demonstrate, none of the three configurations should be less, as shown below,
Backend default: Specifies the address and port of our IIS Site;
Sub vcl_fetch: This is a varnish function. varnish is called after obtaining data from the backend server, that is, IIS;
Sub vcl_recv: varnish function, which indicates that the client is called when the client requests the lever to the reverse proxy server;
Varnish directly ignores the cache when it encounters Cookie-related identifiers in the http header by default. Therefore, we need the above two functions to perform special processing on cookies. Of course, at present, these two functions are simple and barbaric to delete the identifiers. In actual applications, we may need to add some judgment conditions for them according to the actual situation.
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, 100 M
-A: 8011 indicates that varnish is listening on port 8011. In my testing environment, varnish and iis are on the same machine, so IIS has occupied 80. Here I only use other ports.
-T specifies a Management port for varnish;
-F specifies the configuration file to be used;
The following parameters only enable varnish to use the File Cache. The file size is 100 mb. Of course, you should specify the size based on the actual situation;
After varnish is started, if we request http: // address: Port/, we can wait until the 200OK status code, which means varnish has accepted the request correctly.
3: one instance
Create an asp.net page with the following content:
Copy codeThe Code is 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;
If (DateTime. TryParse (this. Request. Headers. Get ("If-Modified-Since"), out IfModifiedSince ))
{
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. depID = B. id order by newid ()"))
{
Var result = ds. Tables [0]. Rows [0] ["name"]. ToString ();
Response. Write (result );
}
}

Perform a stress test on this page. 100 users and 1000 requests will receive the following results:

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

We can see that the throughput has been greatly improved.

4: monitor varnish

You can use the varnishstat command to monitor varnish. In the stress test above, if we use monitoring, the result is as follows:


In this example, we can see that a total of 1000 requests, of which 999 hit the cache, it is because the first time it is clearly necessary to get the output drops from IIS.
5. Manage varnish
You can manage varnish in multiple ways, including changing configurations, stopping services, starting services, and clearing caches. You can use the varnishadm command for management. If you are remotely using the varnishadm command, you can use telnet for management:
Telnet 192.168.0.77 8088
8088 is the Management port we specified when we started varnish. After the connection, stop the service and start the service. You can press help to view all the commands. Run the following command to clear all caches:
Purge. url * $
6: Careful introduction of cache changes after varnish
After varnish is introduced, we can find that after force refresh (ctrl + R5) is used, the dynamic behavior changes, that is, the client browser will request data on VARNISH, however, static cache content already exists in the cache. varnish will first determine whether to update the content based on the HTTP header of the request and the cached content, that is, because the cached content exists, the request will not go to IIS for cache negotiation. At this time, the static content in the cache will be directly returned to the client browser, so that we will not execute the code in Page_Load, because it is in IIS.
To avoid this, we must change the VARNISH configuration file so that when VARNISH encounters a forced update, ignore the cache and directly go to IIS for the request. Add the following function to the configuration file:
Copy codeThe Code is 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 = 0 s;
Return (restart );
}
Return (deliver );
}

After the above modification, if you use force update varnish again, the cache will be ignored and the text will be taken to IIS.
Refer:
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/

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.