Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example, working, NGINX config for proxying to Unifi Controller software and using letsencrypt. Includes websocket fix.
# I had a bit of trouble getting my unifi controller (hosted offsite) to use a proxy/letsencrypt. So here are the fruits of my labor.
# The unifi default port is 8443 running on localhost.
# License: CC0 (Public Domain)
server {
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name unifi.hostname.com;
# Needed to allow the websockets to forward well.
# Information adopted from here: https://community.ubnt.com/t5/EdgeMAX/Access-Edgemax-gui-via-nginx-reverse-proxy-websocket-problem/td-p/1544354
location /wss/ {
proxy_pass https://localhost:8443;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location / {
proxy_pass https://localhost:8443/; # The Unifi Controller Port
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
}
# Unifi still internally uses its own cert. This was converted to PEM and
# is trusted for the sake of this proxy. See here for details:
# https://community.ubnt.com/t5/UniFi-Wireless/Lets-Encrypt-and-UniFi-controller/td-p/1406670
ssl_trusted_certificate /etc/nginx/ssl/unifi/unifi-default-selfsign.pem;
ssl_certificate /etc/letsencrypt/live/unifi.hostname.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/unifi.hostname.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
}
server {
listen 80;
listen [::]:80;
server_name unifi.hostname.com;
location / {
return 301 https://$host$request_uri;
}
}
@dikshant

This comment has been minimized.

Copy link

@dikshant dikshant commented Dec 30, 2017

Thanks for this! It was incredibly helpful.

@epacke

This comment has been minimized.

Copy link

@epacke epacke commented Jul 8, 2018

Thank you very much for sharing!

@pironic

This comment has been minimized.

Copy link

@pironic pironic commented Nov 20, 2018

also helped me. ty!

@tongphe

This comment has been minimized.

Copy link

@tongphe tongphe commented Jan 14, 2019

This one works for me

location /ws {
	proxy_pass https://localhost:8443;;
	proxy_http_version 1.1;
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "Upgrade";
	proxy_set_header Proxy "";
	proxy_set_header Host $http_host;
}
@bluenevus

This comment has been minimized.

Copy link

@bluenevus bluenevus commented Feb 21, 2019

thank you thank you thank you!!!!

@jimmarks

This comment has been minimized.

Copy link

@jimmarks jimmarks commented Jun 18, 2019

This worked for me with one tweak. I am using a stand-alone nginx server for multiple sites. I ended up changing the proxy_pass lines to direct them to my actual internal IP Address.

The rest of this setup worked amazingly.

THANK YOU!!!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Oct 16, 2019

works a fuckin' treat. thx a ton.

@frankhommers

This comment has been minimized.

Copy link

@frankhommers frankhommers commented Oct 28, 2019

This does not work for inform-urls.

@roinou

This comment has been minimized.

Copy link

@roinou roinou commented Feb 3, 2020

Mine was working without the wss part, I'll see with time if it improves or not.
Nevertheless, I still get HTTP 413 when uploading large backups (>1Mb). It seems to be nginx, as there are no errors on the unifi part. Adding this does the trick: client_max_body_size 10M;

@Ramblurr

This comment has been minimized.

Copy link

@Ramblurr Ramblurr commented Mar 3, 2020

The https is very important the proxy_pass https://localhost:8443/ line. I was copy/pasting from an old config and kept getting
the error: This combination of host and port requires TLS. I had forgotten to change the http -> https.

@robwolff3

This comment has been minimized.

Copy link

@robwolff3 robwolff3 commented Apr 14, 2020

After a bunch of trial and error the configuration below is finally how I got the web socket errors to stop. Its a hybrid of my own, @vidia and @tongphe I have the server block repeated twice on different ports for my guest portal which I'm also proxying through nginx.

server {
        listen 443 ssl http2;
        server_name unifi.domain.com;
        include /config/nginx/ssl.conf;
        ssl_trusted_certificate /config/nginx/unifi.pem;
        location /ws {
                proxy_pass https://192.168.xxx.xxx:8443;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
	        proxy_set_header Host $http_host;
        }
        location / {
	        proxy_pass https://192.168.xxx.xxx:8443/;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header Host $http_host;
	        proxy_set_header X-Forwarded-Proto $scheme;
	        proxy_set_header X-Real-IP $remote_addr;
	        proxy_set_header Upgrade $http_upgrade;
	        proxy_set_header Connection "Upgrade";
	        proxy_buffering off;
	        proxy_ssl_verify off;
        }
}
server {
        listen 8843 ssl http2;
        server_name unifi.domain.com;
        include /config/nginx/ssl.conf;
        ssl_trusted_certificate /config/nginx/unifi.pem;
        location /ws {
                proxy_pass https://192.168.xxx.xxx:8844;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
	        proxy_set_header Host $http_host;
        }
        location / {
	        proxy_pass https://192.168.xxx.xxx:8844/;
	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header Host $http_host;
	        proxy_set_header X-Forwarded-Proto $scheme;
	        proxy_set_header X-Real-IP $remote_addr;
	        proxy_set_header Upgrade $http_upgrade;
	        proxy_set_header Connection "Upgrade";
	        proxy_buffering off;
	        proxy_ssl_verify off;
        }
}
@blitzkrieggold

