I want to be able to use nginx to reverse proxy (I don’t understand why it’s called “reverse”) to the mosquito aka mqtt add-on so that I can use mqtt.example.com to connect to the broker using SSL. I want nginx to use route all traffic from port 80 to port 443. In other words I don’t want to open new ports for mqtt. The clients don’t specify a port and they get sent to the SSL port.
The following steps are how I got to the point where I’m able to access mosquitto over port 80. However, I’m stuck on getting it even work on SSL, much less having it routed there. With regualr websites, I’m able to get a Let’s Encrypt SSL certificate installed, as well as routing to 443 for SSL by using CertBot. It usually takes care of everything for you.
sudo certbot --nginx -d mqtt.example.com
However that isn’t working for me, and I don’t know why. I’ve tried both Certbot options for allowing HTTP or forcing HTTPS and neither work. In fact, when I try the “both” option I can’t connect with the test service anymore. Here are the steps I took to set things up. Any help would be appreciated, but I’d really like to make it work using CertBot because that will be updating the certs. Thanks for the use of your eyes.
I put this together from reading the docs and some of the suggestions from this thread and help from @rbray89.
Steps to get mqtt.example.com working with nginx
Install and configure mosquitto add-on broker
Use hassio to install the mosquitto addo-on. Update the configuration settings in the UI. The changes to the default are setting plain_websockets to *true, anonymous to false and adding a login username/password set.
{
"plain": true,
"plain_websockets": true,
"ssl": false,
"ssl_websockets": false,
"anonymous": false,
"logins": [
{
"username": "foo",
"password": "bar"
}
],
"customize": {
"active": false,
"folder": "mosquitto"
},
"certfile": "fullchain.pem",
"keyfile": "privkey.pem"
}
Configure mqtt component in configuration.yaml.
Modify the configuration.yaml to setup the mqtt component.
mqtt:
client_id: home-assistant-1
username: foo
password: bar
broker: 127.0.0.1
Create nginx server block
At this point, make sure you have nginx installed. This guide is a pretty good walkthrough to installing nginx, a firewall, and creating a test site. This is not using the nginx addon, but rather is a seperate service. I have mine running on a different machine than what is running hassio. Make test site and use certbot (described in the guide) to make sure that http://test.example.com get’s redirected to https://test.example.com. After you have that test working, create a server block for mqtt:
sudo nano /etc/nginx/sites-enabled/mqtt.example.com
server {
server_name mqtt.example.com;
listen 80;
location /
{
proxy_pass http://172.16.68.67:1884; #address of home assistant machine
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Validate the server block
sudo nginx -t
Restart service
sudo service nginx restart
Use HiveMQ to test externally. Use mqtt.example.com as the host, use port 80, and specify the username and password.
Use CertBot to create and install a cert and modify the server block
sudo certbot --nginx -d mqtt.example.com
Unfortunately this last step not only doesn’t work, it breaks port 80 working. @rbray89, do you have any idea of what I’m missing?