Web server performance and site access performance optimization ideas
To optimize the performance of your Web server, let's take a look at the steps of the Web server on Web page processing:
1. Web browser makes Web page request to a specific server;
2, the Web server receives the Web page request, looks for the requested Web page, and transmits the requested Web page to the Web browser;
3. The Web browser receives the requested Web page content and displays it.
The above three steps all relate to the Web server, but the actual Web server performance is most relevant in the 2nd step, where the Web server needs to look for the Web page content requested from the browser. We know that the content of the Web page is static, dynamic, static content, the Web server can directly send the results back to the browser, for dynamic content, you usually need to hand over to the application server, the application server returned results. Of course, there are WEB servers themselves that can handle dynamic content, such as IIS, which can self-interpret the two Microsoft Dynamic Web scripting Languages of ASP, ASP.
From the above brief analysis, we can generally get the conclusion that the impact of Web page access will have these factors:
1. The speed at which the WEB server reads static page content from disk, that is, time;
2, the WEB server determines whether the content of the request is static or dynamic content time;
3. When the WEB server forwards the request to the application server;
4. The time required for the application server to process (interpret) dynamic content;
5, the Web server returns the Web content to the browser response time;
6, the WEB server receives the processing performance from the browser request;
7, the time of Web Access request data transmission on the network: including from the browser to the server, and from the server to the browser two parts;
8. The time the browser calculates and renders Web content locally, that is, the time it takes to present content after receiving content.
The above 8 are easy to understand, but also very straightforward, in fact, the following are also related to the Web page access speed experience factors, you can think about it? or whether it affects page access performance.
The time, or performance, that the WEB server performs security policy checks;
The WEB server reads the log file, writes the log contents, closes the log file access time, reads and writes before closing, and the reading and writing in these three steps involves the disk access performance factors;
The number of clients that are concurrently connected to the WEB server session, that is, how much concurrent traffic is being accessed.
We can draw out a total of 11 factors above, and that is:
Web server disk performance;
The performance of the WEB server interacting with the application server;
The application server handles dynamic content performance, or dynamic content application processing performance;
The connection speed of the client and the Web server, that is, the network transmission performance;
Web browsers interpret and render the performance of Web content;
Web Access concurrency performance.
Reflected in our performance optimization, you can start with the angle is:
1, increase the bandwidth, including the server and the client side of the Internet connection bandwidth;
2, speed up the processing performance of dynamic content;
3, as much as possible to use static content, so that the Web server can not request the application server, directly to the Web content to the browser side, here can start the scenario is:
Dynamic Content Caching
Dynamic content Static
4, multi-server load Balancing processing a large number of concurrent access;
5, improve the server disk access performance, which is commonly referred to as I/O performance;
6, reduce the number of HTTP requests in the Web page;
7, replace the better performance of the WEB server;
8, reasonable deployment of servers, closer to the client to deploy the server, has proven to significantly improve access performance.
Performance optimization Practices
After a brief analysis of the preceding section, I believe you have some ideas for optimizing the Web server, you can optimize from the hardware level, software level, WEB code three levels. Below we combine a concrete example to practice once, the example of this article is a small Web site, part of the data system hypothesis, if there is similar, purely coincidental, only for the purpose of the use. In the actual work, if you encounter a large site, you can refer to the analysis here, modify the optimization scheme.
1. Site Introduction
A community forum site, using discuz! Forum program Construction, the program uses the mainstream PHP + MySQL composition.
The site currently has nearly 50,000 registered users, the vast majority of domestic users, the number of active users in about half, the average PV in 15~20 million per day, independent access to the number of IP in about 8000.
2. WEB Server Performance Optimization requirements
The Web site is now deployed in foreign servers, rental virtual host to operate, because the traffic is relatively large, so often receive a large number of virtual hosting service providers to inform, require control of access. In addition, the virtual host server in the United States, did not rent virtual host in the country is very cumbersome in the record, the site at the beginning of the operation when the data volume and access are relatively small, so the performance requirements are not high, the data volume is small, so the server in the query processing data faster, but also make people feel the speed is not slow , the speed of access has dropped dramatically as data volumes and traffic are rising, and it's time to improve accessibility.
Based on the current situation of the Community website, the proposed optimization needs is that the domestic access speed needs to be increased by one times, the current home loading time needs about 40 seconds, the hope is optimized to be within 20 seconds to complete the first page loading.
In addition, the site data can be automatically backed up once a day, backup data retained for one months, so that recovery at any time.
The above two requirements, where the first one is the performance optimization requirements, the second is the additional requirements.
3. Performance optimization Scheme
According to the status of its website and optimization needs, combined with their own experience, coupled with Google's search, and the site owners continue to confirm communication, and ultimately get the following performance optimization program:
Change from Virtual host deployment to standalone server deployment
The virtual host is more limited, unable to customize the Web server itself, unable to configure the PHP dynamic cache, and independent server can enjoy the memory, processor resources, no longer the virtual host to each virtual host user's memory and processor resource consumption limit. Processor resources and memory resources have a direct performance boost for accepting more concurrent access. Standalone server, we choose Linode 2048 model, 2G memory, 4-core processor (Linode All VPS are quad-core processor), 80G hard disk space, 800G network traffic.
Changed from Windows operating system to Linux operating system
Web site using PHP + MySQL program, PHP performance under Windows, limited by the IIS need to call through the ISAPI form of PHP, so performance is not as good as Linux under the PHP module directly to explain PHP, rather than Nginx and P HP-FPM performance, since the use of a standalone server, the operating system can also determine their own, the Linux system we chose the familiar Ubuntu Linux Server 10.04 (not a year ago 12.04), ^-^.
Web Server uses Nginx instead of Apache
Choose Nginx without Apache for the reason very direct and simply, because the site has a lot of static attachment files, in the processing of static content, Nginx performance is almost 10 times times the Apache. In the context of PHP interpretation and pseudo-static rules, Apache is stronger than Nginx, but this does not affect us to abandon it, in order to alleviate this, we later on the PHP dynamic cache.
Dynamic caching of PHP queries, using the Eaccelerator accelerator
PHP Accelerator is a to improve the efficiency of PHP, thereby caching PHP operation code, so that after PHP execution does not have to parse the conversion, you can directly invoke the PHP operation code, so the speed has been improved a lot. Eaccelerator is an open source PHP accelerator, optimized and dynamic content caching, which improves the caching performance of PHP scripts, allowing PHP scripts to be almost completely eliminated from the server in the compiled state. It also has the effect of optimizing the script to speed up its execution efficiency. PHP program code to improve the efficiency of 1-10 times, this acceleration is very obvious.
Specifically, we plan to optimize the following settings for Eaccelerator:
The cache uses physical memory for caching without using disk. We know that memory read and write performance is N times the hard disk, so in memory resources can be scheduled, it is strongly recommended to use memory to save Eaccelerator cache content.
The cache size is set to 32MB, which is the maximum cache capacity supported by the operating system by default. Although the configuration file can be modified to increase this value, but we feel that there is no need, so we gave up.
Nginx Performance Optimization
The use of Nginx, although its performance is very good, but we still need to optimize its performance, in this case, we made the following optimizations:
With 8 processes, each process requires approximately 20M of memory consumption, with a total of about 150M of memory.
Fully use the CPU core of the primary server: four cores, using the CPU stickiness configuration option, each core processor allocates two processes.
Turn on gzip compression: gzip compression on JS, CSS, XML compression effect is very good, can compress half, that is, reduce the transmission time by one times; the image file, JPG has been compressed, it has less compression performance.
Picture Local cache 1 days: A lot of pictures on the site, usually a picture uploaded, will not be frequently modified, only frequent access, so the picture in the Nginx cache, you can reduce the number of server access load, improve access speed.
JS, CSS file local cache 7 days: These two kinds of web files, usually do not modify it, cache it, can reduce the number of loading, improve access speed. Why these two files do not set the cache validity period together with the picture, is to consider the different files change frequency not the same.
Nginx Log Cutting Once a day: This optimization can greatly reduce the size of the Nginx log file, after a week of viewing, the daily log file is about 50M, if not line day cutting, with the month cutting, that one months of log file is a few G, to the WEB server in memory to load such a large file, The system itself is not enough memory, it will naturally use the disk to cache, which affects performance. About 50M per day, in memory can be fully loaded, so Nginx in processing access, you can quickly save access logs.
After the above several optimization projects, Nginx this side altogether need to occupy about 200M memory resources.
optimizing the performance of the PHP CGI process
Nginx does not have a PHP module, so its support for PHP is implemented through PHP-FPM, PHP-FPM is a running process to handle concurrent requests, in this case, we configured 20 processes, each process almost consumes about 20M of memory resources, is about 400M. At the same time, PHP-FPM and Nginx Interactive mechanism, choose the Linux Socket mode instead of TCP protocol port, Socks is a system-level processing mode, Socks is a file connection, and TCP protocol port, need to undergo network protocol processing, performance is inferior to the former, so we chose Former.
MySQL Database Performance Optimization
Because the main program is to choose others to develop open source programs, so the database query program optimization We can not handle, only from the MySQL itself to find a breakthrough.
We can imagine that, for the forum website, usually look at the paste, check the volume of the visit is much larger than the creation of posts, reply to the number of posts, reflected in the MySQL database, that is, read the table and query table data connection processing more. So we have to choose to read the table, query performance better storage engine, combined with the knowledge previously understood, MySQL default MyISAM engine is designed to handle read frequency much more than write frequency of the environment, the query efficiency is very impressive, and memory footprint, which is also in line with our rental low memory configuration VPS.
Specific to the optimization of MySQL configuration parameters, limited by the memory resources on the server itself is limited, the default medium-sized environment configuration file is used directly.
Content Distribution Network Applications
Site daily more than 100,000 access, tens of thousands of independent IP access, view the previous access statistics, access from various regions in the country, using a variety of network connections to access, in order to ensure that users from the network access speed, but also reduce the site server requests, we use a CDN to distribute static content, So that users around the local access to the files that have been cached on the CDN, Cdn Service chamber in the static content on the first visit to their servers across the country, when the second visit, the user is actually not connected to the site server to obtain the files, but directly from the CDN server to obtain, Can significantly improve the performance of the site.
Resources
Here are recommended several books on Web performance optimization, can be found to read, especially the "build high-performance Web site," The book is very worthy of the server maintenance engineer read, it is a server engineer for the performance of the book, a more systematic explanation of the optimization ideas, And there are many examples of practical operations.
Build a high-performance web site
High-performance Linux server Building combat
Linux Server Setup, performance tuning, cluster management tutorials
Linux Server Performance Tuning
This is the end! Thank you
This article from "Du Haiqiang" blog, declined reprint!
Web Server Performance Optimization