502

Bad Gateway

Official Substatus codes
the downstream server returned an error message

Specification of the HTTP status code 502

The 502 Bad Gateway status code indicates that the server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.

Source / Quote by: The 502 Bad Gateway HTTP Status Code is specified by section 6.6.3 of RFC7231.

How to throw a 502 statuscode with PHP?

To throw the HTTP status code 502 on a web page, the PHP function http_response_code can be used. The syntax is as follows: http_response_code(502) (PHP 5 >= 5.4.0, PHP 7, PHP 8)

Test the 502 HTTP status code

In order to be able to display the HTTP status code (in this case 502 Bad Gateway) and other information on the client side, the development console must be opened with F12. Afterwards you have to navigate to the tab "Network". Now you can open the page, in the network tab you should see the web page (example index.php). This must be selected and then the Herder section must be selected. Here the user will see the following result:

Status code 502 Bad Gateway
Overview
URL: https://http-statuscode.com/errorCodeExample.php?code=502
Status: 502 Bad Gateway
Those: Network
IP address: XX.XX.XX.XX
Status code 502 Bad Gateway

How do I solve the problem with the 502 status code?

The HTTP status code 502 Bad Gateway is usually returned by a proxy server, which acts as an intermediary between the client (for example, a web browser) and the server with which the client wants to communicate. The error occurs when the proxy server receives an invalid response from an upstream server. There are several possible causes for this error, and I will discuss some of the most likely problems below and explain how you can fix them.

  • Overloaded or unreachable upstream server: A common reason for HTTP status code 502 Bad Gateway is that the upstream server to which the proxy server connects is overloaded or unreachable. In this case, you should make sure that the upstream server is working properly and has enough resources to process the requests. Check the server logs and network connections to see if there are any problems. If the upstream server is overloaded, you may be able to distribute the load across multiple servers or add additional resources to fix the problem.
  • Network problems: Another possible problem is network issues between the proxy server and the upstream server. This could be an unstable connection, a DNS issue, or a firewall setting that is interfering with communication. Check the network settings to make sure that all the necessary ports are open and the connection is stable. Also test DNS resolution to make sure the upstream server is resolving properly. In some cases, it may also be helpful to restart the proxy server to resolve network issues.
  • Misconfigured proxy server: Incorrect configuration of the proxy server can also result in an HTTP status code 502 Bad Gateway. Make sure that the proxy settings are correct and that the proxy server is properly forwarding requests to the upstream server. Check the proxy configuration files and settings to make sure they are correct. In some cases, it may also be helpful to upgrade the proxy server or switch to a different version to fix known issues.
  • Faulty application on the upstream server: It is also possible that the upstream server has a faulty application or configuration that results in a 502 Bad Gateway HTTP status code. Check the application and server logs to look for possible errors or exceptions. Update or repair the application to fix known issues. It may also be helpful to work with a developer or system administrator to analyze and resolve the issue.
  • Problems with the proxy server itself: Finally, HTTP status code 502 Bad Gateway can also be caused by problems with the proxy server itself. This can be due to software bugs, missing updates or hardware issues. Check the proxy server logs for error messages or warnings. Update the proxy server to the latest version and check if there are any known issues. In some cases, it may be necessary to reinstall the proxy server or switch to an alternative software solution to fix the problem.

It is important to note that resolving HTTP status code 502 Bad Gateway depends heavily on your specific environment and configuration. It may be helpful to work with an experienced developer or system administrator to diagnose and resolve the issue.

How to create a custom error page for the 502 status code

Creating your own 502 Bad Gateway error page is relatively easy with the web server "Apache" as well as with the web server "NGINX".

Apache Webserver

The web server "Apache" is one of the most popular web servers on the Internet. To create an own 502 Bad Gateway error page in "Apache", the following change must be made in the following file.

File: .htaccess
ErrorDocument 502 /errors/502.html

NGINX Webserver

Similar to the web server "Apache", "NGINX" is also widely used on the Internet. To create your own 502 Bad Gateway error page in "NGINX", the following change must be made in the following file.

File: sites-enabled/default
error_page 502 /502.html;
location = /502.html {
    root /usr/share/nginx/html;
    internal;
}

Browser compatibility of the 502 status code

Chrome no data
Edge no data
Firefox no data
Opera no data
Safari no data
Chrome Android no data
Firefox for Android no data
Opera Android no data
Safari on iOS no data
Internet no data
WebView Android no data

Constants in programming languages

HttpStatusCode.BadGateway
http.StatusBadGateway
Response::HTTP_BAD_GATEWAY
httplib.BAD_GATEWAY
http.client.BAD_GATEWAY
http.HTTPStatus.BAD_GATEWAY
:bad_gateway

Sub status codes of the 502 status code

Sub status codes are purely technical, and should never be sent to the user. For example, if status code 502.1 is thrown, it may be logged, but status code 502 will be sent to the user.:
502.1 CGI application timeout
HTTP IIS, Unofficial
502.2 Bad gateway: Premature Exit
HTTP IIS, Unofficial
502.3 Bad Gateway: Forwarder Connection Error (ARR)
HTP IIS, Unofficial
502.4 Bad Gateway: No Server (ARR)
HTP IIS, Unofficial
502.5 WebSocket failure (ARR)
HTTP IIS, Unofficial
502.6 Forwarded request failure (ARR)
HTTP IIS, Unofficial
502.7 Execute request failure (ARR)
HTTP IIS, Unofficial
Author: Tony Brüser
Author: Tony Brüser

Tony Brüser is an enthusiastic web developer with a penchant for HTTP status codes.

LinkedInGitHub