Error message in Nginx: 502 Bad Gateway solution

Source: Internet
Author: User
Tags fpm


Php-cgi supported by php-fpm

Modify the php-fpm process count in/usr/local/php/etc/php-fpm.conf to find max_children and change the following value to the value you want to set.

.

The code is as follows: Copy code

# Vim/usr/local/php/etc/php-fpm.conf

<Value name = "max_children"> 4 </value>

# Max_children indicates the processing process of php-cgi. Setting max_children also needs to be set based on the server performance, increasing the number of processes and occupying memory.

It will also increase accordingly. Under normal circumstances, each php-cgi consumes about 20 MB of memory.

The code is as follows: Copy code

<Value name = "request_terminate_timeout"> 44 s </value>

<Value name = "process_control_timeout"> 44 s </value>

<Value name = "style"> apache-like </value>

# The default static processing method of php-fpm makes the php-cgi process occupy memory for a long time and cannot be released. This is also one of the causes of nginx errors.

Therefore, you can change the php-fpm processing method to the apache mode.

Nginx 502 Bad Gateway error trigger condition


Nginx 502 trigger conditions

The most common occurrence of 502 errors is that the backend host is used as the machine. In the upstream configuration, there is such a configuration: proxy_next_upstream, this configuration

It specifies the error that nginx will go to the next backend host when retrieving data from a backend host. It indicates that 502 of all the situations will occur.

The default value is error timeout. An error occurs when a machine or disconnection occurs, and a timeout is a read congestion timeout, which is easy to understand. I generally write all

On:

Proxy_next_upstream error timeout invalid_header http_500 http_503;

However, now I may want to remove the http_500 option. When http_500 specifies that the backend will return the 500 error, a host will be transferred. If the backend jsp fails

To print a bunch of stacktrace error messages, which are now replaced by 502. But programmers in the company do not think so. They think nginx has encountered an error.

I have no time to explain the 502 principle to them ......

503 error can be retained, because the backend is usually apache resin. If apache crashes, it is error, but resin crashes, it is only 503.

It is necessary to retain.

Solution

If you encounter a 502 problem, you can take the following two steps as a priority.

1. Check whether the current PHP FastCGI Process count is sufficient:

Netstat-anpo | grep "php-cgi" | wc-l

If the number of FastCGI processes actually used is close to the preset number of FastCGI processes, it indicates that the number of FastCGI processes is insufficient and needs to be increased.

.

2. If the execution time of some PHP programs exceeds the Nginx waiting time, you can add the FastCGI timeout time in the nginx. conf configuration file. For example:

For example:

The code is as follows: Copy code

......
Http
{
......
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
......
}
......

An error occurs when memory_limit is set to low in php. ini. The memory_limit of php. ini is changed to 64 MB, and nginx is restarted.

Insufficient storage.


According to the official answer, I checked the relevant possibilities and combined the answers from netizens to find the following solutions.


1. View the number of php fastcgi processes (max_children value)
Code: netstat-anpo | grep "php-cgi" | wc-l
5 (for example, 5)
2. View the current process
Code: top
Observe the number of fastcgi processes. If the number of processes used is equal to or higher than 5, increase the number (depending on the actual situation of your machine)
3, adjust/usr/local/php/etc/php-fpm.conf settings

The code is as follows: Copy code
<Value name = "max_children"> 10 </value>
<Value name = "request_terminate_timeout"> 60 s </value>

Max_children can have a maximum of 10 processes. Each process has 20 MB of memory and a maximum of 200 MB.
Request_terminate_timeout: the execution time is 60 seconds, that is, 1 minute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.