Nginx configuration as reverse proxy under a different url

I’m trying to expose Hass.io to Alexa in order to control my device with an echo dot.
In order to do that, I do have 2 raspberry pies connected to my network:

  • 1 Hass.io instance (listening to 192.168.x.x:8123 - default configuration)
  • 1 nginx instance which is also running duckdns scripts and certbot (listening to 192.168.x.y on ports 80 and 443)

Ports 80 and 443 are forwarded on my router to the nginx server.
so far so good…

I trying to configure nginx to act as a reverse proxy for Home Assistant (but on a different url). The idea is to have home assistant answer to calls to https://zzzz.duckdns.org/hass (using / hass because I don’t want to consume the whole url space with Home Assistant only).

My nginx configuration looks like this:
# Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;

	#root /var/www/html;

	#index index.html index.htm index.nginx-debian.html;

	server_name zzz.duckdns.org;

	#location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
	#	try_files $uri $uri/ =404;
	#}

    listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/zzzz.duckdns.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/zzzz.duckdns.org/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


    if ($scheme != "https"){
        return 301 https://$host$request_uri;
    } # managed by Certbot

    location /hass {
        proxy_buffering off;
        proxy_pass http://192.168.x.y: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;
    }
}

When I connect to https://zzzz.duckdns.org, the page is loading fine, but all js scripts are failing to load (http 404 errors), which make sense because the calls are made to https://zzzz.duckdns.org/frontend_es5/core-6a354e5eb94e5c8a2f58a0a18f948058.js (for instance).

I believe I should add some configuration to my hass.io configuration to generate the correct html (calling for /hass/… instead of /…)

Any help will be greatly appreciated!

I understand what your doing, but why not use the new cloud platform for Alexa integration?

You couldn’t do this when I last checked and I’m yet to see HA offer any ability to change it. Based on past comments by the core developers I doubt it’ll ever change. Your best bet is to buy a cheap domain and make as many subdomains as you need.

I bought a country code domain (.us for me) for $0.99 for the first year, and then just bought 9 more years for under $60 and I currently run 6 or 7 subdomains on it. Everything gets basic auth and SSL with NGINX and fail2ban monitors the nginx logs.

Nginx only allow forward single domain to same IP and Port (at least I never figured out how). This mean forward to files on 192.168.x.y is ok but anywhere on 192.168.x.x will not work.

Add subdomain…https://hass.zzzz.duckdns.org to forward to another server or Port not https://zzzz.duckdns.org/hass

To be clear, this is only true because you initially forward https://zzzz.duckdns.org to 192.168.x.y in the config file, not because nginx is hosted on x.y . If you forward only to 192.168.x.x in config it will be OK.

Thanks for the advice. I just discovered it while looking around the forums. It’s defintitely worth the try.

This is ‘kind of’ true. As long as I have only one public IP address, registrering it under hass.xxx.duckdns.org and/or xxx.duckdns.org will essentially be the same thing. I could probably segregate the request based on the host header…

This may be helpful if you want to use multiple server or location blocks. It is what I used to help create both my server and location blocks.