SIP codes

SIP 502 Bad Gateway: what it means and how to fix it

Short answer: SIP 502 Bad Gateway means a server acting as a gateway or proxy received an invalid response from the next server on the route. On trunks, it usually points to a failure in the carrier's interconnection with the destination network, or a destination out of service. Use the headers to find who generated the 502 and give the carrier the Call-ID.

SIP 502 in the reference
Code502
Official nameBad Gateway
Class5xx: server failure
Defined inRFC 3261
Q.850 causes that usually come with it27 (Destination out of order), 38 (Network out of order)

How Monitor treats SIP 502

The reading depends on what should answer the number, which is the question 2N5 answers when it adds each circuit. These are the rules the product applies today, generated from the same function that judges every test call.

What should answer the numberReading
A greeting or recorded message (the IVR)Down
A test tone or echo serviceDown
Other: it may ring or be answeredDown

What Monitor records for an IVR: The provider could not complete the call because of a fault on its side: a server on the route got a bad reply from the next one (SIP 502).

A call read as Down alerts nobody on its own: it is repeated 3 times, every 2 minutes, and only 4 failures in a row open an incident, with each call's SIP code and Q.850 cause as evidence for the carrier. At the default pace, every circuit is tested every 15 minutes.

If the carrier returns SIP 502 when 2N5's calling account registers, before any call, the test is monitoring blind: it is a problem on 2N5's side, it does not count against the circuit and it does not alert the customer.

What does SIP 502 mean in practice?

SIP 502 Bad Gateway is a 5xx response. Under RFC 3261, the responding server was acting as a gateway or proxy and received an invalid response from the downstream server it contacted to fulfill the request. In other words, whoever sends the 502 is not the direct culprit: it is reporting that the next hop failed.

Where SIP meets the phone network, RFC 3398 converts Q.850 cause 27 (Destination out of order) into a 502, and a 502 into cause 38 (Network out of order). That is why a SIP 502 error on a call to a landline, a mobile or an 0800 (toll-free) number usually means the destination exchange could not deliver the call. In SIP trunk monitoring, what matters is telling a one-off 502 apart from a 502 that keeps repeating for the same destination.

Common causes in Brazil

  • A destination out of service on another carrier’s network, signaled by the exchange with Q.850 cause 27 and converted into a 502 by the gateway.
  • A failure in the interconnection between your trunk’s carrier (Vivo, Claro, Algar, Embratel or another) and the network that hosts the number.
  • A destination PBX with its own IVR, or an E1 link, that is down, so the carrier’s gateway receives a failure.
  • An SBC on your network, in front of Asterisk, Issabel, FreePBX or 3CX, receiving a response from the PBX that it does not accept.
  • Upgraded equipment on the route that started sending non-standard messages to the previous hop.

How to troubleshoot

  1. Capture the call with sngrep and see who generated the 502 from the Via, Server and User-Agent headers.
  2. In Asterisk, Issabel or FreePBX, turn on pjsip set logger on (or sip set debug on in chan_sip) and look for Reason: Q.850;cause=27 or another cause in the response.
  3. Call the same number through another trunk or from a mobile phone. If that also fails, the problem is at the destination.
  4. Try other numbers on the same destination carrier. If they all fail, suspect the interconnection.
  5. If the 502 comes from your own SBC, capture on the PBX side as well and compare the response the PBX sent with the one the SBC passed on.
  6. In 3CX, turn on verbose logging and check the Activity Log for the trunk that returned the 502.

What to send your carrier

  • The exact date and time with the time zone, for example 2026-10-07 14:32:10 -03:00.
  • The calling number (A), the called number (B) and the Call-ID.
  • The full SIP/2.0 502 Bad Gateway line and the Server and Reason headers.
  • Whether other numbers on the same destination network also fail, and how many attempts were made.
  • The pcap file of the capture, if you have one.

Frequently asked questions

Is a SIP 502 a problem with my PBX?

Rarely. A 502 is generated by equipment in the middle of the route, such as a proxy, an SBC or a gateway to the phone network, when the next hop returns something invalid. If the 502 comes from the carrier, the failure is usually beyond it. If it comes from your own SBC, check the response the PBX sent to it.

Which Q.850 cause goes with a SIP 502?

Under RFC 3398, a gateway converts a SIP 502 into Q.850 cause 38 (Network out of order). In the other direction, Q.850 cause 27 (Destination out of order) becomes a 502. A `Reason: Q.850;cause=27` header means the destination exchange considered the destination out of service.

Does a real test call catch this error?

Yes. A real test call receives the final SIP response the network returns, including a 502. When the result counts as a failure, the call is retried before any alert goes out, and the alert is sent by email and SMS.

Know before your customers do

Monitor calls every voice circuit all day and delivers each failure with the SIP code and the Q.850 cause as evidence.