This configuration is for running mosquitto with SSL terminated in nginx. I did it because I have exposed a standalone instance of mosquitto to the internet. If you don’t do this it isn’t necessary.
My setup is based on a manual install of hass so I don’t really know what Hasbian uses nginx for - again I terminate SSL for hass on nginx on my proxy server because I’ve exposed it.
You can install gunzip with apt install gunzip on any Debian install. I’m not sure why perl-pcre didn’t work although it looks like it isn’t needed, so try without.
It just is too complicate for me, I do not understand half of it. My only need is too use mosquitto and other services too, using nginx (since I am not able to use mosquitto with SSL)
So, I ran into a few issues and it took me a bit to work it out, but I got it. I’m also using nginx for the main Home Assistant page so I can do Alexa integration (which is awesome, can’t believe I put it off so long). At first I was trying to stick mosquitto in the sites-available like you did, but that directory by default is for HTTP connections, like websites. So it works for the Home Assistant site, but not for throwing the stream thing in there for MQTT, get errors about stream object in a bad spot. Ended up poking around your linked guide and just adding the stream config after the http config in the main nginx.conf file.
My other problem was I kept getting errors about “the shared memory zone ‘SSL’ is already declared for a different use in /etc/nginx/nginx.conf.” So, I removed all the SSL config stuff from the stream config. This produced a new error “no ‘ssl_certificate’ is defined in server listening on SSL port while SSL handshaking”. So, I went back and added in the ssl_certificate, ssl_certificate_key, and dhparam parameters to the stream config. They point to the same files I used for my main site access.
This is what I ended up with:
stream {
upstream mosquitto {
server servername:1883; # My MQTT server isn't on the same server
}
server {
listen 8883 ssl;
proxy_pass mosquitto;
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/cert.key;
ssl_dhparam /etc/nginx/ssl/dhparams.pem;
}
}
Just wanted to share in case anyone else runs into those issues.
Note, I’m using a Ubuntu VM for Home Assistant and Nginx (same server) and CentOS VM for Mosquitto. So, I didn’t have to do anything special to use the stream directive. I’m also using Zanzito with the OwnTracks emulation with no issues. And I previously bought a wildcard SSL cert through ssl2buy.com (fairly cheap if you want to go that option vs letsencrypt or something similar).
UPDATE:
I did get it to work. For me the keys was learning where the hass.io add-on certbot stores the cert files. Also the mqtt config defaulted to the /ssl directory, so I had change my file path accordingly.
Here is my config …
I had the test_user for when I had 1883 port forward testing owntracks without having to fight TLS. I removed it after I got TLS (8883) working.
Also the cert that needs to be loaded on the owntracks client is in the same directory.
I copied the cert.perm to a different directory, then renamed it to mqtt_cert.perm.
I may play with and see if HA can connect TLS, then I can turn off 1883.
@lizaoreo
I followed your instructions and added the stream to .conf.
Seems to be working (dont know how to really test it but nginx is starting at least) but I`m having problem to connect with Zanzito.
Did you import the cert on your phone or how could you connect secure from Zanzito?
Also, how is the proxy_pass used?
Are you using a purchased SSL cert for your domain? I actually purchased a wildcard cert for the domain I own, so it might be a little different than if you use something like one of the common solutions people use here.
If I understand how NGINX works (I honestly don’t really), the proxy_pass line points to the upstream line, which points to your server for MQTT. In my example above, my MQTT server would be named servername (change to localhost if on same server as NGINX) and it would be listening on port 1883 for MQTT traffic. NGINX is listening on port 8883 for MQTT traffic, so when you hit the NGINX server, you want your traffic to be on port 8883.
In Zanzito, my MQTT connection settings are basically this:
Host: NGINX Server Name (ie, mqtt.homedomain.com)
Port: 8883
Security:
Username: MQTT Username (this is whatever you set up for mosquito, not NGINX/HomeAssistant)
Password: MQTT Password
SSL/TLS Connection: Checked
Don't worry about import CA I'm pretty sure (I didn't need to).
Everything else is variable based on preference (I have instant updates set, heartbeat enabled, and intervals at 90seconds). I don’t use Home host settings as I set up the same domain name environment at home and forward the outside name to the server, so basically same address hits server inside and outside.
Let me know if that helps. If you need help with the mosquito server, let me know and I drop my config in and explain it.