Quick update on this. The solution is documented here: http://frankfurtlovesyou.com/posts/mqtt-bridge-with-mosquitto-and-nginx.html.
The stream directive needs nginx 1.9 and above. If you’re using Raspbian this isn’t available in jessie so I had to compile nginx from source - this is pretty straightforward even on a Pi Zero.
- Download the current STABLE source from Nginx.org: http://nginx.org/download/nginx-1.12.0.tar.gz
- Unzip in a user directory with
gunzip -zxvf
- cd to the directory
- Install prequisites - this is what I needed from configure complaining as I kept trying:
sudo apt install perl-pcre libpcre3-dev libssl-dev
- Configure with this string:
./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-stream --with-stream_ssl_module
This is a very basic configuration. The important bit is --with-stream --with-stream_ssl_module.
- Compile with
make && make install
Nginx will be installed in /etc/nginx. Configuration is as the documentation above which I created in /etc/nginx/sites-available/mosquitto:
stream {
upstream mosquitto {
server localhost:1883;
}
server {
listen 8883 ssl;
proxy_pass mosquitto;
ssl_certificate /etc/dehydrated/certs/example.com/fullchain.pem;
ssl_certificate_key /etc/dehydrated/certs/example.com/privkey.pem;
# from https://cipherli.st/
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
}
}
The SSL configuration is optional but desirable especially if you are exposing the port to the outside world. It may be advisable to generate your dhparam key on another machine, as it takes a while on a Pi. 15 hours or so.
Enable by linking in sites-enabled:
cd /etc/nginx/sites-enabled
ln -s …/sites-available/mosquitto .
To create your SSL certificates I use dehydrated (https://dehydrated.de/), which is a bash LetsEncrypt client which has built in support for DNS validation through assorted modules which is handy for machines that you don’t want to expose to the Internet too much. Follow the instructions on the git site to make them work for you.
Test nginx with nginx -t
What you won’t have at this point is a service file for nginx as in a package so here it is:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Copy this to /lib/systemd/system/nginx.service
and run:
systemctl enable nginx
systemctl unmask nginx
systemctl start nginx
And you should now have a proxy listening on 8883.
For reference this is my mosquitto.conf, nothing special in it:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
connection_messages true
log_dest file /var/log/mosquitto/mosquitto.log
password_file /etc/mosquitto/passwd
include_dir /etc/mosquitto/conf.d
This connects to Owntracks and updates although some more logging might be handy in the debug stage.