Reverse proxy using NGINX

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.

3 Likes