Troubleshooting
Why an OpenEMS Edge WebSocket gets a 502 from your reverse proxy
Short answer
Close code 1002 carrying a 502 means NGINX accepted the Edge's handshake request and then failed to reach the backend WebSocket listener. Prove the Edge-facing backend port answers a direct connection on the proxy host first. Then make one route consistent: Edge URL path, NGINX location and upstream port, read from the running backend configuration.

On this page
A WebSocket close with code 1002 and a 502 inside it is the proxy failing, not OpenEMS. The Edge device reached NGINX, NGINX accepted the handshake request, and only then could it not get a valid answer from the backend listener behind it. Fix it in that order. Prove the Edge-facing backend port accepts a direct connection on the proxy host: in the reported setup that port was configured as 8081 and refused even local connections, which is the whole fault. Once the listener answers locally, make one route consistent end to end: the Edge Controller Api.Backend URL path, the NGINX location prefix and the upstream port. A 502 never left the proxy, so no Edge-side setting will clear it.
What close code 1002 with a 502 tells you #
The Edge's Controller Api.Backend controller dials the backend URL, sends an ordinary HTTP request that asks to upgrade the socket to a WebSocket, and expects HTTP 101 Switching Protocols back. Close code 1002 is that client's protocol-error code: what arrived was not a valid upgrade response. The reason text in the reported log names it precisely — invalid status code received, 502, status line HTTP/1.1 502 Bad Gateway. A proxy emits 502 Bad Gateway when its own upstream request fails with a refusal, a timeout or a dead socket.
That signature splits the connection path in two. Everything up to the proxy works: DNS for the host name, the route from the Edge network, TCP port 80 and NGINX itself, because NGINX is the side that answered. Everything beyond the proxy failed on the way to the backend. The decision is to leave the Edge network, the firewall rule and the host name alone and work only on the backend listener and the proxy-to-backend hop.
Prove the backend listener on the proxy host #
The backend port is a deployment setting, so read it before testing anything:
- Read the port from the running backend configuration, not from a default. The documented defaults belong to other listeners: the Backend's Ui.Websocket on port 8082 serves browsers and the Edge's Controller.Api.Websocket on port 8085 serves UI clients connecting straight to an Edge. The listener your proxy targets for Edge traffic has no documented default; the published routing example runs it on 8091.
- On the proxy host, list the listening TCP sockets and look for that port, for example
ss -ltn | grep 8081. No output means the service is stopped, failed to bind, or lives in another network namespace such as a Docker container, where localhost on the NGINX host is not the backend. - Connect from the proxy host straight to the port:
curl -i --max-time 5 http://127.0.0.1:8081/. Any HTTP answer at all, even an error status, proves a live listener on that address and port. Connection refused proves this hop is the fault. - Match the bind address to the upstream. A listener bound to one interface will not answer on 127.0.0.1, and an upstream written as localhost fails when NGINX and the backend run on different machines or in separate containers, so put the address NGINX can actually reach into the upstream.
A backend that refuses connections even from its own machine is its own fault class with its own trace: why an OpenEMS Backend login returns connection refused walks that case from the service state outwards.
Wire the NGINX route after the listener answers #
Once curl on the proxy host gets any HTTP answer, wire the route. The published example keeps three endpoints on one server block: /m/ serves the OpenEMS UI files, /openems-backend-ui proxies the UI-to-Backend WebSocket, and /openems-backend proxies the Edge-to-Backend connection. The browser side is configured separately from the Edge path, and configuring OpenEMS UI themes and backend connections covers that side. The Edge-facing location needs the prefix, an HTTP/1.1 upstream request, the two upgrade headers and a read timeout longer than the longest idle gap your telemetry accepts:
location /openems-backend {
proxy_pass http://localhost:8091;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 36000s;
proxy_send_timeout 36000s;
}
NGINX defaults the upstream request to HTTP/1.0 and forwards no Upgrade header on its own, so proxy_http_version 1.1 plus the two proxy_set_header lines are what let the handshake pass a healthy listener. The default proxy_read_timeout is 60 seconds, which kills a silent telemetry socket after one minute; the example raises it to 36000 s, and 36000 divided by 3600 is 10 hours. The scheme on proxy_pass stays plain HTTP, because NGINX speaks HTTP for the handshake and switches the socket after the backend answers 101.
One safety line belongs here: a plain WebSocket URL through an unencrypted port 80 listener carries telemetry across the network unencrypted, so terminate TLS on the proxy before the connection leaves the site.
Align the Edge URL path with the location #
The public path and the forwarded path are two different values. NGINX's documented rule: when proxy_pass carries a URI, it replaces the matched location prefix with that URI; when it carries none, the request path reaches the backend unchanged. The reported configuration uses the first kind — location /openems-ws/ with proxy_pass http://openems-ws/ turns an incoming /openems-ws/ request into / at the backend, and the group named openems-ws is the upstream block defined above it in the same file, not an external host name. The working example uses the second kind: proxy_pass without a URI forwards /openems-backend exactly as the Edge sent it.
Commit to one route and set the Edge side to match. With the published layout, the Controller Api.Backend URL becomes ws://my_domain/openems-backend and the location prefix is /openems-backend. With the single-route layout, the URL stays ws://my_domain/openems-ws/ and the upstream must point at the port the backend actually listens on. The handshake test through the public URL is the last check; after it returns 101, this fault is closed.
The numbers, traced end to end #
The failing run, reconstructed from the reported setup:
- The Edge dials
ws://my_domain/openems-ws/with proxy port 80. DNS resolves, TCP 80 connects, NGINX receives the upgrade request for /openems-ws/. - NGINX matches location /openems-ws/; because proxy_pass carries a URI, the forwarded request arrives at the upstream group as GET /.
- The backend's Edge listener was configured for 8081, and the direct test from the same host returned connection refused, meaning no socket on that port.
- NGINX answers the upgrade request with HTTP/1.1 502 Bad Gateway. The Edge expected 101, logged close code 1002 with the 502 in the reason, and closed.
The fault sits at step 3, the backend listener. Steps 1 and 2 succeeded by definition, because NGINX is the side that answered.
The working run, with the published routing numbers:
- Backend Edge listener confirmed listening on 8091 with the socket listing shown earlier.
- NGINX: location /openems-backend, proxy_pass http://localhost:8091 with no URI, so the path arrives unchanged.
- Edge: Controller Api.Backend URL
ws://my_domain/openems-backend. - Checks that must all hold: public port 80 is the port NGINX listens on; the Edge URL path equals the location prefix, /openems-backend equals /openems-backend; the upstream port equals the listener port, the number the direct test proved. The handshake returns 101 and the channel stays open for the read timeout you set.
| Hop | Setting | Reported failing value | Working value |
|---|---|---|---|
| Edge | Controller Api.Backend URL | ws://my_domain/openems-ws/ |
ws://my_domain/openems-backend |
| NGINX | location prefix | /openems-ws/ |
/openems-backend |
| NGINX | upstream | named group openems-ws, localhost:8081 | localhost:8091, no URI in proxy_pass |
| Backend | Edge-facing WebSocket listener | configured 8081, refusing connections | confirmed listening on 8091 |
Why this is not the blocked-MQTT fault #
A reverse proxy can also break this kind of connection by stripping the upgrade headers, and that failure looks different from the client side: raw protocols never tunnel at all and the request dies as a protocol error. That case is a header fault, and why an HTTP reverse proxy breaks native MQTT to your cloud broker takes it apart. The 502 sits one hop earlier: with proxy_http_version 1.1 and the Upgrade and Connection headers in place, the request already reaches NGINX intact, so what is missing is a listening upstream, not a header.
The decision for each case #
- Connection refused on the direct test from the proxy host: the backend listener is down, bound elsewhere, or in another network namespace. Fix the backend service and its bind address first; NGINX and the Edge are innocent.
- Listener answers locally but the proxy still returns 502: the route is wrong. Point the upstream at the proven address and port, keep proxy_http_version 1.1 with the Upgrade and Connection headers, align the Edge URL path with the location prefix, and raise the read timeout for idle telemetry.
- Route and listener both verified and a 502 appears right after a proxy upgrade: suspect the proxy release itself and retest the previous version before changing any OpenEMS setting.
Questions asked
Which ports do the OpenEMS WebSocket listeners use by default?
Two defaults are documented: the Edge's Controller.Api.Websocket listens on port 8085, and the Backend's Ui.Websocket listens on port 8082. The Backend UI Docker image additionally publishes port 8079 for the UI service. The Edge-facing listener your proxy must reach is a deployment setting with no default to assume: one reported setup ran it on 8081 and the published routing example used 8091, so read it from the running backend configuration.
Does changing proxy_pass to ws:// fix the 502?
No. NGINX speaks plain HTTP to the upstream for the handshake, so an HTTP scheme on the upstream with proxy_http_version 1.1 plus the Upgrade and Connection headers is the correct form; the traffic becomes a WebSocket only after the backend answers 101. Changing the scheme creates no backend connection and does nothing for a listener that refuses connections. The 502 clears when the upstream address and port match a live listener, not when the scheme changes.
Can a different reverse proxy produce the same 502?
Yes, and it is not always a configuration mistake. A reported HAProxy move from the 2.3.14 Alpine build to 2.4.19 began returning 502 Bad Gateway for WebSocket backends over both HTTP/2 and HTTP/1.1, and rolling back to 2.3.14 cleared it. With Apache, the reported correction was to target the port actually set in the backend configuration, 127.0.0.1:8078 in that deployment, instead of a generic route.
The OpenEMS UI works through the same proxy. Doesn't that prove the backend is fine?
No. The Backend serves UI connections and Edge connections on separate WebSocket listeners with separate ports, the UI one at its documented default and the Edge-facing one set per deployment. A working dashboard proves only the UI listener and its route. Test the Edge-facing port itself from the proxy host before judging the Edge route, because a healthy UI side and a refused Edge side coexist, which is exactly the reported failure.


