前言
- 实现了HTTPS访问、隐藏端口号。
- Homeassistant安装在Docker
遇到的问题
在宝塔设置反向代理后直接访问报错:400: Bad Request
- Home-Assistant使用反向代理要设置白名单,不然会拦截。
设置白名单后能正常访问,但无法登录,登录会弹出:Unable to connect to Home Assistant.
- Home-Assistant要用到websocket,而宝塔默认的反代配置文件没有添加websocket支持。
正式笔记
宝塔方面操作
添加SSL支持
添加反代
修改反代配置文件(添加websocket支持)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
#PROXY-START/
location ~* \.(gif|png|jpg|css|js|woff|woff2)$
{
proxy_pass https://hass.xxx.cn:8123;
proxy_set_header Host hass.xxx.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
expires 12h;
}
location /
{
proxy_pass https://hass.xxx.cn:8123;
proxy_set_header Host hass.xxx.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
add_header Cache-Control no-cache;
}
#PROXY-END/
#PROXY-START/
location ~* \.(gif|png|jpg|css|js|woff|woff2)$
{
proxy_pass https://hass.xxx.cn:8123;
proxy_set_header Host hass.xxx.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
expires 12h;
}
location /
{
proxy_pass https://hass.xxx.cn:8123;
proxy_set_header Host hass.xxx.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
add_header Cache-Control no-cache;
}
#PROXY-END/
Home-Assistant方面的设置
获取反向代理源IP
Received X-Forwarded-For header from an untrusted proxy 172.17.0.1
A request from a reverse proxy was received from 172.17.0.1,but your HTTP integration is not set-up for reverse proxies
添加SSL支持和反代白名单
- 把
.crt
、.key
证书文件拷贝到config
目录
- 打开
configuration.yaml
,添加如下配置(视情况修改),然后重启服务:
http:
base_url: [域名]
ssl_certificate: [.crt文件]
ssl_key: [.key文件]
use_x_forwarded_for: true
trusted_proxies:
- 1.14.147.22
- 172.17.0.1
- 127.0.0.1
- ::1
- 上面最下面那几个ip就是白名单列表,可以把公网IP和本地IP也写上去。