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

server {
  listen *:80;
  server_name           homeassistant.realm.local;
  #index  index.html index.htm index.php;
  #access_log            /var/log/nginx/homeassistant.realm.local.access.log combined;
  #error_log             /var/log/nginx/homeassistant.realm.local.error.log;
  location / {
    return 301 https://$server_name$request_uri;
  }
}

server {
  listen       *:443 ssl;

  server_name  homeassistant.realm.local;

  ssl_certificate           /etc/nginx/ssl/certs/homeassistant.realm.local/cert.pem;
  ssl_certificate_key       /etc/nginx/ssl/certs/homeassistant.realm.local/key.pem;
  ssl_protocols             TLSv1.2;
  ssl_ciphers               ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK;

  # Ensure this line points to your dhparams file
  ssl_dhparam /etc/nginx/ssl/dhparams.pem;
  
  index  index.html index.htm index.php;
  access_log            /var/log/nginx/ssl-homeassistant.realm.local.access.log combined;
  error_log             /var/log/nginx/ssl-homeassistant.realm.local.error.log;

  proxy_buffering off;

  location / {
    proxy_pass            http://192.168.0.40:8123;
    #proxy_read_timeout    90s;
    #proxy_connect_timeout 90s;
    #proxy_send_timeout    90s;
    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;
  }
}
