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(); } ?> Copy after login Copy after login |
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
