I am trying to get Apache 2.4.7 on a Ubuntu 16.04 server to proxy and reverse proxy a connection to a local Mosquitto MQTT broker via WebSockets.
libwebsockets
is installed and Apache hasmod_proxy_wstunnel
andmod_websocket_mosquitto
available and enabled. I have been trying to follow this and this, but with little luck.The Mosquitto broker is working locally and I can pub/sub it from an SSH terminal using
mosquitto_pub
andmosquitto_sub
using the credentials I created, but is TLS disabled.I have tried a couple of ways of getting it working; I have tried to get Apache to listen on a port and setup a vhost on that port to proxy and reverse to the local port first using
mod_proxy_wstunnel
( the top 2 commented lines) and then usingmod_websockets_mosquitto
like this:<virtualhost *:1888> #ProxyPass / ws://127.0.0.1:1884 keepalive=on retry=360 #ProxyPassReverse / ws://127.0.0.1:1884 keepalive=on <IfModule mod_websocket.c> #Loadmodule mod_websocket_mosquitto /usr/lib/apache2/modules/mod_websocket_mosquitto.so <Location /> MosBroker localhost MosPort 1884 SetHandler websocket-handler WebSocketHandler /usr/lib/apache2/modules/mod_websocket_mosquitto.so mosquitto_init </Location> </IfModule> </virtualhost>
No luck there.
I then tried the same thing but with a subdomain and port 80:
<virtualhost *:80> ServerName sub.domain.com ServerAdmin some@guy.com DocumentRoot "/var/www/public_html/subdomain" #ProxyPass / ws://127.0.0.1:1884 keepalive=on retry=360 #ProxyPassReverse / ws://127.0.0.1:1884 keepalive=on <IfModule mod_websocket.c> #Loadmodule mod_websocket_mosquitto /usr/lib/apache2/modules/mod_websocket_mosquitto.so <Location /mqtt> MosBroker localhost MosPort 1884 SetHandler websocket-handler WebSocketHandler /usr/lib/apache2/modules/mod_websocket_mosquitto.so mosquitto_init </Location> </IfModule> </virtualhost>
Same thing.
Access logs show a GET request using the latter method and nothing when using the former (Log directives are in the above snippets, but removed for the purpose of this question). Nothing displays in the Error logs.
Is there something that I’m missing? Maybe there is a better way around this?
Answer
I have stumbled opon the same problem and also tried to use mod_websocket_mosquitto to get this to work, but without success.
What worked for me was to use proxy_wstunnel directly (following this short article – I did not encounter the mentioned issues in the article, they should be fixed by now):
First I told apache to listen on port 1883 to accept the mqtt requests (the default config file for this is ports.conf
). Of course you can chose basically any port, as long as your client knows where to connect to.
Listen 1883
Then I set up a simple VirtualHost to proxy incoming requests:
<VirtualHost *:1883>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /mqtt ws://$Broker-IP:$Broker-Port
ProxyPassReverse /mqtt ws://$Broker-IP:$Broker-Port
</VirtualHost>
This config will propably need some tweeks, but it allowed me to connect to my broker using my apache’s IP.
To test this, I used eclipse’s mqtt-spy which is a tad easier to use for manual publishing/subscribing than the command line 🙂
PS: And remember to tell mosquitto to use websockets as protocol in your mosquitto.conf
:
listener $Broker-Port
protocol websockets
Attribution
Source : Link , Question Author : aliaksei , Answer Author : Muto