Reverse Proxy MQTT Through NGINX

I know I’m replying to an old post. Hopefully this is helpful to someone.

I just started using OpenHAB and MQTT. One way I found to proxy MQTT through nginx is to use websockets instead of MQTT. This works with OwnTracks (after configuring OwnTracks to use websockets instead of MQTT). I don’t have any IoT devices that use MQTT so I don’t know if it’s possible to make this work for those devices.

In mosquitto.conf, add the following to use websockets instead of MQTT

listener 9002
protocol websockets

Restart mosquitto to confirm this works.

Then update your nginx site to reverse proxy websockets connections (you could also do this without SSL, but this should make it ready to expose to the internet).

server {
   listen                          443 ssl;
   server_name                     <server name>;
   add_header                      Strict-Transport-Security "max-age=31536000; includeSubDomains";
   ssl_certificate                 <SSL cert location>;
   ssl_certificate_key             <SSL key location>;

   location / {
      proxy_http_version                        1.1;
      proxy_set_header          Upgrade         $http_upgrade;
      proxy_set_header          Connection      "upgrade";
      proxy_pass                                http://localhost:9002/;
    }
}

Restart nginx and you should be able to access MQTT through nginx.