Inhaltsverzeichnis

Unifi Network Application behind Nginx (local) reverse proxy

Assuming you have installed Unifi Network Application on a Linux host and you have a DNS A (and/or AAAA) record pointing to your.server.name (for STUN & inform to work),

you should be able to reach your UNA-WebUI under:

https:///your.server.name:8443

The following procedure will let your UNA-WebUI directly show up on:

https:///your.server.name (Just like a Unifi CloudKey Enterprise)

(making it easier for your people and not letting the server show a 404 when accessing without the correct port destination).

Make sure you have placed the correct HTTPS key and certificate in /etc/ssl/private/ and /etc/ssl/certs/

Else, please refer to SSL configuration in the nginx wiki linked in the Refernces before going through next steps.

Here you go:
Click on the code to copy to clipboard

Install nginx webserver

 sudo apt update; sudo apt install -y nginx nginx-extras 

(or install it with the use of the package manager of your distro!)

Configure your local nginx reverse proxy

Kick the default site configuration of your nginx by deleting the link:

sudo rm /etc/nginx/sites-enabled/default 


Create a file for your unifi-application e.g.:

sudo touch /etc/nginx/sites-availible/unifi 


and link it to /etc/nginx/sites-enabled/unifi

 sudo ln -s /etc/nginx/sites-availible/unifi /etc/nginx/sites-enabled/unifi


Edit your created file by copying the template below and adjusting it to your environment:

sudo nano /etc/nginx/sites-availible/unifi


(replace what is between » « for your case; do not keep the » «)

unifi
# unifi reverse proxy

upstream unifi {
  server 127.0.0.1:8443;
}


# send http request to https

server {
  listen 80;
  listen [::]:80;
  server_name >>your.server.name<<;
  rewrite ^(.*) https://$server_name$request_uri? permanent;
}


server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name >>your.server.name<<;
  # SSL parameters
  # params
  include /etc/nginx/conf.d/ssl;		
  # certs
  ssl_certificate /etc/ssl/certs/>>your cert here<<;
  ssl_certificate_key /etc/ssl/private/>>your key here<<;
  # log
  access_log /var/log/nginx/unifi.access.log;
  error_log /var/log/nginx/unifi.error.log;

  # Redirect requests to unifi backend server
  location  /  {
         proxy_pass      https://unifi/;
         proxy_redirect  https://unifi/ /;
         proxy_buffering off;

         proxy_read_timeout 60s;

         proxy_set_header          Host            $host;
         proxy_set_header          X-Real-IP       $remote_addr;
         proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;

         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "Upgrade";
  }
 
}



Test your nginx config with:

sudo nginx -t


The file /etc/nginx/conf.d/ssl in my case contains:

ssl_session_timeout 1h;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-SHA384;
ssl_ecdh_curve secp384r1;

This should be tight enough for the time being, but can also be adjusted in any case for the future.

Restart your nginx webserver to load the new configuration:

sudo systemctl restart nginx.service
or:
sudo service nginx restart


Placing the UNA behind a distant reverse proxy

If the Ubiquiti devices should communicate to the UNA via the distant reverse proxy, it is also possible (but was not my intention).
You just have to modify the upstream unifi { server your.server.ip.here:8443; } section in the nginx configuration above.
BUT:
You must then also proxy STUN (3478/udp) and inform (8080/tcp) with a stream { … } section within the nginx configuration.
You will find more information about this in the References.
Please refer to them for more in that case.

Enjoy!

References:
https://www.nginx.com/resources/wiki/
https://community.ui.com/questions/All-unifi-controller-ports-behind-reverse-proxy/fc52545a-6dbe-4ded-9ad4-de0ac4e68491