Skip to main content

Home Assistant

下载

官网文档

外网访问(结合frp、nginx、公网服务器)

note

配置完成后一定要彻底清空网页缓存重新登录,否则不会生效!!!如果修改完配置仍然无法生效,可以换个浏览器试试。

准备

  • 本地服务器(树莓派之类的也可以,我是用的废弃电脑)
    • frp
    • home assistant
  • 公网服务器(只要有公网IP即可,公网带宽最好在5以上)
    • frp
    • nginx

frp服务器配置

frps.ini
[common]
;tcp信号传输端口
bind_port = 7000
;接收http服务的端口
vhost_http_port = 8091
;用来验证是自己的信号的token
auth_token = token

# frp管理后台端口,请按自己需求更改
dashboard_port = 7500
# frp管理后台用户名和密码,请改成自己的
dashboard_user = your-user
dashboard_pwd = your-pwd
enable_prometheus = true

# frp日志配置
log_file = /var/log/frps.log
log_level = info
log_max_days = 3

frp 客户端配置

frpc.ini
[common]
; 你的远程服务器ip地址
server_addr = ***.***.***.***
; 远程的服务器开放信号端口
server_port = 7000
; 用来验证是你的服务器的token,和服务器上的保持一致
auth_token = your-token

[ha]
type = http
; 你的home assistant服务的IP
local_ip = 127.0.0.1
; 你的home assistant服务的端口
local_port = 8123
custom_domains = your domaim

Home Assistant配置

note

如果是docker方式拉取的Home Assistant,那么需要先进入容器内修改这一文件。

  1. 查看容器的ID
sudo docker ps -a
  1. 进入该容器
sudo docker exec -it <容器ID> /bin/bash
  1. 编辑文件
vi configuration.yaml
  1. 保存退出

w+q

  1. 退出容器
exit
  1. 重启容器
sudo docker restart <容器ID/名字>
configurations.yaml
http:
use_x_forwarded_for: true
ip_ban_enabled: true
cors_allowed_origins:
- https://example.com
trusted_proxies:
- 192.168.1.0/24 # 你开启Home Assistant的电脑的IP段
- ***.***.***.*** # 你公网服务器的IP地址
- 127.0.0.0/24 # 本地IP段
# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes

# Text to speech
tts:
- platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

Nginx 配置

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80;
#listen [::]:80 default_server;

# SSL configuration
#
listen 443 ssl;
#listen [::]:443 ssl default_server;

if ($scheme = http) {
rewrite ^(.*) https://$host$1 permanent;
}
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

# ssl证书地址
ssl_certificate /usr/local/nginx/cert/example.com/ssl.pem; # pem文件的路径
ssl_certificate_key /usr/local/nginx/cert/example.com/ssl.key; # key文件的路径

# 此地址用来作为代理frp,不需要任何的文件源
# root /var/www/example.com;

# Add index.php to the list if you are using PHP
# index index.html index.htm index.nginx-debian.html;

server_name example.com;

location / {
proxy_pass http://127.0.0.1:8091/; # frp web服务接收的IP和端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $remote_addr;
}

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}