Network

502 Bad Gateway: what causes the error and how to fix it

502 Bad Gateway: what causes the error and how to fix it

I HTTP error codes they are possibly returned by the Web server to the client when requesting a specific page or resource when there are problems relating to its correct delivery. In reality, every time the browser asks the web server for any item, the latter returns a status code. When everything is successful, the 200 (“OK”) code is released in the HTTP response header. Lately it can happen to often see errors 502 Bad Gateway: what are they, what are they due to and how to solve the problem?

The status codes of HTTP requests

Regardless of whether you use the HTTP protocol or HTTPS, variant that adds the data encryption and the attestation of the identity of the website through a digital certificate, each resource transferred to the browser contains, in the header, a status code. Among the most famous are the 404 and 500 errors which, respectively, signal a missing resource and a server-level problem that prevented the delivery of the requested item.

With the 500 errorsthere is usually very little that can be done: their appearance is an indication of an anomaly detected by the web server. They can be caused by a programming error in the scripts of the pages, from the impossibility of connecting to a database, from the unavailability of a resource used on the server side, from a software problem that impacts on the correct functioning of the Web server. Just to give a few examples.

In the case of 500 errors, in short, the problem is server side so it depends and can only be fixed by the administrators of the website you are visiting.

Identikit of the 502 Bad Gateway error

lie themistake 500 is generico and, unless additional messages appear, does not provide detailed explanations of the nature of the problem, the appearance of 502 Bad Gateway it is certainly more “talking”.

The error 502 Bad Gatewayin fact, indicates a communication problem between a server and a gateway o proxy. The message occurs when the gateway or proxy does not receive a valid response from the destination servers.

Gateways and proxies are systems that interpose themselves between the user’s client (the browser that requests a resource via HTTP/HTTPS) and the web server itself. If this intermediate system cannot communicate with the destination server, for example because it does not respond, is late in doing so or sends inadequate responses (because it is itself in error), here is the 502 Bad Gateway error.

But why do you need to use a gateway or proxy? Is it not possible to connect the browser directly with the destination server?

Gateways and Proxies: Why They Are Used on the Web

More and more websites are using gateways and proxies. The reasons are the most diverse: caching, load balancing, protection and security, optimization of data packets, filtering and access control. That’s why, when something doesn’t work, lately we are witnessing more and more often the appearance of502 error.

And server proxy can cache responses from the target server. When any client-side browser requests a resource, the proxy checks if it has a copy in cache of the answer. If so, the proxy will directly return the response to the client without having to contact the destination server again. That is improves performance reducing latency and bandwidth usage. One of the most famous services that offer features of proxying e caching it’s Cloudflare, but there are many others. The advantage of Cloudflare is that these skills are also accessible with the free plan, completely free.

Thanks to this mechanism, the destination Web server is automatically much more “unloaded” both at the level of committed resources (CPU, memory,…) and bandwidth involved.

What proxies and gateways do

A proxy or gateway, as mentioned above, can also be used for distribute the load of requests between multiple destination servers. Instead of sending all requests directly to a single server, algorithms can be used load balancing. This way administrators can improve performance by maximizing the scalability of the system.

Some proxies provide advanced features such as those for the safety. They are useful, for example, for monitoring data packets in transit and filtering the IP addresses from which “dirty” or harmful traffic can notoriously originate.

Also, in some cases, the proxy is called ad optimize content transferred via HTTP/HTTPS. It can for example compress responses of the web server to reduce the amount of data transmitted and, for example, perform the image compression so that they are easier to manage. Thanks to these precautions, the page is delivered in a shorter time, the performances are improved overall and it is possible to obtain the favor of tools such as Google PageSpeed ​​Insights and Chrome Lighthouse.

How to fix the 502 Bad Gateway error

Being a of a 50x erroralso the 502 Bad Gateway is difficult to resolve by the user connecting to a website. Usually the error 502 Bad Gateway it is temporary and is intended to resolve itself in a short time.

Sometimes it is attributable to a problema del proxy/gateway while the destination server does not show any difficulties. The Cloudflare System Status page reports real-time issues that can cause a 502 error to appear on clients. Other suppliers set up dashboard similar.

More experienced users can try connecting to the target web server without going through the proxy. Cloudflare and the like do not expose the public IP address of the main web server: thus, typing the nslookup command followed by the domain name at the command prompt always returns the proxy address.

However, try using services such as ViewDNS.info IP history and IPhistory.ch. By typing, in the search box, the domain name of your interest, you receive the list of IP addresses used over time. You can try editing your local HOSTS file by entering your pre-Cloudflare IP address.

502 Bad Gateway, scavalcare proxy

Let’s take the example in the figure. As you can see, the website in question switched to using Cloudflare as a proxy service at some point. What you can try to do is intervene on the file HOSTS specifying one of the two public IPs indicated followed by the fully qualified domain name.

Without changing HOSTS, you can resolve a domain name differently using Chrome: just switch to the Google i browser parameters corrected.

By doing so, you can bypass the proxy and see if you can reach the target website bypassing Also the error 502 Bad Gateway.

The image used in the opening is from iStockphoto and has been acquired under license.

Leave a Reply

Your email address will not be published. Required fields are marked *