Home > Article > Operation and Maintenance > How php connects to apache with timeout
Preface
To understand the connection timeout problem between the browser and apache, you need to first understand the keep of http -alive attribute. Let’s briefly introduce keep-alive first. You can find a more detailed introduction online.
The browser and apache are both based on the http protocol. The popular explanation of the keep-alive attribute in the http protocol is that the browser and apache establish a TCP connection for the first time. After the data is transmitted, the TCP connection will not be disconnected immediately, but will continue to wait for the next request. The connection will be disconnected after a period of time (keep-alive-time).
Let’s do a test to check the TCP connection status of apache when it turns on keep-alive support and turns off keep-alive support.
Server | Centos on the virtual machine |
Client | This machine On IE6 browser |
Server address | 192.168.212.128 |
192.168 .212.1 | |
Open the browser to access apache. Use the netstat command to check the connection status.
#netstat –nt|grep –i ’80′
You can see four connections. Because the local access speed is very fast, only the TIME_WAIT status can be captured. Why does the test.html web page have four links?
Looking at the content of test.html, we can know that there are:
1, main.css file
2, main.js file
3, main. jpg picture
4, its own test.html file
so there are four links.
Let’s take a look at the connection status after turning off apache’s keep-alive support.
Restart the server, access test.html with the browser, and check the connection.
#service httpd restart
#netstat –nt|grep –i ’80′
You can see that there is only one connection. And the connection status is ESTABLISHED. We set keepAlliveTimeout=15 in httpd.conf, so the connection is closed after 15 seconds after the connection is established.
Conclusions from the testIf you turn off the keep-alive attribute of apache, all files in the accessed page (test.html in the above example), including js, css, pictures, etc. must establish a new TCP connection. There are as many connections as there are referenced files. The specific number of files can be viewed using Firefox's BUG tool.
The bottom 11 requests in the above picture are the number of files that need to be referenced in the web page.
If you enable the keep-alive attribute of apache, all files in the accessed page (test.html in the above example), including js, css, images, etc., will only establish a TCP connection and transfer all files in order. data. Wait for KeepAliveTimeout =15 seconds after all data is transmitted before closing the connection.
References seen on the Internet:If Apache currently responds to 100 user visits per second, KeepAliveTimeOut=5, then the number of httpd processes is 100* 5=500 (prefork mode). If an httpd process consumes 5M of memory, it is 500*5M=2500M=2.5G. Is it an exaggeration? Of course, Apache and Client only made 100 TCP connections. If your memory is large enough, the system load will not be too high. If your memory is less than 2.5G, Swap will be used. Frequent Swap switching will increase the load on the CPU.
Now we turn off KeepAlive, Apache still responds to 100 user visits per second, because we have separated images, js, css, etc., and there is only one request per visit. This At that time, the number of httpd processes was 100*1=100, and the memory used was 100*5M=500M. At this time, Apache and Client also made 100 TCP connections. The performance has improved a lot.
Browser connection timeout
Each browser has a default connection timeout. The default time for IE6 is 60 minutes.
This value can be modified through the registry.
1. Open the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings.
2. Add an item with a DWORD value, name it ReceiveTimeout, and set it to 1000. The default unit of this value is milliseconds, and the time set here is 1 second.
Start when the browser starts accessing the website, and close the connection after 1 second. (The set value is a bit extreme, but it is convenient for display).
Restart the browser to access the website.
Centos on the virtual machine | |||||||||||||
This machine On IE6 browser | |||||||||||||
192.168.212.128 | ##Client address | ||||||||||||
Accessed file index.php | |||||||||||||
服务端 | 虚拟机上的Centos |
客户端 | 本机上IE6浏览器 |
服务端地址 | 192.168.212.128 |
客户端地址 | 192.168.212.1 |
访问的文件index.php | <?php for($i = 0;;$i++){ echo date('H:i:s',time()); echo '<br/>'; flush(); } ?> |
max_execution_time | 30 |
访问index.php。
<?php for($i = 0;;$i++){ echo date('H:i:s',time()); echo '<br/>'; flush(); } ?>
30秒后IE死掉了。为什么呢?答:index.php中有死循环。执行到max_execution_time=30秒后php停止了操作。浏览器这边死掉了。
总结
如果从头到尾看完上面的内容,会得出如下结论:
1,在客户端,浏览器控制着浏览器和apache的最大连接超时时间。
2,在服务端(不打开防火墙),linux和apache都不能控制最大连接超时时间,只有php或者mysql等运行程序通过控制自身的执行时间来控制浏览器和apache的最大连接超时时间。
3,在服务端(打开防火墙),linux上的防火墙和php,mysql等共同控制浏览器和apache的最大连接超时时间。
4,这里的浏览器和apache的最大连接超时时间包括TCP连接中的所有状态超时时间的综合。
更多Apache的相关技术文章,请访问Apache教程栏目进行学习!
The above is the detailed content of How php connects to apache with timeout. For more information, please follow other related articles on the PHP Chinese website!