Keepalive configuration in Apache

Source: Internet
Author: User

 

Introduction

First, analyzeYslowTest the front-end performance of a page.

All requests here refer to HTTPRequest,Block the division of each stage of a request->Domain name resolution->Establish a connection->Send request->Wait for response->Receive data. Of course, not every request must contain all processes.

In the above test, there is another part in the process of requesting to download resources:TCPRequest link and disconnection, and this articleArticleOfficially speaking of this request. 

SoHTTPRequest andTCP What is the relationship between requests?? Simply put, it is TCP The request is closer to the underlying layer. HTTP Application requests such TCP There are many requests HTTP Request (as to the number of requests, Apache Can be set), at the same time TCP Connection to disconnection ratio HTTP The request link and disconnection require more memory resources and time consumption. .

 

 

Meaning of keepalive

 

Meaning of keepalive configuration: HTTP/1.1 The client will try to keep the customer's HTTP Connection. Multiple copies are transmitted through one connection. HTTP Request Response. In this way, the client can improve 50% Response time between the left and right, while the server reduces the overhead of more connections. However, this depends on whether the client wants to maintain the connection. IE By default, the connection is maintained. 100 When a website contains images, IE It is possible to only open 2 Data is transmitted through these two connections, instead 100 Connections.

InApacheServer,KeepaliveIs a Boolean value,OnIndicates opening,OffThis command is disabled.HttpdThe server exists.

 

KeepalivetimeoutIndicates the duration of persistent connection persistence. That is to say, the timer starts after the connection ends, and no re-transmission is performed for the specified time.HTTPRequest to disconnect the connection. The default value is5Seconds. This value can be larger, but cannot be too large. Otherwise, too many connections will be waiting at the same time, resulting in high memory usage.

 

 

The role of keepalive

 

How to balance network bandwidth with server resources. This should be analyzed according to the specific situation.

So let's consider3Case:
1. When a user browses a webpage, in addition to the webpage itself, multipleJavascriptFile, multipleCSSFile, multiple image files, and all these files are in the sameHTTPServer.
2. When a user browses a webpage, in addition to the webpage itselfJavascriptFile, an image file.
3. A user browses a dynamic web pageProgramGenerates content instantly without referencing other content.

for the preceding 3 medium, I think: 1 it is best to enable keepalive , 2 random, 3 it is best to disable keepalive


InApache, Open, and closeKeepaliveWhat are the similarities and differences between functions and servers?The following is a theoretical analysis.

Open Keepalive It means that every time the user completes all access, it will be closed after a certain period of time. TCP Before closing the connection, there must be Apache The process corresponds to this user and cannot process other users. Keepalive The timeout value is 10 Second, server processing per second 50 Independent user access, then the system Apache The total number of processes is 10*50 = 500 If a process occupies 4 m Memory, then the total consumption 2G Memory, so we can see that in this configuration, memory consumption is considerable, but the advantage is that the system only processes 50 Times TCP .

If you disable Keepalive , If it is still per second 50 If the number of consecutive requests is 3 , Then Apache The total number of processes is 50*3 = 150 If each process still occupies 4 m Memory, the total memory consumption is 600 m This configuration can save a lot of memory, but the system has processed 150 Times TCP The handshakes and closed operations of, therefore, consume more CPU Resources.

Let's take a look at the practice.

in a group of servers that process a large number of dynamic webpage content, keepalive function, it is often observed that a large user traffic volume apache there are also a large number of processes. The system frequently uses swap memory and the system is unstable, sometimes the load fluctuates greatly. keepalive the obvious changes are: apache reduces the number of processes and increases the idle memory, used for file systems cache memory is also increased. CPU the overhead is increased, but the service is more stable and the system load is also relatively stable, there are few situations where the load fluctuates in a large scope, and the load is reduced to a certain extent. What is not obvious is that the average load of the system does not change significantly when the traffic volume is small.

Summary:
On servers with ample memory, whether or not to disableKeepaliveFunction, server performance will not change significantly;
If the server memory is small, or the server has a large number of file systems to access, or the server mainly processes Dynamic Web Services, disableKeepaliveAnd saves a lot of memory for the file system.CacheTo improve the file system access performance, and the system will be more stable. 

 

 

Keepalive configuration file

 

If keepalive is set, it is time-consuming to find the setting file. Most of the configurations in earlier versions areHttpd. confFile, current version(2.4.2)OfApacheMany configurations are separated into different files, so I only search for one file.

... apache/CONF/extra/httpd-default.conf

# This configuration file reflects default settings for Apache HTTP server. # You may change these, but chances are that you may not need. ### Timeout: the number of seconds before between ES and sends time out. # timeout 60 # keepalive: whether or not to allow persistent connections (more than # One request per connection ). set to "off" to deactivate. # keepalive on # maxkeepaliverequests: the maximum number of requests to allow # during a persistent connection. set to 0 to allow an unlimited amount. # We recommend you leave this number high, for maximum performance. # maxkeepaliverequests 100 # keepalivetimeout: number of seconds to wait for the next request from the # same client on the same connection. # keepalivetimeout 5 # UseCanonicalName: determines how Apache constructs self-referencing # URLs and the SERVER_NAME and server_port variables. # When set "off", Apache will use the hostname and port supplied # by the client. when set "on", Apache will use the value of the # servername directive. # UseCanonicalName off # accessfilename: the name of the file to look for in each directory # For Additional configuration directives. see also the AllowOverride # directive. # accessfilename. htaccess ## ServerTokens # This Directive configures what you return as the server HTTP Response # header. the default is 'full' which sends information about the OS-type # and compiled in modules. # Set to one of: full | OS | minor | minimal | major | prod # where full conveys the most information, and prod the least. # ServerTokens full # optionally add a line containing the server version and virtual host # name to server-generated pages (internal error documents, FTP directory # listings, mod_status and mod_info output etc ., but not CGI generated # documents or custom error documents ). # Set to "email" to also include a mailto: Link to the serveradmin. # Set to one of: On | off | email # serversignature off # hostnamelookups: log the names of clients or just their IP addresses # e.g ., www.apache.org (on) or 204.62.129.132 (off ). # The default is off because it 'd be overall better for the net if people # Had to knowingly turn this feature on, since enabling it means that # each client request will result in at least one lookup request to the # nameserver. # hostnamelookups off # Set a timeout for how long the client may take to send the request header # and body. # The default for the headers is header = 20-40, minrate = 500, which means wait # For the first byte of headers for 20 seconds. if some data arrives, # increase the timeout corresponding to a data rate of 500 Bytes/s, but not # above 40 seconds. # The default for the request body is body = 20, minrate = 500, which is the same # but has no upper limit for the timeout. # To disable, set to header = 0 body = 0 #<  Ifmodule  Reqtimeout_module  >  Requestreadtimeout header = 20-40, minrate = 500 body = 20, minrate = 500  </  Ifmodule  > 

 

The comments section of the file has given the specific meaning of each parameter, so there is no need to explain them one by one here.

 

 
This article content, will be updated to the new blog: http://tt-topia.rhcloud.com/reprint Please note:

 

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.