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

502 bad gateway nginx what does it mean?

Aug 28, 2019 am 10:46 AM
nginx

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
Copy after login

If it is not running, we can start it manually

    /etc/init.d/php_cgi start
Copy after login

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
Copy after login

②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;
Copy after login

6. Solve the 502 error in nginx

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

    netstat -anpo | grep "php-cgi" | wc -l
Copy after login

    如果实际使用的"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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

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.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles