Solution to nginx 502 Bad Gateway error

Source: Internet
Author: User
Tags vps
Today, my VPs frequently prompts that the nginx 502 Bad Gateway is incorrect. After the VPs is restarted, it becomes annoying. I cannot figure it out. There was no problem when my website reached 1290 of the traffic in the previous two days. Why did 502 Bad Gateway appear this time? Depressed !!! After searching for a long time, I finally found a lot of relevant answers and hoped that this error would not occur again after modification. Alas, since I have been searching for answers online for so long, I have to record useful things so that I won't go to Google next time ~ I used the one-click installation package lnmp. If something goes wrong, I must search for it on the official forum. That's a great deal. The official website has such a top post. Let's take a look. Lnmp one-click installation package official: The first reason is that the lnmp one-click installation package has many problems, namely 502 Bad Gateway. In most cases, some lib packages in the script may not be installed before PHP is installed, as a result, PHP is not compiled and installed successfully. Solution: You can try to manually install the tool based on the script in the lnmp one-click installation package to see what caused the error. The second reason is:
In PHP. in INI, The eaccelerator configuration item must be placed before the Zend optimizer configuration, otherwise it may also cause 502 Bad Gateway. The third reason is that 502 problems occur during installation and use, it is generally because the default PHP-CGI process is five, and may cause 502 because the phpcgi process is insufficient, you need to modify/usr/local/PHP/etc/php-fpm.conf to add the max_children value as appropriate. The fourth reason is:
PHP Execution times out. Modify/usr/local/PHP/etc/PHP. ini _ Time Change to 300 for the fifth reason:
Insufficient disk space. For example, the sixth reason is that MySQL logs occupy a large amount of space:
Check whether the PHP-CGI process is running Some netizens have also provided other solutions: Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process is terminated for some reason (generally because the problem of reading resources) is not completed, in general, nginx 502 Bad Gateway is related to the php-fpm.conf settings. The php-fpm.conf has two crucial parameters: max_children and Request_terminate _ Timeout, but this value is not general, but must be calculated by yourself. 502 problems occur during installation and use. Generally, the default PHP-CGI process is five, which may be caused by insufficient phpcgi processes, you need to modify/usr/local/PHP/etc/php-fpm.conf to add the max_children value as appropriate. The calculation method is as follows: If your server has good performance and sufficient bandwidth resources, you can directly Request_terminate _ Set timeout to 0 s. 0 s means to keep the PHP-CGI running without a time limit. And if you can't do that, it means your PHP-CGI may have a bug, or your bandwidth is not enough, or other reasons lead to your PHP-CGI, then I suggest you give Request_terminate _ Timeout is assigned a value that can be set based on the server performance. In general, the better the performance, the higher you can set, 20 minutes-30 minutes can be set. How does max_children calculate the value? In principle, the larger the value is, the better. If the PHP-CGI process is too large, it will process very quickly and there will be very few requests in the queue. You also need to set max_children Based on the server performance. Generally, a server normally consumes about 20 mb of memory per PHP-CGI. 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, it indicates that the number needs to be increased (depending on the actual situation of your machine) 3. Adjust the related settings of/usr/local/PHP/etc/php-fpm.conf <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 _The timeout execution time is 60 seconds, that is, 1 minute. [Transfer] http://dadloveu.blog.51cto.com/715500/766214

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.