This comment has been minimized.

Copy link

@blitzkrieggold blitzkrieggold commented Apr 25, 2020

I took the original and added this line to the location /wss/ section and it seems to work.
proxy_set_header Origin '';

@DomenicoCasillo

This comment has been minimized.

Copy link

@DomenicoCasillo DomenicoCasillo commented May 19, 2020

I took the original and added this line to the location /wss/ section and it seems to work.
proxy_set_header Origin '';

The above is perfectly fine.

@rexkani

This comment has been minimized.

Copy link

@rexkani rexkani commented May 28, 2020

Hi guys,

may i know if this code works for AP adoption "inform"?

@azN2

This comment has been minimized.

Copy link

@azN2 azN2 commented May 31, 2020

Hi guys,

may i know if this code works for AP adoption "inform"?

https://domain/inform

what is everyone doing about the STUN warning, have you been able to solve this?

@vidia

This comment has been minimized.

Copy link
Owner Author

@vidia vidia commented Jun 1, 2020

Thank you to everyone who has responded to this. I will have to go through all of these suggestions and take in whatever new changes are working best for people. I have the posted code deployed currently and haven't noticed any issues, but it is possible I've missed something because I haven't updated it since I posted it initially.

@azN2, I do have STUN issues on my box, but my issues seem to be because STUN is binding to ipv6 and not ipv4, which is causing it to not be accessible over ip4 like I want it to be. I haven't found a solution to force it to use ipv4, but I'm mentioning it here in case that bit of info helps you find your own solution.

@rexkani, I believe this will work for /inform. I don't believe that will be anything special or different than accessing the controller regularly.

@rexkani

This comment has been minimized.

Copy link

@rexkani rexkani commented Jun 2, 2020

hi @azN2 and @vidia, the code did not work for STUN and inform, these traffics does not run on HTTPS and therefore needs to make use of the nginx "Stream" module. i've leart that from below discussion:

another thing that is still missing after adding the stream for STUN and inform is the remote guest captive portal. i seems to be running on 8880? i dont have time to work on it yet.

@systemofapwne

This comment has been minimized.

Copy link

@systemofapwne systemofapwne commented Jun 18, 2020

Thank you. You directed me into the right direction. Here is "my snippet", which basically enables websockets & httpupgrade on / (catchall) instead of splitting between / and /wss. The reason is, that more locations than just /wss need http upgrade

server {
	listen 443 ssl http2;
	listen [::]:443 ssl http2;
	
	server_name MY.DOMAIN;
	
	#SSL/TLS config
	include /PATH/TO/SSL.CONF;
	
	location / {
	        #Unifi ip
	        proxy_pass https://10.1.0.106:8443/;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                proxy_ssl_verify off; # Ignores the unifi controller's self-signed certificate (potentially allows MITM - Add the certificates, as shown for the other examples above, in order to mitigate this attack vector)
        
        	#"Upgrade" for WebSockets
                proxy_http_version 1.1;
                proxy_buffering off;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_read_timeout 86400;
        }
}

As a little sidenote: If you plan to use HTTP Auth, you need to block forwarding of the auth-data to the unify-controller. Otherwise you will end up with a 400: Bad Request error.

Simply add this line to the location block

proxy_set_header Authorization "";

@MaximvanLuttikhuizen

This comment has been minimized.

Copy link

@MaximvanLuttikhuizen MaximvanLuttikhuizen commented Aug 7, 2020

I took the original and added this line to the location /wss/ section and it seems to work.
proxy_set_header Origin '';

This also worked for me. The errors in the user interface disappeared. The return code in Nginx changed from 404 to 101 (Switching protocol).

@filupmarley

This comment has been minimized.

Copy link

@filupmarley filupmarley commented Aug 24, 2020

The https is very important the proxy_pass https://localhost:8443/ line. I was copy/pasting from an old config and kept getting
the error: This combination of host and port requires TLS. I had forgotten to change the http -> https.

This was my issue as well. This fixed it for me!

@jleboube

This comment has been minimized.

Copy link

@jleboube jleboube commented Oct 19, 2020

I realize this is for self hosted controller, however, can anyone let me know if you've been able to get nginx reverse proxy working with a Cloud Key Gen 2 Plus? I am able to get to a login screen, but when UniFi attempts to perform the two factor auth, it bombs out. Any help would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.