IMD SolutionComputing LibraryShop main store

Troubleshooting

Why an HTTP reverse proxy breaks native MQTT to your cloud broker

Edge computing and IIoT Published 7 min read

Short answer

Your NGINX server block on port 80 forwards HTTP, but a native MQTT client sends binary MQTT control packets over TCP, so the proxy never completes a session. Forward raw TCP with a stream proxy or expose the broker port directly, with authentication, topic ACLs and TLS. WebSocket upgrade headers help only when both ends speak MQTT over WebSockets.

On this page
  1. What the listener actually receives
  2. Trace the hop before you touch the config
  3. Reading the first packet a native MQTT client sends to a port 80 listener
  4. The three paths that carry native MQTT
  5. Open the port without opening the broker
  6. Separate the broker session from the IoT Hub forward
  7. The decision for each setup
  8. Questions asked

A PLC with a native MQTT client cannot reach an Aedes broker that sits behind an NGINX server block listening on port 80, because that block parses HTTP and MQTT is not HTTP. The PLC sends a binary CONNECT packet, the HTTP parser rejects it before any broker code runs, and the client never sees a CONNACK. Take the HTTP listener out of the path: publish the broker's TCP port directly with authentication, topic authorization and TLS, or front it with an NGINX stream proxy when you need TLS termination or source filtering at an extra layer. WebSocket upgrade headers do nothing here unless the client and the broker both speak MQTT over WebSockets.

What the listener actually receives #

The posted configuration is an http-context server block: it accepts a connection, waits for an HTTP request line, and uses that request to choose a location and an upstream. A native MQTT client never produces a request line, so the parser has nothing to route and the proxy_pass line never executes. The Upgrade and Connection headers only mean something inside an upgrade exchange the client itself requested; they cannot turn a raw TCP session into one.

An HTTP front end is the right tool when the client itself speaks HTTP — the working case is a historian web client served through a reverse proxy — and the same transport rule is what makes it the wrong tool here.

Trace the hop before you touch the config #

Work the layers in order; each step rules out a different one. If the early steps fail, the problem predates the proxy entirely, and the PLC MQTT reachability fixes cover that underlying path.

  1. PLC interface: link state, IP address, subnet mask, gateway, and DNS when the broker is addressed by hostname.
  2. Destination: the VM's public endpoint, not the LAN address that worked in the local test.
  3. Broker listener: the startup log line and the process.env.PORT value the server actually bound, then list the VM's listening sockets to see which interface and port it holds.
  4. Cloud rules: the VM's network security group. Its rules name individual ports or port ranges, so the allow rule must cover the chosen TCP port, scoped to your plant's source addresses.
  5. Guest firewall: a permitted cloud rule does not override a blocking rule inside the VM; both layers must match the listener.
  6. TCP test from the PLC's network: a successful connect proves socket reachability, never MQTT acceptance.
  7. Logs: a connection that never arrives points upstream of the proxy; an HTTP bad-request reply points at the listener; a TCP session that opens and dies without a CONNACK is the case the no-CONNACK failing-hop walkthrough isolates.

A session that never opens is routing or filtering; a session that opens and dies at the parser is the section below.

Reading the first packet a native MQTT client sends to a port 80 listener #

The inputs, all from the posted code and configuration:

  • The listener: an NGINX server block with listen 80; and listen [::]:80;, which puts NGINX's HTTP request parser on everything that connects.
  • The forward: proxy_pass http://localhost:1883;, which points the block's HTTP client at the loopback TCP port the broker holds.
  • The backend: Aedes attached with require('net').createServer(aedes.handle), a plain TCP listener; the code contains no HTTP or WebSocket server for the block to forward to.
  • The client: a PLC with a native MQTT client, so its first packet is a CONNECT.
  • The protocol rule: every MQTT control packet starts with a fixed header whose first byte carries the packet type in its high nibble and packet-specific flags in its low nibble, and CONNECT is packet type 1.

What the example tests: an HTTP/1.1 parser expects a printable method token — GET, POST, HEAD — followed by a request target, while a native MQTT client expects its binary fixed header to reach a broker. One listener parses one shape, not both.

The arithmetic: a CONNECT starts with the byte 0x10. In binary that is 0001 0000 — a high nibble of 0001, which is decimal 1, the CONNECT type, and a low nibble of 0000, the zero flags CONNECT requires. The method token the block was written for begins with the bytes 0x47 0x45 0x54, the ASCII codes of G, E and T in GET. The received 0x10 matches no method token, so the parser raises a malformed-request condition, the listener answers HTTP 400 Bad Request and closes the socket. The TCP connection itself succeeded, which is exactly why a port test passed minutes earlier.

The result: the PLC receives an HTTP status where it waits for a CONNACK, treats the exchange as a failed connection, and re-sends CONNECT on a fresh socket for as long as its retry loop runs.

The decision: decode the first byte of the client's first packet. A high nibble of 1 (0x10) is a native MQTT CONNECT, and no location or header configuration in an HTTP server block can carry it — move the session to a raw TCP path. Only when the client's first packet is an HTTP upgrade request, and the backend behind proxy_pass can answer that upgrade, do the WebSocket headers do anything.

The three paths that carry native MQTT #

