Reverse proxy using NGINX

Tags: #<Tag:0x00007f739a5dc090> #<Tag:0x00007f739a5e3e58>

:warning: This guide has been migrated from our website and might be outdated. Feel free to edit this guide to update it, and to remove this message after that.

Using NGINX as a proxy for Home Assistant allows you to serve Home Assistant securely over standard ports. This configuration file and instructions will walk you through setting up Home Assistant over a secure connection.

1. Get a domain name forwarded to your IP

Chances are, you have a dynamic IP address (your ISP changes your address periodically). If this is true, you can use a Dynamic DNS service (like duckdns) to obtain a domain and set it up to update with you IP.
If you later purchase your own domain name, you will be able to easily get a trusted SSL certificate later.

2 Install NGINX on your server

This will vary depending on your OS. Check out Google for this.
On a Raspberry Pi, this would be:

sudo apt-get install nginx

After installing, ensure that NGINX is not running.

:information_source: You will at least need NGINX >= 1.3.13, as WebSocket support is required for the reverse proxy.

3. Obtain an SSL certificate

There are two ways of obtaining an SSL certificate.

Using Let’s Encrypt

If you purchased your own domain, you can use https://letsencrypt.org to obtain a free, publicly trusted SSL certificate. This will allow you to work with services like IFTTT. Download and install per the instructions online and get a certificate using the following command.

sudo ./letsencrypt-auto certonly --standalone -d example.com -d www.example.com

Instead of example.com, use your domain. You will need to renew this certificate every 90 days.

Or - Using openssl

If you do not own your own domain, you may generate a self-signed certificate. This will not work with IFTTT, but it will encrypt all of your Home Assistant traffic.

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 9999
openssl rsa -in key.pem -out key.pem
sudo cp key.pem cert.pem /etc/nginx/ssl
sudo chmod 600 /etc/nginx/ssl/key.pem /etc/nginx/ssl/cert.pem
sudo chown root:root /etc/nginx/ssl/key.pem /etc/nginx/ssl/cert.pem

4. Create dhparams file

As a fair warning, this file will take a while to generate.
If you don’t have the ssl subdirectory, you can either create it, or update the config below to use a different folder.

cd /etc/nginx/ssl
sudo openssl dhparam -out dhparams.pem 2048

5. Install configuration file in NGINX

Create a new file /etc/nginx/sites-available/hass and copy the configuration file (which you will need to edit) at the bottom of the page into it.

:information_source: Some Linux distributions (including CentOS and Fedora) will not have the /etc/nginx/sites-available/ directory. In this case, remove the default server {} block from the /etc/nginx/nginx.conf file and paste the contents from the bottom of the page in its place. If doing this, proceed to step 7.

6. Enable the Home Assistant NGINX configuration

cd /etc/nginx/sites-enabled
sudo unlink default
sudo ln ../sites-available/hass default

7. Start NGINX

Double-check your new configuration to ensure all settings are correct and start NGINX.
On a Raspberry Pi, this would be done with:

sudo systemctl start nginx

When it’s working you can enable it to autoload with:

sudo systemctl enable nginx

8. Port forwarding

On your router, setup port forwarding (look up the documentation for your router if you haven’t done this before).
Forward port 443 (external) to your Home Assistant local IP port 443 in order to access via https.
Also forward port 80 to your local IP port 80 if you want to access via http.
Do enable LAN Local Loopback (or similar) if you have it.
Do not forward port 8123.

9. Configure Home Assistant

Home Assistant is still available without using the NGINX proxy. Restricting it to only listen to 127.0.0.1 will forbid direct accesses.
Also, Home Assistant should be told to only trust headers coming from the NGINX proxy. Otherwise, incoming requests will always come from 127.0.0.1 and not the real IP address.

In your configuration.yaml file, edit the http setting.

http:
  # For extra security set this to only accept connections on localhost if NGINX is on the same machine
  # Uncommenting this will mean that you can only reach Home Assistant using the proxy, not directly via IP from other clients.
  # server_host: 127.0.0.1
  use_x_forwarded_for: true
  # You must set the trusted proxy IP address so that Home Assistant will properly accept connections
  # Set this to your NGINX machine IP, or localhost if hosted on the same machine.
  trusted_proxies: <NGINX IP address here, or 127.0.0.1 if hosted on the same machine>

NGINX configuration (referred to earlier)

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    # Update this line to be your domain
    server_name example.com;

    # These shouldn't need to be changed
    listen [::]:80 default_server ipv6only=off;
    return 301 https://$host$request_uri;
}

server {
    # Update this line to be your domain
    server_name example.com;

    # Ensure these lines point to your SSL certificate and key
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Use these lines instead if you created a self-signed certificate
    # ssl_certificate /etc/nginx/ssl/cert.pem;
    # ssl_certificate_key /etc/nginx/ssl/key.pem;

    # Ensure this line points to your dhparams file
    ssl_dhparam /etc/nginx/ssl/dhparams.pem;


    # These shouldn't need to be changed
    listen [::]:443 ssl default_server ipv6only=off; # if your nginx version is >= 1.9.5 you can also add the "http2" flag here
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
    # ssl on; # Uncomment if you are using nginx < 1.15.0
    ssl_protocols TLSv1.2;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    proxy_buffering off;

    location / {
        proxy_pass http://127.0.0.1:8123;
        proxy_set_header Host $host;
        proxy_redirect http:// https://;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

Hello. Can any body tell me how can I use Asterisk/FreePBX and HA at the same time with NGINX. I have Ubuntu 20.04

hi, appreciate your work.

Can I somehow use the nginx add on to also listen to another port and forward it to another APP / IP than home assistant

I use different subdomains with nginx config. It’s pretty straight-forward:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    # Update this line to be your domain
    server_name myURL.com;

    # These shouldn't need to be changed
    listen [::]:80;
    return 301 https://$host$request_uri;
}

server {
    # Update this line to be your Home Assistant domain
    server_name ha.myURL.com;

    location / {
        proxy_pass http://internal_IP:8123;
        proxy_set_header Host $host;
        proxy_redirect http:// https://;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}


server {
    #my Personal webpage without a subdomain. Repeat this block with each subdomain as needed for your apps/servers
    server_name    myURL.com www.myURL.com;
    root           /var/www/myURL.com;
    index          index.html;
    try_files $uri /index.html;

    location ~ \.wav$ {
        add_header Content-Disposition "inline";
      }
}

Note, you’ll need to make sure your DNS directs appropriately. I personally use cloudflare and need to direct each subdomain back toward the root url. NGINX makes sure the subdomain goes to the right place. That DNS config looks like this:

Type | Name
AAAA | myURL.com
CNAME | ha
CNAME | www
…etc.

1 Like

I’m having an issue with this config where all that loads is the blue header bar and nothing else.

In Chrome Dev Tools I can see 3 errors of “Failed to load module script: The server responded with a non-JavaScript MIME type of “text/html”. Strict MIME type checking is enforced for module scripts per HTML spec.”

Looks like the proxy is not passing the content type headers correctly. Is there something I need to set in the config to get them passing correctly?