search
HomeOperation and MaintenanceApacheHow php connects to apache with timeout

How php connects to apache with timeout

Jun 29, 2019 pm 01:37 PM
apachephpConnection timed out

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
  
  
<meta>  
<link>  
<script></script>  
  
  
您知道吗?A处和B处的色值是一样的。<br>  
<img  src="/static/imghwm/default1.png" data-src="./main.jpg" class="lazy" alt="How php connects to apache with timeout" >  
  
##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(&#39;H:i:s&#39;,time());  
 echo &#39;<br/>';  
 flush();  
}  
?>
max_execution_time 30

访问index.php。

<?php   
for($i = 0;;$i++){  
 echo date(&#39;H:i:s&#39;,time());  
 echo &#39;<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
Apache's Role: Serving HTML, CSS, JavaScript, and MoreApache's Role: Serving HTML, CSS, JavaScript, and MoreApr 19, 2025 am 12:09 AM

Apache can serve HTML, CSS, JavaScript and other files. 1) Configure the virtual host and document root directory, 2) receive, process and return requests, 3) use .htaccess files to implement URL rewrite, 4) debug by checking permissions, viewing logs and testing configurations, 5) enable cache, compressing files, and adjusting KeepAlive settings to optimize performance.

What Apache is Known For: Key Features and AchievementsWhat Apache is Known For: Key Features and AchievementsApr 18, 2025 am 12:03 AM

ApacheHTTPServer has become a leader in the field of web servers for its modular design, high scalability, security and performance optimization. 1. Modular design supports various protocols and functions by loading different modules. 2. Highly scalable to adapt to the needs of small to large applications. 3. Security protects the website through mod_security and multiple authentication mechanisms. 4. Performance optimization improves loading speed through data compression and caching.

The Enduring Relevance of Apache: Examining Its Current StatusThe Enduring Relevance of Apache: Examining Its Current StatusApr 17, 2025 am 12:06 AM

ApacheHTTPServer remains important in modern web environments because of its stability, scalability and rich ecosystem. 1) Stability and reliability make it suitable for high availability environments. 2) A wide ecosystem provides rich modules and extensions. 3) Easy to configure and manage, and can be quickly started even for beginners.

Apache's Popularity: Reasons for Its SuccessApache's Popularity: Reasons for Its SuccessApr 16, 2025 am 12:05 AM

The reasons for Apache's success include: 1) strong open source community support, 2) flexibility and scalability, 3) stability and reliability, and 4) a wide range of application scenarios. Through community technical support and sharing, Apache provides flexible modular design and configuration options, ensuring its adaptability and stability under a variety of needs, and is widely used in different scenarios from personal blogs to large corporate websites.

Apache's Legacy: What Made It Famous?Apache's Legacy: What Made It Famous?Apr 15, 2025 am 12:19 AM

Apachebecamefamousduetoitsopen-sourcenature,modulardesign,andstrongcommunitysupport.1)Itsopen-sourcemodelandpermissiveApacheLicenseencouragedwidespreadadoption.2)Themodulararchitectureallowedforextensivecustomizationandadaptability.3)Avibrantcommunit

The Advantages of Apache: Performance and FlexibilityThe Advantages of Apache: Performance and FlexibilityApr 14, 2025 am 12:08 AM

Apache's performance and flexibility make it stand out in a web server. 1) Performance advantages are reflected in efficient processing and scalability, which are implemented through multi-process and multi-threaded models. 2) Flexibility stems from the flexibility of modular design and configuration, allowing modules to be loaded and server behavior adjusted according to requirements.

What to do if the apache80 port is occupiedWhat to do if the apache80 port is occupiedApr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to solve the problem that apache cannot be startedHow to solve the problem that apache cannot be startedApr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools