• HomeAssistant快速使用教程三:树莓派安装nginx并对其进行灵活配置



    前言

    为什么要用nginx?现在hass已经用了8123,node-red用了1880,emqx用了18083。现在的接口太多了,我根本记不过来,所以用nginx进行管理访问。
    当前,因为是在树莓派上运行,arm架构在源码安装的时候各种依赖以及替代依赖搞得头大,再次选择docker安装。
    附上官方连接:官方链接

    安装nginx

    为了方便的修改配置文件等,所以使用的时候需要把一些关键文件,映射到本地文件夹。

    新建一个nginx用来做本地映射,我的路径是/home/pi/opt/nginx

    这里创建目录的时候直接创建到nginx文件夹就行了,保证nginx里面是空的,然后让他自己启动容器把容器中的文件复制到物理机文件夹,然后再次基础上我们再进行配置文件的修改。

    现在开始执行命令启动nginx。现在就可以等待docker拉取镜像了,最好docker换一下源,这样速度会快。

    sudo docker run -d --name nginx -p 10004:80 --net host nginx
    
    • 1

    现在访问80
    端口
    在这里插入图片描述
    看到这个说明成功了。
    现在容器里面已经有了默认的配置文件,现在复制出来到刚才创建的文件夹

    sudo docker container cp nginx:/etc/nginx /home/pi/opt
    
    • 1

    在这里插入图片描述

    正常的话你的物理机文件夹里已经有了。
    现在把刚才用到的中间商docker容器给删了

    sudo docker stop nginx
    sudo docker rm nginx
    
    • 1
    • 2

    你已经有了配置文件,现在就可以进行文件映射了

    sudo docker run --name nginx -d -p 10004:80 --net host -v /opt/nginx:/etc/nginx nginx
    
    • 1

    然后再看你的浏览器80端口,如果ok了。说明安装成功。

    配置nginx

    这里有个教程连接:菜鸟教程-nginx配置
    在这里我想实现的是访问http://localhost/hass的时候请求发到了http://localhost:8123。用到了反向代理功能。
    因为nginx配置读取conf.d文件夹下所有.conf结尾的配置文件,所以我们不动原生的,直接新建一个。
    在nginx/conf.d文件夹下有个default.conf直接复制进去
    这是我定义的配置文件:

    server {
        listen       80;
        listen  [::]:80;
        server_name  http://192.168.1.24/;
    
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        #最后加上/不然定位不到
        location /hass/ {
        #最后面加上/不然定位不到
            proxy_pass  http://192.168.1.24:8123/;
        }
        location /nodered/ {
            proxy_pass  http://192.168.1.24:1880/;
        }
        location /emqx/ {
            proxy_pass  http://192.168.1.24:1883/;
        }    
        location /docker/ {
            proxy_pass  http://192.168.1.24:9000/;
        }  
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    重启docker容器

    sudo docker restart nginx
    
    • 1

    现在就ok了

    可能出现的错误

    树莓派可能会出现80端口占用。
    一般来说你使用

    sudo losf -i:80 ##得到使用80端口的进程PID
    ps {PID} ##查看进程的详情信息
    
    • 1
    • 2

    命令就可以看到具体的端口使用的命令。我这里是apache2
    我直接

    sudo systemctl disable apache2.service
    
    • 1

    就给关了,然后重启就行了。

  • 相关阅读:
    北大肖臻老师《区块链技术与应用》系列课程学习笔记[6]比特币脚本
    财务小知识之名词解释
    关于NVIC 中断控制器的中断配置。
    第6章 如何产生优秀的 Micro SaaS 创意
    WPF动画(2)
    uniapp 离线打包 plus.runtime.install 安装页面不弹起
    喜大普奔!有软考电子版证书可以正式办理广州落户了!
    WPF程序中 Invoke的使用
    simulink-自定义模块GUI回调函数
    苹果Ios系统app应用程序开发者如何获取IPA文件签名证书时需要注意什么?
  • 原文地址:https://blog.csdn.net/weixin_44379605/article/details/127787459