Preface
To understand the connection timeout problem between the browser and Apache, you need to understand the HTTP Keep-alive property first. First, a brief introduction to Keep-alive, you can find more detailed information from the Internet.
Both browsers and Apache are based on HTTP protocols. The common explanation for the keep-alive attribute in the HTTP protocol is that the browser and Apache first set up a TCP connection, the data will not immediately disconnect the TCP connection, but continue to wait for the next request. The connection will not be disconnected until a period of time (Keep-alive-time) is maintained.
Here's a test to see the TCP connection status of Apache when you turn on keep-alive support and turn off keep-alive support.
Service side |
CentOS on a virtual machine |
Client |
IE6 Browser on this machine |
Service-side Address |
192.168.212.128 |
Client Address |
192.168.212.1 |
Files accessed by test.html |
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <link rel= "stylesheet" type= "Text/css" href= "/main.css"./> <script type= "Text/javascript" src= "/main.js" ></script> <body> Do you know. The color value at a and B is the same. <br/> </body>
|
First turn off the Apache keep-alive parameter and open httpd.conf.
Open the browser to access Apache. Use the netstat command to view connection status.
#netstat –nt|grep–i ' 80′
You can see four connections, because local access is fast and can only crawl to the TIME_WAIT state. Why would a test.html Web page have four connections?
Look at the contents of test.html can be known:
1,MAIN.CSS file
2,mian.js file
3,main.jpg Pictures
4, the test.html file itself
So there are four connections.
Take a look at the connection status after turning off Apache's keep-alive support.
Restart the server, the browser accesses test.html, and the connection is viewed.
#service httpd Restart
#netstat –nt|grep–i ' 80′
You can see that there is only one connection. And this connection state is established. We set the keepallivetimeout=15 in the httpd.conf, so the connection is not closed until 15 seconds after the connection is established.
the conclusion of the Test
If you turn off Apache's keep-alive attribute, all files, including js,css, pictures, and so on, in the page visited (test.html in the previous example) are to establish a new TCP connection. How many connections are established with the number of reference files. The specific number of files can be viewed using the Firefox bug tool.
The bottom 11 requests in the previous illustration are the number of files that need to be referenced in the page.
If you turn on the Apache Keep-alive property, all files, including js,css, pictures, and so on in the page (test.html in the example above) are accessed, and all of the data is transmitted sequentially, in the form of a TCP connection. All data transfer is completed wait KeepAliveTimeout = 15 seconds before closing the connection.
See the online reference:
If the current Apache response to 100 users per second access, keepalivetimeout=5, this time the number of httpd process is 100*5=500 (prefork mode), a httpd process consumes 5 m memory, is 500*5m=2500m= 2.5G, exaggerate it. Of course, Apache has only 100 TCP connections with the client. If your memory is big enough, the system load is not too high, if your memory is less than 2.5G, you will use swap, frequent swap will aggravate the CPU load.
Now we turn off the Keepalive,apache. Still respond to 100 users per second access, because we will picture, JS, CSS, etc. separated out, each visit only 1 request, at this time the number of httpd process is 100*1=100, using memory 100*5m= 500M, at this time Apache and client also carried out 100 times TCP connection. Performance has increased too much.
connection timeout for browser
Each browser has a default connection timeout. The default time for IE6 is 60 minutes.
This value can be modified by the registry.
1, open the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings.
2, add a DWORD value of the item, named ReceiveTimeout, set 1000. The default unit for this value is milliseconds, where you set the 1 second time.
Start the Web site from the browser, and close the connection after 1 seconds. (The value is a bit extreme, but easy to show).
Restart the browser to access the Web site.
Service side |
CentOS on a virtual machine |
Client |
IE6 Browser on this machine |
Service-side Address |
192.168.212.128 |
Client Address |
192.168.212.1 |
Files accessed by index.php |
<?php echo Date (' H:i:s ', Time ()); Sleep (10); ?> |
You can see that the browser does not find the server, but access to the test.html is accessible.
Access to index.php display connection is unsuccessful. Because Sleep (10) in index.php is delayed by a function of 10 seconds. The IE6 connection timeout is 1 seconds. So the connection failed.
Access TEST.HML can connect successfully. Because it is to access the local server, the transmission speed is very fast, in the IE6 1 seconds timeout time has passed the entire data.
the conclusion of the Test
The default connection timeout for IE6 is 60 minutes. You can modify the value by receivetimeout the value in the registry.
Actual function: Use IE6 to upload a large file to the server, if upload time more than 60 minutes will disconnect.
This is why some sites have to specialize in the development of active plug-ins to achieve IE6 large file upload. This value is not actively modified by the user.
Apache Connection Timeout
Look at Apache's configuration file to see a timeout value.
Someone would think that this is Apache's connection timeout parameter.
We set it to Timeout = 1 to access index.php.
See or can be accessed, then this timeout is not Apache's connection timeout. Timeout is the maximum value that Apache receives between the last request and the arrival of the subsequent request. You can look at the TCP connection state migration in the browser and Apache communication to more accurately understand the timeout value.
So what is Apache's connection timeout? What is the parameter control?
A: Apache has no maximum connection timeout and no parameters to control the connection timeout. Because Apache is the application layer in the TCP/IP model.
So what does the server control the maximum connection timeout between the browser and Apache?
Answer: Linux
the conclusion of the Test
Apache has no maximum connection timeout and no parameters to control the connection timeout. Because Apache is the application layer in the TCP/IP model.
connection timeout for Linux
These two parameters can be used in the Linux system configuration for connection time.
#sysctl-a|grep Time
One is to limit the timeout for the fin_wait state,
One is to limit the timeout for keepalive connections.
Conclusions
Linux's default configuration also does not control the browser and Apache connection timeout parameters, only through the Linux firewall to control the connection between Apache and browser maximum connection time.
operation timeout for PHP
Open php.ini to see two parameters.
Max_execution_time: The maximum time a PHP program executes.
Max_input_time: The maximum time a form is submitted.
These two values are important. Let's do a test:
Service side |
CentOS on a virtual machine |
Client |
IE6 Browser on this machine |
Service-side Address |
192.168.212.128 |
Client Address |
192.168.212.1 |
Files accessed by index.php |
<?php for ($i = 0;;; $i + +) { echo Date (' H:i:s ', Time ()); Echo ' <br/> '; Flush (); } ?> |
Access to index.php.
<?php
for ($i = 0;;; $i + +) {
echo Date (' H:i:s ', Time ());
Echo ' <br/> ';
Flush ();
}
?>
ie died in 30 seconds. Why, then? A: There are dead loops in the index.php. PHP stopped operation after execution to max_execution_time=30 seconds. The browser died here.
Summary
If you read through the above, you will come to the following conclusions:
1, at the client, the browser controls the maximum connection timeout for the browser and Apache.
2, in the service side (do not open the firewall), Linux and Apache can not control the maximum connection timeout, only PHP or MySQL and other running programs to control their own execution time to control the maximum connection between the browser and Apache timeout time.
3, on the server side (open firewall), Linux firewall and Php,mysql, and other common control browser and Apache maximum connection timeout.
4, the maximum connection timeout for the browser and Apache here includes a combination of all state timeout times in the TCP connection.