Path Client needs Broker side When it fits
Direct TCP to the broker Native MQTT over TCP Listener published on the public endpoint; cloud and guest rules allow it Default choice: fewest hops, nothing in the path to misparse
NGINX stream proxy Native MQTT over TCP A stream-context server block forwards raw TCP to the broker's real address and port; the installed build must include the stream module TLS termination or source filtering at the proxy
MQTT over WebSockets Client firmware configured for MQTT over WebSockets A WebSocket-capable broker listener, which the net.createServer wiring above does not provide Browser dashboards and script clients; PLC firmware rarely qualifies

Take the direct path first and prove the session end to end; that path is one inbound allow rule for TCP 1883 and nothing else in front of the broker. Add the stream proxy only when it earns its keep: its block mirrors the HTTP one, a listen directive for the public port plus a proxy_pass, but the bytes pass through uninterpreted. Retest from the PLC network after every change, because a configuration that loads is not a session that completes.

Open the port without opening the broker #

An MQTT port published with no access control is a public write into your topics, so this is the one safety-critical step: before the allow rule goes live, enable broker authentication so every client presents credentials, and add topic-level authorization so each client can publish and subscribe only to its own subjects. If the PLC firmware supports TLS, terminate it at the stream proxy or on a broker TLS listener and keep the cleartext port closed or loopback-only. Certificate and protocol-version choices on the listener side are their own task; the walkthrough of TLS 1.3 settings on an MQTT broker listener shows what that side looks like on one broker platform.

Separate the broker session from the IoT Hub forward #

The posted application runs two independent connections: the PLC's inbound MQTT session to Aedes, and the broker's outbound connection to Azure IoT Hub through the azure-iot-device client. The forwarding call iotClient.sendEvent runs only after aedes.on('publish') fires, so a hub-side delivery failure can exist while the PLC session is healthy, and a PLC that never connects can coexist with a working hub link. The Mqtt protocol import chooses only the hub-side transport; it changes nothing about the listener in front of Aedes. Confirm the PLC session first — the publish callback firing with a non-null client is the first sign of a completed CONNECT.

The decision for each setup #

  • Native PLC client, no proxy requirement — the posted case: publish the broker port directly, with authentication, topic authorization and TLS, scoped to the plant's source addresses.
  • TLS termination or source filtering needed at an extra layer: replace the http-context block with a stream-context one that forwards raw TCP.
  • WebSocket-capable clients only: give the broker a real WebSocket listener; then, and only then, the posted block with its upgrade headers becomes usable.
  • Any client whose first packet is a native MQTT CONNECT: never behind an HTTP server block, because the parser eats the CONNECT before the broker ever sees it.

Questions asked

The TCP port test to the VM passes, so why does the PLC still fail?

A successful TCP connect proves only socket reachability. The session still has to survive the proxy's HTTP parser, broker authentication and topic authorization. On a fresh Azure VM the network security group also ends with a default deny-all inbound rule, so the test means nothing until an allow rule for the chosen port exists at both the cloud and guest firewall layers.

Which port does MQTT use, and which one should I open?

The protocol defines 1883 for MQTT over plain TCP and 8883 for MQTT over TLS; WebSocket deployments usually sit behind the standard web ports instead. Open only the single port the selected path needs, scoped to your plant's source addresses, and keep the broker listener on loopback or a private interface when a proxy fronts it.

Does the Azure IoT Hub forward fail for the same reason?

No. The PLC's MQTT session and the broker's outbound connection to IoT Hub are two independent links. In the posted code the forwarding call iotClient.sendEvent runs only after a publish arrives, and the helpers iotClient and iotResult are not defined in the snippet shown, so verify how the hub client is constructed before reading a delivery failure as a connection failure.

How do I confirm the broker actually accepted the PLC?

Watch the broker side, not the network side. The posted aedes.on('publish') callback fires with a non-null client only after CONNECT and CONNACK have completed, so a silent callback alongside a chatty capture means the session is still dying upstream. On the wire, a successful handshake ends with a CONNACK whose return code is 0; a socket that closes right after CONNECT points back at the listener in front of the broker.

Can I just keep the port 80 block and add the WebSocket headers?

Those headers only forward an upgrade the client itself requested; they cannot create one. A native MQTT client never sends an HTTP upgrade, and the Node server shown attaches Aedes to a plain TCP listener that cannot answer one either. Both ends need WebSocket support - the client firmware and a WebSocket-capable broker listener - before that block serves MQTT.

Keep reading

  • How-to

    Bridge ScadaBR data to MQTT through Node-RED

    In this ScadaBR 1.2 design nothing on the ScadaBR side speaks MQTT, so Node-RED bridges both directions: the HTTP Sender passes point values as GET parameters to a /nodered HTTP input, a switch on msg.parts.key publishes them to topics BIN, INT and FLOAT, and a join feeds them back as GET query parameters to the HTTP Receiver at /ScadaBR/httpds. Match the BIN, INT and FLOAT names everywhere.

    Edge computing and IIoT

  • How-to

    Bridge a cellular tracker's TCP stream into MQTT for AWS IoT Core

    Build a small ingest service on your edge PC: it terminates the tracker's raw TCP session, decodes each stored record, and republishes one MQTT message per fix over TLS on port 8883. Acknowledge a packet only after every publish is confirmed, or a coverage-gap batch is silently lost.

    Edge computing and IIoT

  • Comparison

    Cloud IIoT to Ignition data integration options compared

    To push cloud machine data into a customer's Ignition install, choose a subscription route: publish MQTT with the Sparkplug payload to a broker the customer designates, and let them subscribe through the MQTT module they add. An OPC UA server interface is the stricter alternative. A JSON/XML web API only works with customer scripts that poll.

    Edge computing and IIoT