Analyze why access to Linux Apache is too slow

Source: Internet
Author: User

I am busy learning Linux, and I will always encounter some problems during my study. However, after I solved these problems, I learned a lot from them. During this period, I solved such a Linux Apache problem. Cause: an online server has encountered slow access recently. It takes more than two seconds to click a link. According to our estimation of the number of visitors, the server should not respond so slowly, so it is necessary to analyze the Linux Apache to solve the slow Website access.

Analysis:
1. First, when the page access slows down, use the top command to view the server load and find that the load is not high. It is estimated that it is not a program problem.

2. Check the number of httpd in the thread. ps-aux | grep httpd | wc-l shows that the number of threads has reached the maximum value set by apache. Therefore, it is determined that too many Website Visitors cause slow access.

3. for verification, check the number of connections and the current number of connections, which are
Netstat-ant | grep $ ip: 80 | wc-l
Netstat-ant | grep $ ip: 80 | grep EST | wc-l
We found that the number of connections was very large, far exceeding our estimate.

4. At the beginning, I was not very familiar with the MPM configuration method of the server. I think modifying the server configuration can solve the problem. The main configuration part includes the prefork mode or work mode configuration. The following is a brief introduction.
Prefork mode:
Default Configuration of apache working in prefork mode:
<IfModule mpm_prefork_module>
ServerLimit 2000
StartServers 5 # specify the number of sub-processes created at server startup
MinSpareServers5 # specify the minimum number of idle sub-Processes
MaxSpareServers 10 # specify the maximum number of idle sub-Processes
MaxClients 150 # specify the maximum number of concurrent threads for a single process to access the client at the same time). Any requests that exceed this limit will enter the waiting queue. Once a connection is released, requests in the queue will receive services
MaxRequestsPerChild0 # specify the maximum number of requests allowed by each sub-process in its lifecycle. The default value is "fail". 0 indicates that the sub-process will never end.
</IfModule>

After the "StartServers" sub-process is initially created, the prefork control process creates a process to meet the needs of the MinSpareServers settings. Wait for one second to create two more processes, and then wait for one second to create four more processes ...... In this way, the number of Created processes is increased exponentially, up to 32 processes per second until the value set by MinSpareServers is met. This mode eliminates the need to generate new processes when requests arrive, thus reducing system overhead and increasing performance.
MaxSpareServers sets the maximum number of idle processes. If the number of idle processes is greater than this value, Apache automatically kill unnecessary processes. This value should not be too large, but if it is smaller than MinSpareServers, Apache will automatically adjust it to MinSpareServers + 1. If the site load is large, consider increasing MinSpareServers and MaxSpareServers at the same time.

MaxClients is the most important of these commands. It sets the request that Apache can process at the same time and is the parameter that has the greatest impact on Apache performance. The default value of 150 is far from enough. If the total number of requests reaches this value, you can confirm it through ps-ef | grep httpd | wc-l), then the subsequent requests will be queued, until a processed request is completed. This is the main reason why there are still a lot of system resources and HTTP access is slow. In theory, the larger the value, the more requests can be processed, but the default limit of Apache cannot be greater than 256. In apache2, you can use the ServerLimit command to increase MaxClients without re-compiling Apache.

Although MaxClients can be greatly increased by setting ServerLimit, it is often counterproductive and the system consumes all the memory. Take a server as an example: memory 2 GB, each apache process consumes about 0.5% of the memory, which can be confirmed by ps aux), that is, 10 MB, theoretically, this server consumes all the system memory to run a maximum of 200 apache processes. Therefore, set MaxClients with caution.
Worker mode:
The default configuration of apache working in worker mode is as follows:
<IfModule mpm_worker_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild0
</IfModule>

A Worker generates a "StartServers" sub-process by the main control process. Each sub-process contains a fixed number of ThreadsPerChild threads, and each thread processes Requests independently. Similarly, in order not to generate a thread when the request arrives,
MinSpareThreads and MaxSpareThreads set the minimum and maximum number of Idle threads, while MaxClients sets the maximum number of clients simultaneously connected. If the total number of threads in the existing sub-process cannot meet the load, the control process will derive a new sub-process.
The maximum default values of MinSpareThreads and MaxSpareThreads are 75 and 250, respectively. These two parameters have little impact on Apache performance and can be adjusted as needed.
ThreadsPerChild is the most performance-related command in worker MPM.
The maximum default value of ThreadsPerChild is 64. If the load is large, 64 is not enough. The ThreadLimit command must be explicitly used. The maximum default value is 20000.

In Worker mode, the total number of requests that can be processed simultaneously is determined by the total number of sub-processes multiplied by the value of ThreadsPerChild. The value must be greater than or equal to MaxClients. If the load is large and the number of existing sub-processes cannot meet the requirements, the control process will derive a new sub-process. By default, the maximum number of sub-processes is 16. When you increase the number, you must explicitly declare that the maximum ServerLimit value is 20000 ). Note that if ServerLimit is explicitly declared, the value multiplied by ThreadsPerChild must be greater than or equal to MaxClients, and MaxClients must be an integer multiple of ThreadsPerChild, otherwise, Apache automatically adjusts to a corresponding value.
The apache server adopts the prefork working mode and makes corresponding adjustments to MaxClients. It is found that the number of connections can reach the maximum in a short time after the service is started.

5. Later, I thought about the pages that users visit. I opened the access_log in the configuration and found that more than 85% of the access requests were direct access to the resource files, users may use multi-threaded download tools, or these resources may be leeching.

6. Locate the problem and solve it. Two methods are taken into account:

A. Thread restrictions on connection to A single IP address. multi-thread connection to resources is not allowed.
For IP address restrictions, I use the mod_limitipconn module. The advantage of this module is that it is relatively simple, but it cannot be set for individual folders or files and does not support virtual hosts.
After this module is installed in apache, the following sections can be added to the configuration file:
ExtendedStatus On
<IfModule mod_limitipconn.c>
<Location/> # all virtual hosts/directory
MaxConnPerIP 3 # Each IP address only allows three concurrent connections
NoIPLimit image/* # No IP address limit on Images
</Location>
<Location/mp3> # All Hosts/mp3 Directories
MaxConnPerIP 1 # Each IP allows only one connection request
OnlyIPLimit audio/mpeg video # This restriction only applies to video and audio files.
</Location>
</IfModule>

B. Add URL rewriting to prevent leeching.
One important way to prevent leeching is to determine the refer of the request. However, if you use some browsers to remove or disguise the refer when sending a request, this method will be powerless. However, it seems that there are more advanced methods to implement this function.
After installing the mod_rewrite module of apache, add
RewriteEngine On
RewriteCond % {HTTP_REFERER }! ^ Http://abc.com/.#$ [NC]
RewriteCond % {HTTP_REFERER }! ^ Http://abc.com $ [NC]
RewriteCond % {HTTP_REFERER }! ^ Http://www.abc.com/.#$ [NC]
RewriteCond % {HTTP_REFERER }! ^ Http://www.abc.com $ [NC]
RewriteRule. * \. (gif | jpg | swf) $ http://www.abc.com/about/nolink.png [R, NC]
In this way, requests for leeching will be redirected to an error page, reducing the load on the server from downloading.

You are not afraid of encountering difficulties in your study. You must dare to solve the difficulties before you can become mature and learn knowledge. Have you learned about Linux Apache from your literature?

  1. Learning Manual: Install ADSL in SUSE Linux
  2. How to install OpenFOAM in Open SUSE
  3. How to use the Open Suse printer sharing settings
  4. Operation notes: suse faq modify XWindows resolution
  5. Knowledge about Suse Chinese Encoding

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.