Skip to content

Instantly share code, notes, and snippets.

@dlangille
Last active January 17, 2022 18:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dlangille/f93b2edf91da919a8ad1410671c9dddc to your computer and use it in GitHub Desktop.
Save dlangille/f93b2edf91da919a8ad1410671c9dddc to your computer and use it in GitHub Desktop.
nginx reverse proxy for unifi controller, so you can put a real cert in front of it.
# listen on port 80, and redirect to port 443
server {
listen 10.0.0.131:80;
server_name unifi01.int.unixathome.org;
error_log /var/log/nginx-unifi01.int.unixathome.org.error.log info;
access_log /var/log/nginx-unifi01.int.unixathome.org.access.log combined;
return 301 https://$server_name$request_uri;
}
server {
listen 10.55.0.131:443 ssl http2;
server_name unifi01.int.unixathome.org;
client_max_body_size 50m;
root /usr/local/www/unifi01.int.unixathome.org;
error_log /var/log/nginx-unifi01.int.unixathome.org.error.log info;
access_log /var/log/nginx-unifi01.int.unixathome.org.access.log combined;
ssl_certificate /usr/local/etc/ssl/unifi01.int.unixathome.org.fullchain.cer;
ssl_certificate_key /usr/local/etc/ssl/unifi01.int.unixathome.org.key;
location / {
proxy_pass https://unifi01.int.unixathome.org:8443/;
proxy_redirect https://unifi01.int.unixathome.org:8443/ /;
proxy_buffering off;
proxy_read_timeout 60s;
# May not need or want to set Host. Should default to the above hostname.
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";
}
}
@dlangille
Copy link
Author

Lines 43-45 avoid the 'Websocket connection error' messages. Those message coincide with Javascript console messages such as:

components.manage.js?v=5.6.24.0:215 WebSocket connection to 'wss://example.org/wss/s/super/events' failed: Error during WebSocket handshake: Unexpected response code: 400
n.open @ components.manage.js?v=5.6.24.0:215

The original revision of this file separately proxied /wss ; I found that to be unnecessary.

@dok18
Copy link

dok18 commented Dec 13, 2018

@dlangille any ideas how to use subfolder location for this?

location  /unifi {
    ....
}

returns 404 for me.

@ryman1
Copy link

ryman1 commented Oct 5, 2021

Thank you for this! All the other advice out there seemed to not fix the websocket error you mentioned.

@dlangille
Copy link
Author

@dlangille any ideas how to use subfolder location for this?

location  /unifi {
    ....
}

returns 404 for me.

Sorry, I only just saw this. Is it working yet for you?

@dlangille
Copy link
Author

Thank you for this! All the other advice out there seemed to not fix the websocket error you mentioned.

I'm happy it helps you. This solution is still in use at my place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment