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, while another common practice of 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 will route to the windows webiis server in the background. 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:

 
 
  1. backend default  
  2. {  
  3. .host = "192.168.0.77";  
  4. .port = "80";  
  5. }  
  6. sub vcl_fetch  
  7. {  
  8. remove beresp.http.Set-Cookie;  
  9. }  
  10. sub vcl_recv  
  11. {  
  12. 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: varnishin> 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:

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. this.Response.AddHeader("Cache-Control", "max-age=60");  
  4. this.Response.AddHeader("Last-Modified", DateTime.Now.ToString("U", DateTimeFormatInfo.InvariantInfo));  
  5. DateTime IfModifiedSince;  
  6. if (DateTime.TryParse(this.Request.Headers.Get("If-Modified-Since"), out IfModifiedSince))  
  7. {  
  8. if ((DateTime.Now - IfModifiedSince.AddHours(8)).Seconds <60)  
  9. {  
  10. Response.Status = "304 Not Modified";  
  11. Response.StatusCode = 304;  
  12. return;  
  13. }  
  14. }  
  15. string conn = "Data Source=192.168.0.77;Initial Catalog=luminjidb;User Id=sa;Password=sa;";  
  16. 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()"))  
  17. {  
  18. var result = ds.Tables[0].Rows[0]["name"].ToString();  
  19. Response.Write(result);  
  20. }  

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, you can find that the dynamic behavior changes after pressing ctrl + R5), 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:

 
 
  1. sub vcl_hit {  
  2. if(req.http.Cache-Control~"no-cache"||req.http.Cache-Control~"max-age=0"||req.http.Pragma~"no-cache"){  
  3. set obj.ttl=0s;  
  4. return (restart);  
  5. }  
  6. 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.

Original article: http://www.cnblogs.com/luminji/archive/2011/10/17/2178759.html

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.