前期准备
- Cloudflare:用于 CDN 和 SSL 证书管理 域名:例如 nezha.
qq.com- A 服务器:安装 Dashboard 面板 + 反向代理
- 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-A. 安装 Nginx 并配置反向代理
3-1 安装 Nginx
# 更新系统并安装 Nginx: 【CentOS / RHEL】 yum install -y curl wget sudo unzip nginx 【Debian / Ubuntu】 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/WebSocket
# 打开 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
3-B. 宝塔配置反向代理
# -------------------------
# 哪吒服务器监控 gRPC 服务
# -------------------------
upstream dashboard { #对应反代的grpc_pass grpc://dashboard 给 Nginx 一个“别名”叫 dashboard,指向哪吒的本地服务 127.0.0.1:7124
server 127.0.0.1:7124;
keepalive 512; #可以保持长连接,提高 WebSocket、gRPC 等实时通信性能
}
# 将上述代码复制到网站配置 server 上面
server
{
# ==================================================
# 代理目标地址、端口 变量定义
# ==================================================
set $proxy_url 127.0.0.1;
set $port 7124;
# ==================================================
# Cloudflare 回源 IP 地址范围
# 官方文档:https://www.cloudflare.com/zh-cn/ips/
# ==================================================
underscores_in_headers on; # [Cloudflare → Nginx] 允许 HTTP header 带下划线,兼容 Cloudflare 自定义头,不开启时,像 nz-realip 这种头会被 Nginx 忽略
ignore_invalid_headers off;
#------ Cloudflare 代理
# [Cloudflare → Nginx] 接受来自 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;
# [Cloudflare → Nginx] 从 Cloudflare 回源请求中获取真实客户端 IP
real_ip_header CF-Connecting-IP;
#------ Nginx 反向代理
# set_real_ip_from 0.0.0.0/0; # [Nginx → Cloudflare] 接受来自 Nginx 反向代理 的回源 IP
# sreal_ip_recursive on; # 只取第一个 IP(客户端真实 IP)
# ==================================================
# Web 面板代理配置
# ==================================================
location ^~ / {
proxy_pass http://127.0.0.1:7124;
proxy_set_header Host $host;
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 nz-realip $http_cf_connecting_ip;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
proxy_buffer_size 128k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 0;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
proxy_ssl_server_name off;
proxy_ssl_name $proxy_host;
add_header Strict-Transport-Security "max-age=31536000";
}
# ==================================================
# gRPC 服务(哪吒通信)
# ==================================================
location ^~ /proto.NezhaService/ {
grpc_pass grpc://dashboard;
grpc_set_header Host $host;
grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#------ Cloudflare 代理
grpc_set_header nz-realip $http_CF_Connecting_IP;
#------ Nginx 反向代理
# grpc_set_header nz-realip $remote_addr;
grpc_read_timeout 600s; # [Nginx → 后端] 后端 gRPC 响应超时,防止长时间没响应被断开
grpc_send_timeout 600s; # [Nginx → 后端] 向后端发送请求超时
grpc_socket_keepalive on; # [Nginx → 后端] 保持长连接,减少 TCP 建立/关闭开销
client_max_body_size 10m; # [Nginx → 后端] 限制请求体最大 10MB,防止超大请求占用资源
grpc_buffer_size 4m; # [Nginx → 后端] gRPC 请求/响应缓冲区大小
}
# ==================================================
# WebSocket 服务
# ==================================================
location ~* ^/api/v1/ws/(server|terminal|file)(.*)$ {
proxy_pass http://$proxy_url:$port;
proxy_set_header Host $host;
#------ Cloudflare 代理
proxy_set_header nz-realip $http_cf_connecting_ip;
#------ Nginx 反向代理
# proxy_set_header nz-realip $remote_addr;
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;
}
4. 安装 Agent 探针
登录 (Dashboard) 面板,首次登录的默认用户名和密码均为 admin,并复制 Agent 安装命令,并在 B服务器 安装。

# 安装 必要组件
【CentOS / RHEL】
yum -y install curl wget sudo unzip
【Debian / Ubuntu】
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 # Nezha JSON 生成器: https://nezhajsontools.pages.dev/ https://nz-rule-generator.pages.dev/ https://nz-rule-generator.pages.dev/ # 哪吒探针最简单美化教程 https://1keji.net/t/topic/31 # TCPing 节点查询 https://lf3-ips.zstaticcdn.com/ https://tcping.wuxie.de/ # ICMP Ping 节点查询 https://dnsdaquan.com/ https://ipw.cn/doc/else/dns.html # TCP-Ping 节点 https://hunter.qianxin.com 语法搜索:ip.city="广州" AND ip.isp="电信" AND (ip.port=80 OR ip.port=443)
6. 安全设置
6-1 (Dashboard) 面板开启OAuth2 绑定第三方网站账户 官方说明
- 访问 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)远程控制功能
- 把 disable_command_execute: 改成true
# 探针(Agent) 配置 打开 /opt/nezha/agent/config.yml # ========================= # 哪吒 Agent 国内模板 # Agent 版本:https://github.com/nezhahq/agent/releases # ========================= # ============================ # 核心安全通信 # ============================ client_secret: # 客户端密钥,用于 Agent 与 Dashboard 安全通信 # ============================ # 日志与调试 # ============================ debug: false # 部署初期建议 true,稳定运行后可改为 false # ============================ # 更新 # ============================ disable_auto_update: false # 是否禁用自动更新 disable_force_update: false # 是否禁止强制更新,只允许手动更新 use_gitee_to_upgrade: true # 使用 Gitee 作为更新源,对国内服务器友好 use_atomgit_to_upgrade: false # 使用 AtomGit 作为更新源,对国内服务器友好 self_update_period: 0 # 自动更新间隔(分钟) # ============================ # 网络与通信 # ============================ disable_nat: false # 是否禁止内网穿透任务 disable_send_query: false # 禁止 TCP/ICMP/HTTP 测试任务 insecure_tls: false # 自签名证书可 true,生产环境必须 false tls: true # 开启 Agent 与 Dashboard 的 TLS 加密 server: qq.com:443 # Dashboard 地址(必须带端口) use_ipv6_country_code: false # 是否强制用 IPv6 查询国家码 # ============================ # 系统信息上报 # ============================ ip_report_period: 1800 # 本地 IP 更新间隔 report_delay: 3 # 系统信息上报延迟 # ============================ # 硬件监控 # ============================ gpu: false # 是否启用 GPU 监控 temperature: true # 硬件温度监控 #hard_drive_partition_allowlist: ["/","/data"] # 开关分析:生产环境开避免占用资源;全部分区可关 #nic_allowlist: # 开关分析:多网卡环境可选择性监控 # ============================ # 性能优化 # ============================ skip_connection_count: false # 禁用网络连接数监控 # 开关分析:小型环境可关;高负载一定要开 skip_procs_count: false # 禁用进程数监控 # 开关分析:普通环境可关;资源受限环境可开 # ============================ # Agent 唯一标识 # ============================ uuid: # Agent 唯一 ID(不能修改) # ============================ # DNS 与自定义 IP 查询 # ============================ #custom_ip_api: ["https://api.ip.sb/ip","https://api64.ip.sb/ip"] 自定义 IP 查询 API
2. 重启让配置生效
# 运行 哪吒监控管理脚本,选择 3. 重启并更新面板 ./nezha.sh 哪吒监控管理脚本 --- https://github.com/nezhahq/nezha --- 1. 安装面板端 2. 修改面板配置 3. 重启并更新面板 4. 查看面板日志 5. 卸载管理面板 ————————————————- 6. 更新脚本 ————————————————- 0. 退出脚本 请输入选择 [0-6]: 3
# 查看网络接口详细状态及开机累计流量
ip -s link
# 查看系统中可用的网络接口列表
ls /sys/class/net/
# ===============================
# 当前网络接口累计流量统计脚本
# ===============================
RX_BYTES=$(cat /sys/class/net/eth0/statistics/rx_bytes)
TX_BYTES=$(cat /sys/class/net/eth0/statistics/tx_bytes)
format_bytes() {
local bytes=$1
if (( bytes < 1024*1024 )); then
echo "$((bytes/1024)) KB"
elif (( bytes < 1024*1024*1024 )); then
awk "BEGIN {printf \"%.1f MB\", $bytes/1024/1024}"
else
awk "BEGIN {printf \"%.2f GB\", $bytes/1024/1024/1024}"
fi
}
RX_FMT=$(format_bytes $RX_BYTES)
TX_FMT=$(format_bytes $TX_BYTES)
echo "当前累计流量: 下载RX=${RX_FMT}, 上传TX=${TX_FMT}"
7. 按照上述方法操作,绝对能正常搭建并使用,但可能由于你开启了某些功能导致冲突,以下是排查问题时可能用到的命令
测试 gRPC 服务: curl https://qq.com:443/proto.NezhaService/ -H "Content-Type:application/grpc" -X POST -v
7-1 按照教程操作后,探针(Agent)无法连接的解决方法
此内容查看价格为10扶持币,请先登录
本文最后更新于:2026-6-20 at 11:15:46
原文链接:https://junkai.cc/571.html,转载请注明出处~~~


评论0