Home  >  Article  >  Operation and Maintenance  >  How php connects to apache with timeout

How php connects to apache with timeout

步履不停
步履不停Original
2019-06-29 13:37:173542browse

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.

##Client address192.168 .212.1The accessed file test.html
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<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>  
</head>  
<body>  
您知道吗?A处和B处的色值是一样的。<br/>  
<img src="./main.jpg">  
</body>  
</html>
##First turn off the keep-alive parameter of apache , open httpd.conf.
Server Centos on the virtual machine
Client This machine On IE6 browser
Server address 192.168.212.128

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 test

If 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.

ServerClientServer address##Client address192.168 .212.1Accessed file index.php
<?php  
echo date(&#39;H:i:s&#39;,time());  
sleep(10);  
?>

可以看到浏览器显示找不到服务器,但是访问刚才的test.html是可以访问的。

访问index.php显示连接不成功。因为index.php中sleep(10)延迟10秒的函数。而IE6的连接超时时间为1秒。所以就连接失败了。

访问test.hml可以成功连接。因为是访问本地服务器,传输速度很快,在IE6的1秒超时时间之内就已经传完全部数据了。

测试得到的结论

IE6的默认连接超时时间为60分。可以通过注册表中ReceiveTimeout值修改该值。

实际作用:使用IE6往服务器上传一个大文件,如果上传时间超过60分钟就会断开连接。

这也是为什么有些网站要专门开发active插件来实现IE6的大文件上传了。用户是不会主动修改这个值的。

apache的连接超时

看apache的配置文件可以看到有个timeout值。

有人会以为这个是apache的连接超时参数。

我们把它设置为timeout =1访问index.php。

看到还是可以访问的,那么这个timeout不是apache的连接超时时间。timeout是apache收到上一个请求和后面一个请求到来之间的最大值。您可以查看浏览器与apache通讯中的TCP连接状态迁移更加准确的明白timeout的值。

那么apache的连接超时时间到底是多少?是什么参数控制呢?

答:apache没有最大连接超时时间,也没有控制连接超时的参数。因为apache是在TCP/IP模型的应用层。

那么服务端是什么控制了浏览器和apache之间的最大连接超时时间呢?

答:linux

测试得到的结论

apache没有最大连接超时时间,也没有控制连接超时的参数。因为apache是在TCP/IP模型的应用层。

linux的连接超时

在linux的系统配置中可以到关于连接时间的有这两个参数。

#sysctl -a|grep time

一个是限制FIN_WAIT状态的超时时间,

一个是限制keepalive连接的超时时间。

结论

linux的默认配置下也没控制浏览器和apache连接超时的参数,只有通过linux的防火墙才能控制apache和浏览器之间连接的最大连接时间。

PHP的操作超时

打开php.ini可以看到两个参数。

max_execution_time:一个php程序执行的最长时间。

max_input_time:一个表单提交的最长时间。

这两个值很重要。我们做个测试:

Centos on the virtual machine
This machine On IE6 browser
192.168.212.128
服务端 虚拟机上的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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn