Open Source API Gateway

502 Bad Gateway: Backend Closed the Connection

Membrane API Gateway returns 502 Bad Gateway with the detail Backend closed the connection before the response was complete. when the backend starts to answer but closes the connection partway through the status line or the response headers. The backend or a network device between gateway and backend is to blame.

The Error Response

The response contains the status 502 Bad Gateway, the title Bad Gateway, the detail Backend closed the connection before the response was complete., the problem type https://membrane-api.io/problems/internal and a link to this page. The type internal means that the gateway could not complete the processing of the request. In production mode, the title is replaced by Internal Server Error and the details are left out.

Common Causes

The Backend Crashed or Restarted

The backend crashed, restarted or killed the worker that handled the request while it was writing the response head.

A Device in Between Cut the Connection

A load balancer, firewall or proxy between Membrane and the backend closed the connection, e.g. because of a request timeout.

The Backend Sends a Malformed Response

The backend writes a response head without the line breaks HTTP requires and then closes the connection.

How to Fix It

  1. Operator: check the backend's log and health at the time of the failure.
  2. Operator: check timeouts and connection limits of network devices between gateway and backend.
  3. Client: retry the request. An idempotent request is usually safe to repeat.

Hide Details in Production

By default, Membrane runs in development mode and adds details to error responses that help you find the problem. These details may reveal information about your setup. In production, turn them off:

configuration:
production: true
configuration:
  production: true

Add the configuration document to your apis.yaml and separate it from the APIs with ---.

Related Documentation

Configuration reference:

Tutorials with runnable examples:

← All Membrane error messages

Still stuck? See the troubleshooting guide or ask on GitHub Issues. For commercial support, contact info@predic8.de.