完整教程:搭建哪吒监控 V1 配置CDN,并启用 Agent TLS 连接

前期准备

  1. Cloudflare 用来配置 CDN 和 SSL 证书
  2. 一个域名(如 qq.com
  3. 两台服务器,A服务器安装 Dashboard 面板Nginx,B服务器安装 Agent 探针。如果需要,也可以选择将两者安装在同一台服务器上。

1. Cloudflare 配置

1.1 添加 A 记录,指定到 A服务器IP

1.2 开启 gRPC 与 WebSockets

1.3 创建 SSL 证书,并复制到 A服务器 /etc/ssl/private/ 目录

证书(.cer) 复制到 /etc/ssl/private/fullchain.cer
私钥(.key) 复制到 /etc/ssl/private/private.key

2. 安装 Dashboard 哪吒监控面板

哪吒面板(Dashboard)用于管理和展示探针数据。 官方手册

2.1 执行脚本

# 海外服务器(GitHub):
curl -L https://raw.githubusercontent.com/nezhahq/scripts/refs/heads/main/install.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh

# 中国大陆服务器(Gitee):
curl -L https://gitee.com/naibahq/scripts/raw/main/install.sh -o nezha.sh && chmod +x nezha.sh && sudo CN=true ./nezha.sh

2-2. 安装流程

3. 安装 Nginx 并配置反向代理

3.1 安装 Nginx

# 更新系统并安装 Nginx:
apt update
apt install -y curl wget sudo unzip
apt install -y nginx

# 生成 Diffie-Hellman 密钥
openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

3.2. 配置 Nginx 反向代理、gRPC

# 打开 Nginx 配置文件:
/etc/nginx/nginx.conf
#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name qq.com; # 替换为你的域名
        ssl_certificate          /etc/ssl/private/fullchain.cer; #  替换为你的域名证书路径
        ssl_certificate_key      /etc/ssl/private/private.key;    #  替换为你的域名私钥路径
        ssl_stapling on;
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:10m;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
        ssl_prefer_server_ciphers on;
        ssl_dhparam /etc/ssl/certs/dhparam.pem; 

        # 配置真实 IP 来源 (Cloudflare 的 IP 范围)
        set_real_ip_from 103.21.244.0/22;
        set_real_ip_from 103.22.200.0/22;
        set_real_ip_from 103.31.4.0/22;
        set_real_ip_from 104.16.0.0/13;
        set_real_ip_from 104.24.0.0/14;
        set_real_ip_from 108.162.192.0/18;
        set_real_ip_from 131.0.72.0/22;
        set_real_ip_from 141.101.64.0/18;
        set_real_ip_from 162.158.0.0/15;
        set_real_ip_from 172.64.0.0/13;
        set_real_ip_from 173.245.48.0/20;
        set_real_ip_from 188.114.96.0/20;
        set_real_ip_from 190.93.240.0/20;
        set_real_ip_from 197.234.240.0/22;
        set_real_ip_from 198.41.128.0/17;
        
        # 允许处理下划线的请求头(特别是 CF-Connecting-IP)
        underscores_in_headers on;
        
        real_ip_header CF-Connecting-IP;
        real_ip_recursive on; 

        # 设置 proxy_temp_file_write_size
        proxy_temp_file_write_size 512k;
        
        # gRPC 设置   
        location ^~ /proto.NezhaService/ {
            grpc_set_header Host $host;
            grpc_set_header nz-realip $http_CF-Connecting-IP;
            grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            grpc_read_timeout 600s;
            grpc_send_timeout 600s;
            grpc_socket_keepalive on;
            client_max_body_size 10m;
            grpc_buffer_size 4m;
            grpc_pass grpc://dashboard;
        }

        # websocket 反向代理
        location ~* ^/api/v1/ws/(server|terminal|file)(.*)$ {
            proxy_set_header Host $host;
            proxy_set_header nz-realip $http_CF-Connecting-IP;
            proxy_set_header Origin https://$host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
            proxy_pass http://127.0.0.1:8008;
        }

        # Web 反向代理
        location / {
            proxy_set_header Host $host;
            proxy_set_header nz-realip $http_CF-Connecting-IP;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_read_timeout 3600s;
            proxy_send_timeout 3600s;
            proxy_buffer_size 128k;
            proxy_buffers 4 256k;
            proxy_busy_buffers_size 256k;
            proxy_max_temp_file_size 0;
            proxy_pass http://127.0.0.1:8008;
        }
    }

    upstream dashboard {
        server 127.0.0.1:8008;
        keepalive 512;
    }
}

3.3. 重启Nginx

# 检查配置文件的语法和正确性
nginx -t

### 结果解释
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok      # 语法正确
nginx: configuration file /etc/nginx/nginx.conf test is successful      # 正常启动


# 重启 Nginx 并检查其运行状态:
systemctl restart nginx && systemctl status nginx

4. 安装 Agent 探针

登录 (Dashboard) 面板,首次登录的默认用户名和密码均为 admin并复制 Agent 安装命令,并在 B服务器 安装

# 安装 必要组件
apt update
apt install -y curl wget sudo unzip

### 安装 Agent 演示
root@s38455:~# curl -L https://raw.githubusercontent.com/nezhahq/scripts/main/agent/install.sh -o agent.sh && chmod +x agent.sh && env NZ_SERVER=qq.com:443 NZ_TLS=true NZ_CLIENT_SECRET=DFF4dDF44ffsfdfdfFFF ./agent.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4947  100  4947    0     0  15564      0 --:--:-- --:--:-- --:--:-- 15605
Installing...
2025/03/07 01:04:15 Successfully executed action install!
nezha-agent successfully installed  # 说明成功

