This article describes how to compress pages to save website bandwidth and increase user access speeds.
The website access speed is determined by multiple factors, such as the application response speed, network bandwidth, server performance, and network transmission speed between the client and the client. One of the most important factors is the application's response speed. Therefore, when you are worried about website performance, the first thing you need to do is to speed up application execution as much as possible. You can use cache or optimize code execution efficiency to speed up your application.
However, this article does not describe how to improve the efficiency of application execution, but just to prevent you from having to go to medicine. To ensure that your application performance is good enough and the server performance is fully satisfied, try web page compression to further improve the browsing speed of the web page, it does not require any cost at all, but it will slightly increase the CPU usage of your server by one or two percent or less.
Web page compression is a protocol that is shared by web servers and browsers. That is to say, web servers and browsers must support this technology. Fortunately, the popular browsers currently support this technology, including IE, Firefox, and opera. servers include Apache and IIS. The negotiation process is as follows:
- First, the browser requests a URL address and sets the attribute accept-encoding value to gzip, deflate in the request header, indicates that the browser supports gzip and deflate compression methods (in fact, deflate also uses the gzip compression protocol. The difference between the two is described below );
- After receiving the request, the WEB server determines whether the browser supports compression. If yes, the compressed response content is transmitted; otherwise, the uncompressed content is transmitted;
- After obtaining the response content, the browser determines whether the content is compressed. If yes, It decompress the content and then displays the content on the response page.
In practical applications, we found that the compression ratio is usually 3 to 10 times, that is, the page with a size of 50 kb, after compression, the actual size of the transmitted content is only 5 to 15 kb, which can greatly save the server's network bandwidth. If the application responds quickly enough, the speed bottleneck of the website is converted to the network transmission speed. Therefore, after the content is compressed, the page browsing speed can be greatly improved.
Next we will introduce how to enable the web page compression function in several common environments.
Pure Tomcat server
If your web application runs under the Tomcat server and directly uses the HTTP service provided by Tomcat, we recommend that you do it immediately because it is too simple, you only need. in the xml configuration file, add a compression parameter to HTTP ctor and restart the Tomcat server to take effect immediately. The configuration is as follows:
<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="20000" redirectPort="8443" compression="on"/> |
Tomcat adopts the HTTP/1.1 gzip compression protocol. It determines whether the browser supports the gzip compression protocol based on whether the accept-encoding value in the request sent by the Browser contains gzip, if the browser supports gzip compression, otherwise, no compression will be performed. Tomcat also has another parameter compressablemimetype, which can be used to specify the type of content to be compressed. For example, you can specify the configuration values as text/html, text/plain, only the pages whose contenttype is text/html and text/plain are compressed. However, you 'd better include CSS and JavaScript files in the compressed file type, because the compression effect of the two is also very obvious.
Apache server
In Apache 1.3, mod_gzip is commonly used to compress the output content. Currently, mainstream browsers support gzip decompression. In apache2, this module is renamedmod_deflate
The module File name ismod_deflate.so
. Mod_gzip is not described in this article. The following describes how to enable and configure it in Apache 2.mod_deflate
Module. By default, Apache is not enabled for both Windows and Linux/Unix. This module is not even available in Linux/Unix. You need to compile this module manually.
The following describes how to enable and configure Windows and Linux operating systems.mod_deflate
Module.
In Windows, the Apache server installed with the installer already has the modules required by deflate.mod_deflate.so
Andmod_headers.so
You only need to enable and configure the httpd. conf configuration file as follows:
LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so <Location /> # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48 # the above regex won't work. You can use the following # workaround to get the desired effect: BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary </Location> |
If it is a Linux/Unix operating system, if you do not have the two modules required during compilation and installationmod_deflate
Andmod_headers
Compilation is a little troublesome. First, let's first take a look at how to compile the two modules during the compilation and installation of Apache. Please add two parameters when executing the configure program:
# ./configure --enable-deflate --enable-headers |
In this way, after compiling Apache, you can enable and configure it directly in httpd. conf.deflate
Module. The configuration method is the same as that on Windows.
If your Apache is already running and you do not want to recompile it again, you can choose to compile onlydeflate
Module filesmod_deflate.c
Andmod_headers.c
. These two files are located in{apache-src}/modules/filters/
Directory (where{apache-src}
Directory of the apache source file ). Use the following command to compile the two source files separately.
# {apache-bin}/apxs -i -a -c {apache-src}/modules/filters/mod_deflate.c # {apache-bin}/apxs –i –a –c {apache-src}/modules/filters/mod_headers.c |
Where{apache-bin}
For the bin directory under the Apache installation directory, configure the module directly in httpd. conf.
Most of the time you compile it separatelydeflate
The module may encounter a compilation error and the prompt is:
Cannot load /opt/apache/modules/mod_deflate.so into server: /opt/apache/modules/mod_deflate.so: undefined symbol: deflate
The solution is as follows:
Edit/usr/local/apache2/bin/apr-config
Change the value of LDFLAGS to "-lz" in the file and re-compile the file.mod_deflate
Module,apxs -ica mod_deflate.c
You can.
To save unnecessary trouble, add--enable-deflate --enable-headers
Parameters.
IIS server
Microsoft's IIS server is also one of the most widely used WEB servers, and it is also essential to run ASP pages. IIS6 itself supports gzip compression, IIS5 is more difficult, you can find some third-party components to deal with, such as httpzip, URL is: http://www.port80software.com/products/httpzip? Vid = 3354166, but it is billed. Next we will introduce how to enable the compression function in IIS6.
Open the Internet Information Service (IIS) manager, right-click "website"-> "properties", and select "service ". In the "HTTP compression" box, select "compressing Application Files" and "compressing static files", and set "temporary directory" and "maximum temporary directory limit" as needed, as shown in:
Figure 1 Set website attributes
Next, configure the gzip component. In the Internet Information Service (IIS) manager, click "Web Service extension"-> "to add a new Web Service extension... ", enter the extension" HTTP Compression "in the" New Web service extension "box, and add" required file "to C: \ WINDOWS \ system32 \ inetsrv \ gzip. dll, and select "set extension status to allow", as shown in:
Figure 2 set Web service extension
Figure 3 new Web Service Extension
Before modification, we need to modify a configuration file. Before modification, stop the IIS service and Open C: \ Windows \ System32 \ inetsrv \ MetaBase. xml. The file is large. Find the following information:
<IIsCompressionSchemeLocation ="/LM/W3SVC/Filters/Compression/gzip" HcCompressionDll="%windir%\system32\inetsrv\gzip.dll" HcCreateFlags="1" HcDoDynamicCompression="TRUE" HcDoOnDemandCompression="TRUE" HcDoStaticCompression="TRUE" HcDynamicCompressionLevel="0" HcFileExtensions="htm html txt" HcOnDemandCompLevel="10" HcPriority="1" HcScriptFileExtensions="ASP dll exe" > </IIsCompressionScheme> |
Add some file suffixes to be compressed. HcFileExtensions is the extension of static files, and js and css are added. HcScriptFileExtensions is the extension of dynamic files, and ASPx is added. After saving the files, start IIS to take effect.
Finally, we will introduce how to test whether the previous work is effective. You may find it strange that the configuration is complete. Open the page in a browser and check the page source code. The content has not changed, the size is the same as the original one. Why? This is because the browser has extracted the content and has two methods to determine whether the compression takes effect: first, view the WEB server logs, whether Apache or IIS, the two access log formats are similar to the following:
127.0.0.1 - - [14/May/2006:08:44:28 +0800] "GET /manual/style/css/manual.css HTTP/1.1" 200 19351
The last two digits are the HTTP result code (200 indicates OK), and 19351 indicates the size of the response content, compare the size of the source code with the size of the source code in the browser to check whether the Code takes effect. Another method is to write an HTTP client Applet and set the value of Accept-Encoding to gzip and deflate. This program requests a URL address on the server, then print the response content. If there are a bunch of garbled characters, congratulations, the configuration is successful. The following is a test client code written in Java (the commons-httpclient package is required ):
HttpClient http = new HttpClient(); String url = "http://www.dlog.cn/javayou"; GetMethod get = new GetMethod(url); try{ System.out.println("fetching url : "+ url); get.addRequestHeader("accept-encoding", "gzip,deflate"); int er = http.executeMethod(get); if(er==200){ System.out.println(get.getResponseContentLength()); String html = get.getResponseBodyAsString(); System.out.println(html); System.out.println(html.getBytes().length); } }finally{ get.releaseConnection(); } |
Conclusion
The above are two popular WEB server software and Tomcat server page compression configuration methods. If some other J2EE application servers do not support this function, you can consider using the Filter (Servlet Filter) for specific code and configuration methods, refer to the document provided by the Resin server. However, it is important to remind readers that the access described in this article is only carried out when the server's response speed is sufficiently optimized, that is to say, this solution is considered only when bandwidth becomes a bottleneck of the system.
Source: http://Jorkin.Reallydo.Com/default.asp? Id = 490