For one of our web projects, due to the increase in new cities, the number of visits has increased and the pressure on the db has increased. As a business party that provides interfaces, a large number of "502" requests have been reported recently by downstream feedback.
502, bad gateway, is usually an error in upstream (here is PHP). For PHP, the common cause of 502 is that the script execution exceeds the timeout setting time, or the timeout setting is too large, causing the PHP process to take a long time It cannot be released and there are no idle worker processes to pick up guests.
Our project is caused by the PHP execution time setting being too short. In this case, you can first increase the PHP execution time appropriately and ensure that 502 is cleared first. Optimization will take more time after all.
There are two options to control php execution time, max_execution_time in php.ini and request_terminate_timeout in php-fpm. request_terminate_timeout can override max_execution_time, so if you don’t want to change the global php.ini, just change php- The configuration of fpm is enough.
Next I will analyze in detail why the execution of the php script exceeds the set time and causes nginx to return 502.
Let’s set the scene first and let the problem reoccur:
nginx and php only start one worker each for easy tracking.
php-fpm's request_terminate_timeout is set to 3s.
Test script test.php
sleep(20); echo 'ok';
go go go:
Visit www.v.com/test.php in the browser, and 404 will appear as expected after 3 seconds. ? ? ? what? ? ?
It’s a bad start, quickly take a look at the nginx configuration file
This location configuration is when a 5xx error occurs Jump to a nice-looking interface, but I don’t have the file 50x.html under /usr/share/nginx/html. So I got a 404. Doesn't this affect the accuracy of my judgment of the problem? Just comment it out! Visit again, wait 3 seconds, and finally the 'normal' interface comes out.
The environment is good, let’s follow the routine below. Follow the troubleshooting routine for web problems. Let’s take a look at the error log first:
nginx:
The errors reported are recv() failed (104: connection reset by peer.
recv failed and the connection was reset. Why was the connection reset? Set it? Isn’t it consistent?
We are looking at the error log of php-fpm:
(Note that the php_admin_value[error_log] option in php-fpm specifies the error log of php, which will be overwritten in php.ini. But here we are not looking at php errors, but at php-fpm errors. The error log of php-fpm is specified by the error_log option in php-fpm.conf.)
Each request generates 2 warnings and 1 notice:
warning: The script execution timed out and terminated.
warning: The child process received sigterm The signal exited.
Notice: A new child process was started (because I set pm.min_spare_servers = 1)
It seems that if the worker process of php times out, it will not only terminate the script execution , and the worker process will also exit. It seems that the nginx error connection is reset because the php worker process exits (in the tcp connection, if one party is disconnected, it will send rst to the other party)
Through the log We can already know that the execution of the php script times out and the worker sub-process exits, causing nginx to report an error connection reset by peer. Let’s use strace to see the situation of php and nginx:
php:
1.Accept an nginx connection request (socket, bind, and listen are all completed in the master). You can see that the port of nginx is 47039, and the data is read from fd0, which is from the standard input. This is stipulated by the fast-cgi protocol. The connected descriptor after accept is 3.
2. Read the data passed by nginx from fd3, in the fastcgi protocol format, and received 856 bytes. Why read5 What about times?
Because the fastcgi protocol data packet is 8-byte aligned and consists of a packet header and a packet body. And it will first send a request packet, including some request id, version, type and other information (the header and body each occupy 8 bytes), and then send a params packet to pass the get parameters and environment variables (the header is 8 bytes) , the packet body becomes longer), and finally a params data packet without a packet body and only a packet header is sent, indicating the end of parameter sending (8 bytes of packet header). So the first three reads are used to read the header and body of the request packet, as well as the header of the params packet. The fourth read is to read the real data, and the last read is to read the header of the last params packet. Therefore, the data transmitted by nginx should be 8 8 8 856 8 = 896 bytes (which can correspond to the transmission bytes of nginx below). Note that if it is post mode, stdin data packets will also be sent.
3. Set sleep for 20s, which is sleep(20) in the php program. After that, because the process is terminated, there will be no more. The strace program also exited.
nginx:
The above is the detailed content of How to solve nginx+php-fpm service HTTP status code 502. For more information, please follow other related articles on the PHP Chinese website!

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version