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:
- 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: 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:
- 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 a.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, 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:
- 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 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