Home >Operation and Maintenance >Nginx >502 bad gateway nginx what does it mean?

502 bad gateway nginx what does it mean?

藏色散人
藏色散人Original
2019-08-28 10:46:3965153browse

502 bad gateway nginx what does it mean?

1. What does 502 bad gateway nginx mean?

Generally speaking, 502 bad gateway means there is a problem with the website you are visiting, because when the 502 Bad Gateway server acts as a gateway or proxy, it is to complete access to the next server, but the server returns An illegal response was received. Maybe temporarily, maybe yes. It is recommended that you wait for a while and try to access again.

2. What causes 502 bad gateway?

Most of the reasons for 502 bad gateway are due to connection timeout. We send a request to the server. Because the server currently has too many connections, the server cannot give a normal response. This will occur. 502 bad gateway error.

Generally speaking, the server just acts as a gateway or proxy to meet the requirements of customers (such as web browsers) to access the URL we request. The server received an invalid response from the upstream server it requested to access. This does not mean that the upstream server is down (unresponsive gateway/proxy), but that the upstream server and gateway/proxy do not agree on the protocol to exchange data. Since the Internet protocol is fixed, this means that one or two machines have been incorrectly or not fully programmed.

3. What is the cause of the fixed 502 error?

Generally this problem is due to poor IP communication between the backend computers, possibly including the web server for the site you are trying to access. While analyzing this issue, you should clear your browser cache before visiting again.

If you see this problem on all websites you try to access when surfing the Internet, there are two possibilities

①Your ISP has a major equipment failure/overload

②Yes Problems with the internal internet connection such as your firewall not functioning properly.

In this case, only your ISP can help you. In the second case, you need to fix what is blocking your access to the Internet.

If you only experience this problem with some of the websites you are trying to access, it is most likely a problem with one of these websites, whose equipment is malfunctioning or overloaded. Contact your site's administrator.

4. How to rescue 502 bad gateway?

1. Refresh

To put it bluntly, it is very simple - refresh (not an ordinary refresh)

The refresh mentioned here mainly refers to downloading from the server The data is sent to the local hard disk browser, and then the data is read from the local hard disk and displayed in the browser for us to see. There are usually two refresh modes:

①Basic refresh: click refresh or use the F5 shortcut key

The basic refresh just re-fetches the data from the local hard disk to the browser, and does not redirect it to the browser. The server makes the request. Most users refresh like this most of the time, and it has no effect when encountering a 502 error.

② Refresh from the server: If you re-click directly on the webpage link you want to browse, you will find the "502 bad gateway" error message page that was just displayed. After re-clicking the link, you can browse normally.

Solution: Refresh from the server: shortcut key ctrl F5. This will resend the request to the server. If the server can respond to you normally, you can see the page.

2. DNS buffering problem

This situation is mainly caused by you visiting some foreign websites that are blocked by the national firewall, such as YouTube and Facebook.

Solution: In this case, access is usually available within a few minutes. You can also try running ipconfig /flushdns in the cmd window to refresh the DNS cache

3. DNS is hijacked

Solution: Change the DNS, 114.114.114.114 is recommended, and Ali’s public DNS such as: 223.5.5.5, 223.6.6.6

5. Nginx 502 bad gateway error

① Check whether php-cgi is running

Sometimes due to excessive website traffic or other reasons, php-cgi goes down directly, so we have to check whether php-cgi is running. Execute the following command:

    ps -A | grep php5-cgi

If it is not running, we can start it manually

    /etc/init.d/php_cgi start

If you find that php-cgi sometimes goes down for unknown reasons, you can use the following script to temporarily solve the problem. Add to cronjob.

    if ps aux | grep ‘php5-cgi’ | grep -v grep > /dev/null ; then echo "PHP-cgi is runnning !" else echo "PHP-cgi is down. Starting over…" /etc/init.d/php-fcgi start fi

②The reason why the number of fastcgi processes is not enough and the php execution time is long

The number of fastcgi processes can modify the value of max_children in php-fpm.conf, and the memory consumed by php-cgi at peak times It is 20M, please calculate it according to your own memory situation.

Limiting the execution time of php can be set in request_terminate_timeout in php-fpm.conf. This is to prevent bugs in the php program from causing php-cgi to die.

③FastCGI execution time is too long

Increase the following parameter values ​​according to the actual situation

    fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;

6. Solve the 502 error in nginx

    ①查看当前的PHP FastCGI进程数是否够用

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

    如果实际使用的"FastCGI进程数"接近预设的"FastCGI进程数",那么,说明"FastCGI进程数"不够用,需要增大。

    ②部分PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间

    例如,系统当前的PHP FastCGI进程数明显超过了预设值的64这个数值,在电信的服务器上查看当前的PHP FastCGI进程数没有高于64这个数值,而且网通线路的活动连接明显高于电信的活动连接,由此可以说明出现nginx不稳定的情况是由于服务器访问负载过大引起的。

    总结:php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉,都会出现502错误。

更多Nginx相关技术文章,请访问Nginx使用教程栏目进行学习!

The above is the detailed content of 502 bad gateway nginx what does it mean?. 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