Bad gateway
The Bad gateway error means that a server acting as a gateway or proxy received an invalid or no response from an upstream server. This is a global ROOT-level error that commonly appears in web servers, APIs, load balancers, and reverse proxy setups. It can occur across Linux and Windows systems, Java and Spring Boot applications, Docker containers, cloud platforms, and microservice architectures when inter-service communication fails.
When does this error occur?
- A reverse proxy forwards a request to an upstream service that is down
- An API gateway cannot get a valid response from a backend service
- Timeouts or connection resets between proxy and upstream server
- Misconfigured upstream host, port, or protocol
- Overloaded backend services failing to respond correctly
Root cause of Bad gateway
At the network and application boundary, Bad gateway occurs when an intermediary server successfully receives a client request but fails to obtain a valid response from the next server in the chain. This can be caused by upstream service crashes, incorrect routing, protocol mismatches, connection failures, or invalid responses that the gateway cannot parse or forward.
How to fix the error (step-by-step)
Linux / macOS
Check whether the upstream service is reachable from the gateway.
curl http://upstream-host:port
ping upstream-host
Verify that the service is listening on the expected port.
ss -tuln
netstat -tuln
Windows
Test connectivity to the upstream service.
ping upstream-host
tracert upstream-host
Confirm that the backend service is running and accessible.
Java / Spring Boot
Ensure the backend application is running and responding correctly.
curl http://localhost:8080/health
Check application logs for crashes, exceptions, or long response times. Verify that proxy timeout values are not lower than backend processing time.
Docker / containers
Confirm container networking and service discovery.
docker ps
docker network inspect bridge
Test connectivity from the proxy container to the backend container.
docker exec -it <proxy_container> curl http://backend:port
Database / network services
If the upstream depends on a database or external service, ensure those dependencies are reachable and responsive.
Verify the fix
Repeat the original request through the gateway or proxy. The issue is resolved when the gateway consistently receives valid responses from the upstream service and forwards them successfully to clients.
Common mistakes to avoid
- Restarting the gateway without checking upstream service health
- Assuming the error is caused by client requests
- Setting proxy timeouts lower than backend response times
- Misconfiguring upstream hostnames or ports
- Ignoring backend application logs
Quick tip
Always monitor upstream service health and response times to prevent Bad gateway errors in proxy-based architectures.
FAQ
Q: Is Bad gateway the same as Service unavailable?
A: No. Bad gateway indicates an invalid response from an upstream server, while Service unavailable means the service is temporarily unable to handle requests.
Q: Can Bad gateway occur even if the backend is running?
A: Yes. It can happen if the backend responds too slowly, crashes mid-request, or returns malformed responses.
Conclusion
The Bad gateway error signals a failure between a gateway and its upstream service. Verifying connectivity, configuration, and backend health resolves the issue. Explore related ROOT gateway and network errors on ErrorFixHub for deeper troubleshooting.
Comments
Post a Comment