Document directory
- [Compression]
- [Configure compression in IIS]
- [Content expirations]
- [Configure content expirations in IIS]
- [Remove unnecessary HTTP headers]
[Overview]
In this chapter, we focus on infrastructure construction.
- HTTP compression (compression)
- Content expirations (content expiration)
- Content Distribution Networks-CDN (Content Delivery Network)
- Etags (E tag)
- Remove unnecessary HTTP headers
[Compression]
As we mentioned earlier, one of the performance rules is that the less data transmitted between the server and the browser, the better. In this rule, we can use a mature technology: HTTP compression.
- The server evaluates the "Accept-encoding" in the header of the HTTP request sent by the client ". To determine whether the client can process compressed data. If yes, the server compresses the data and returns the result to the client.
- In the keynote third-party service evaluation, the result is that the bandwidth can be reduced by 53% on average, and the website is faster by 25% on average.
- Currently, IIS also provides support for this technology (free of charge)
Iis7 support for compression
- Supports automatic stop when the CPU usage is too high
- By default, the minimum file size is 256 kb.
- Static content compression is enabled by default.
If you are interested in using compression in IIS. See http://weblogs.asp.net/owscott/archive/2004/01/12/57916.aspx
[Configure compression in IIS]
Open IIS. Select a website. Select compression on the right tab)
Some may find that dynamic compression is gray. In fact, we did not install the dynamic compression module. I am using win7. The installation method is as follows:
Control Panel-Program-enable or disable Windows functions-Internet Information Service-World Wide Web Service-performance function (cumbersome)-Dynamic Content Compression
If a friend of the win server version needs:
-
- Administrative Tools | Server Manager.
- Expand roles and click Web Server (IIS ).
- Scroll to role services and click Add role services to open the Add role services wizard.
- On the select role Services page, scroll to performance, select Dynamic Content compression, and select next.
Now we also use the website used in Chapter 1 for testing. Open Fiddler.
Write down the size of each file. Return to IIS, enable dynamic compression and static compression. Now when IIS receives a request to my Js and CSS files. First, it will determine whether the file requested by Bei has been compressed. Without it, it will be compressed and stored in a directory. When other users request the same file, they can directly obtain the compressed file.
Now let's go back to Fiddler and check the result.
The jquery file is compressed from 236k to 90 K. Other JS and CSS files are also compressed accordingly.
Now we can use Microsoft Network Monitor, another tool in the previous chapter, to observe the changes before compression and after compression.
Before compression:
After compression:
It is very easy to compare with MS network monitor. Before and after compression, the number of frames is significantly reduced, from 40 to 9.
More than 95% of requests are asked whether to enable compression. Therefore, mainstream browsers support compression.
Enabling compression consumes a little CPU. Iis7 optimized this part.
To ensure that compression does not overload the CPU, iis7 calculates the average CPU usage every 30 seconds. When the CPU usage exceeds a limit, compression is automatically disabled. When the CPU usage is lower than the limit, compression is enabled again.
The default value is:
|
Disable Compression |
Restart Compression |
Dynamic files |
90% |
50% |
Static files |
100% |
50% |
Note that this means that if the CPU on the server is always above 50%, but occasionally higher than 90%, dynamic File compression will be disabled, but it will not be enabled again.
You can modify these restrictions by modifying the applicationhost. config file, which is usually in the c: \ windows \ system32 \ inetsrv \ config folder.
Find the
2. Modify the httpdynamiccompressionenablecpuusage attribute:<
httpCompression
dynamicCompressionEnableCpuUsage
=
"70"
.... >
3. Restart IIS.
[Content expirations]
Let's review the first performance rule we mentioned. One is to transmit data as often as possible.
When a user accesses the homepage of my website, he will get my Js, CSS, images, and other files. The browser saves it to the local cache. A few days later, the user came to visit my website. The server will also check that there are files in the browser cache. If yes, it will tell the server. I have this file. But I want to know if its version is the latest. The server will check whether the file has been changed. If changed, the server will send a new file. If no, the server returns the browser status code 304. Still.
304. not changed.
Use browser cache:
- Set the expiration time for content folder
- Avoid files that are not frequently requested
- Rename the file. If you need to rewrite the browser cache (Version Number/timestamp)
[Configure content expirations in IIS]
Open IIS. Select your site. Select the scripts folder (where JS is installed. If any) in the view tab, find the HTTP response header and enter.
In the upper-right corner, set the common header-to make the web content expire. Let's set it to 5 days. Visit our website at this time. The following figure shows how to use fiddler:
We can calculate the max-age. Exactly 5 days: now we can open ie again. Internet Options-view history-settings-view files
Find our JS file. View attributes, expire on April 9, August 7, exactly five days :)
[CDN]
Another effective way to improve website performance is to use CDN.
CDN stands for content delivery network (CDN. The basic idea is to avoid bottlenecks and links on the Internet that may affect data transmission speed and stability, so that content transmission can be faster and more stable. A layer of smart virtual network formed by placing node servers in various parts of the network on the basis of the existing Internet, the CDN system can redirect users' requests to the nearest service node in real time based on the network traffic and connection, load status, distance to the user and response time of each node.. The aim is to allow users to obtain the desired content nearby, solve the problem of Internet congestion, and improve the response speed when users access the website.
Here I only do a brief introduction, need to know more details, see: http://en.wikipedia.org/wiki/Content_delivery_network
The readers will surely say that I am a zombie. What are so many things? Is CDN used by large companies related to us?
The following describes the CDN that we can use:
Jquery is one of the most popular JS libraries. Google, Microsoft, and jquery.com all host jquery libraries on their CDN for free. See:
Here I can use jquery files on our own server. You can also choose to read from CND on the official Google, Microsoft, and jquery websites. That is, you can reduce the pressure on your server. You can also use CND to speed up reading.
[Etags]
What is etags? The full name of etag is entity tag. Etag is actually a value composed of hash + changenumber. Hash is generated by the file content. In iis7, changenumber is 0 by default;
As mentioned earlier in this Chapter, when we set the content to expire and use browser cache to achieve performance optimization, we will use expires headers to tell the browser that we no longer need to send conditional GET requests to the server, you can directly use the data in the cache to speed up access.
The etags process is as follows:
- The client requests a resource source.
- The server returns the page Souce and adds an etag to the source.
- The client displays the page and caches the page together with etag.
- The customer requests the resource source again and passes the etag returned by the server in the last request to the server.
- The server checks the etag and determines that the page has not been modified since the last client request. The server returns the response 304 (not modified -- not modified) and an empty response body.
From this point of view, etag may mislead the browser and allow the browser to ignore the cache and repeatedly download the same file.
We try to delete etag from.
Let's see what the etag looks like before it is deleted?
using System;using System.Collections.Generic;using System.Linq;using System.Web;/// <summary>/// Summary description for ETagRemoveModule/// </summary>public class ETagRemoveModule : System.Web.IHttpModule{ public ETagRemoveModule() { // // TODO: Add constructor logic here // } public void Dispose() { } public void Init(HttpApplication context) { context.EndRequest += new EventHandler(HandlerEndRequest); } public void HandlerEndRequest(Object sender, EventArgs e) { System.Web.HttpContext.Current.Response.Headers.Remove("ETag"); }}
<system.webServer> <modules> <add name="ETagRemoveModule.cs" type="ETagRemoveModule"/> </modules> </system.webServer>
Etags is missing :)
[Remove unnecessary HTTP headers]
We can see from Fiddler that the HTTP response header contains a lot of information.
However, there is no need to display some information to users. For example, we use. netframework 4. 0. We use iis7.5.
First, it is redundant information. Secondly, it may also be used by some hackers.
So let's modify our code:
public void HandlerEndRequest(Object sender, EventArgs e) { HttpResponse oResponse = System.Web.HttpContext.Current.Response; if (null != oResponse) { oResponse.Headers.Remove("X-AspNet-Version"); oResponse.Headers.Remove("Server"); oResponse.Headers.Remove("ETag"); } }
Our headers are much cleaner and more secure.
The next chapter introduces [frontend performance]
References:
Http://technet.microsoft.com/en-us/library/cc730629.aspx
Http://weblogs.asp.net/owscott/archive/2004/01/12/57916.aspx
Http://www.infoq.com/cn/articles/etags
Http://www.webusability.cn/what-is-etag-howto-remove-etags-iis6-iis7-759/
Http://en.wikipedia.org/wiki/Content_delivery_network