Each Internet user saw the message “502: Bad Gateway” in his browser, which means that an error occurred on the web server side during the processing of the request. Most often, the causes of failures are associated with incorrect configuration of the web server or equipment of the hosting company. A common problem is incorrect DNS or proxy processing.
What does "502: Bad Gateway" mean?
The error means that a request to the web server from the browser was received, but was incorrectly processed or deleted from the general queue. If the server is programmatically located behind the Internet gateway, then the probable problem is precisely in the gateway or in the operation of the web server itself, which transmits error information to the gateway, and it is then displayed in the user's browser.
Causes of Error 502. Lack of Server Resources
The main problem of this error is the insufficient level of server resources for processing incoming requests, which is why the server starts to generate error 502. Lack of resources can be either hardware (lack of RAM, insufficient processor or disk speed), or software related improperly configured server or software used.
But there is a situation when, with a sufficient level of resources, the server continues to periodically generate an error 502: Bad Gateway. What could it be if not hardware failures?
The resource limit can be entered automatically when a dedicated VPS server or shared hosting leaves the tariff plan. As a result, the server may not cope with the current load.
Web servers, such as Apache, have a strictly defined number of request handlers, which are set during its configuration. If all of them are simultaneously busy, the incoming request is placed in the queue, and its time cannot exceed a minute for each of them. If the waiting time is exceeded, the request processing is canceled, and the user gets an error 502. Additionally, the processor may simply not have enough current free resources for normal queue processing.
When using the PHP + FastCGI software bundle on the server, with an unoptimized number of php-cgi processes, overloads and lack of resources almost always occur.
The most common causes of insufficient resources to process requests

- The server does not cope with the current number of simultaneous website visitors. Also, a significant part of the resources can be spent by various search robots and incorrect software scripts.
- Server overload during hacker DDos attacks, when the server is brought to failure by a large number of simultaneous requests for processing.
- There is a situation when the error 502: Bad Gateway unexpectedly occurs when using the shared hosting service . What does it mean? This may indicate a problem on the hosting side, known as overselling, that is, the hoster is placing so many client accounts on one server that they lack current hardware resources at peak times.
- Incorrect configuration of the server itself or lack of software or hardware compatibility.
- Using plug-in external resources on the site, such as hit counters, various widgets, scripts. An error can occur with problems with such external sources.
- Uploading large files to the server with poor quality Internet connection.
PHP script errors
Error 502 can occur due to scripts, plug-ins, or extensions in the PHP language, which, upon completion of execution, do not pass the appropriate code to the server. For example, a 502: Bad Gateway nginx error can be generated as one of the proxy server variants due to incorrect definition of the script state and perception of a disconnection from the web server services as a system failure.
So, all the static information (pictures, site pages, Robots files) loads normally and without errors. What does “502: Bad Gateway” mean in this case? That a crash occurs when loading software scripts, plugins and extensions.
Corrective Action 502
First of all, it is necessary to analyze the current server load, especially at the time of the error. Particular attention should be paid to loading RAM and, if necessary, increasing its size to improve the process of processing requests.
Check if limits are set correctly for the number of simultaneously processed php-cgi processes. Their incorrect configuration leads to a quick overload of the service and the constant occurrence of the message "502: Bad Gateway". What is it and how to correctly set process limits, find out in the support service of the hosting provider.
Reduce requests to external network resources as much as possible and set a time limit for their download and response. This will allow you to see what the server’s resources are spent on, and will help get rid of the external “slipping” of the site.
If you suspect an overselling or incorrect server configuration, contact the host support service. If there is no reaction, and the problem persists, change the hosting provider. Server unavailability can lead to losses due to a simple error 502: Bad Gateway. What this means for business, I think, is not worth explaining.