5. 美化面板 Dashboard 与 监控节点

# NezhaDash官方文档:
https://nezhadash-docs.buycoffee.top/custom-code

# 服务器公开备注生成器:
https://nezhainfojson.pages.dev/

# 哪吒探针最简单美化教程
https://1keji.net/t/topic/31


https://dnsdaquan.com/
https://ipw.cn/doc/else/dns.html
# ICMP Ping 节点
https://www.nodeseek.com/post-82748-1

# TCP-Ping 节点
https://www.nodeseek.com/post-68572-1

# TCP-Ping 节点
https://hunter.qianxin.com
语法搜索:ip.city="广州" AND ip.isp="电信" AND (ip.port=80 OR ip.port=443) AND ip.asn="4134"

6. 安全设置

6.1 (Dashboard) 面板开启OAuth2 绑定第三方网站账户 官方说明

  1. 访问 GitHub 开发者设置 创建 OAuth 应用,并记录 Client ID 与 Client secrets:

2. 打开 Dashboard 面板配置,设置 OAuth2

# 打开 /opt/nezha/dashboard/data/config.yaml 面板(Dashboard) 配置,添加下面代码:

oauth2:
  GitHub:
    clientid: "FF5sdfsfsbdb23dbf"  # 替换你的 Client ID
    clientsecret: "sfsdf6s5df65sf65sd6f5sd6f5sf5s6df"  # 替换你的 Client secrets
    endpoint:
      authurl: "https://github.com/login/oauth/authorize"
      tokenurl: "https://github.com/login/oauth/access_token"
    userinfourl: "https://api.github.com/user"
    useridpath: "id"

3. 登录 Dashboard 面板,进入个人信息,完成 GitHub 授权,即可通过 GitHub 账号登录面板。

4. 授权 第三方网站 登录成功后,可到 更新个人资料 禁止密码登录。

6.2 关闭 探针(Agent)远程控制功能

  1. 把 disable_command_execute: 改成true
# 探针(Agent) 配置
打开 /opt/nezha/agent/config.yml

client_secret: DXwVsdfsdfsfdsdf545s4f  # 探针客户端密钥,必须与哪吒面板中的密钥匹配,否则无法连接
debug: false  # 是否开启调试模式,true 开启详细日志,false 关闭(建议保持 false)
disable_auto_update: false # 是否禁用自动更新,true 禁用,false 允许探针自动更新
disable_command_execute: true # 是否禁用远程命令执行,true 禁止,false 允许(建议生产环境设置为 true 提高安全性)
disable_force_update: false # 是否禁用强制更新,true 禁止,false 允许探针被强制更新
disable_nat: false  # 是否禁用 NAT 穿透,false 允许,true 禁用(适用于固定公网 IP 服务器)
disable_send_query: false  # 是否禁用查询系统信息(如 CPU、内存、磁盘等),false 允许,true 禁用
gpu: false  # 是否启用 GPU 监控,true 监控 GPU,false 关闭(适用于 GPU 服务器)
insecure_tls: false  # 是否允许不安全的 TLS 连接(忽略证书错误),false 严格检查,true 忽略(自签名证书可设为 true)
ip_report_period: 0  # IP 变更上报间隔(秒),0 表示仅在启动时上报一次,>0 表示每 n 秒检测 IP 变更并上报
report_delay: 0  # 探针上报状态的延迟时间(秒),0 立即上报,>0 延迟 n 秒后上报(适用于网络不稳定环境)
self_update_period: 0  # 探针自更新周期(秒),0 代表不自动更新,>0 每 n 秒检查更新(建议保持 0)
server: qq.com:443  # 哪吒面板服务器地址,必须修改为实际的面板地址,例如 mynezha.example.com:443
skip_connection_count: false  # 是否跳过网络连接数统计,false 统计,true 跳过
skip_procs_count: false  # 是否跳过进程数统计,false 统计,true 跳过
temperature: false  # 是否监控 CPU 温度,true 监控,false 关闭(适用于有温度传感器的服务器)
tls: true  # 是否启用 TLS 加密通信,true 使用 HTTPS(推荐),false 使用 HTTP(仅适用于内网)
use_gitee_to_upgrade: false  # 是否使用 Gitee 进行更新,false 使用 GitHub,true 使用 Gitee(适用于中国大陆服务器)
use_ipv6_country_code: false  # 是否使用 IPv6 进行国家/地区判断,false 使用 IPv4,true 使用 IPv6(适用于 IPv6-only 服务器)
uuid: sdfsdf-0243480-3444-4343-gdfgdfg4343  # 探针唯一标识符,必须保持唯一,否则可能导致数据冲突

2. 重启让配置生效

# 运行 哪吒监控管理脚本,选择 3. 重启并更新面板
./nezha.sh


哪吒监控管理脚本
--- https://github.com/nezhahq/nezha ---
1.  安装面板端
2.  修改面板配置
3.  重启并更新面板
4.  查看面板日志
5.  卸载管理面板
————————————————-
6.  更新脚本
————————————————-
0.  退出脚本

请输入选择 [0-6]: 3

7. 按照上述方法操作,绝对能正常搭建并使用,但可能由于你开启了某些功能导致冲突,以下是排查问题时可能用到的命令

测试 gRPC 服务: curl https://qq.com:443/proto.NezhaService/ -H "Content-Type:application/grpc" -X POST -v

7.1 按照教程操作后,探针(Agent)无法连接的解决方法

此内容查看价格为10扶持币,请先
本文最后更新于:2025-3-10 at 14:29:53
原文链接:https://junkai.cc/571.html,转载请注明出处~~~
0

评论0

请先
显示验证码
没有账号?注册  忘记密码?