一、1Panel 域名证书
如果你使用的是 Cloudflare 的 15 年自签证书,现在需要更换为公开 CA 签发的免费证书(1Panel 自带申请免费证书并自动续签的功能),这样在关闭 CDN 后就不会出现证书链的问题(Cloudflare 的 SSL/TLS 加密模式更改为【完全】)。
二、1Panel 域名配置文件
打开 1Panel 你添加的哪吒面板域名——配置文件,在最后面加上代码:
# 定义真实 IP 变量
map $http_cf_connecting_ip $real_ip {
"" $remote_addr;
default $http_cf_connecting_ip;
}
# 允许头部包含下划线
underscores_in_headers on;
# 设置真实 IP 头部
real_ip_header nz-realip;
# 定义上游服务器
upstream dashboard {
keepalive 512;
server 127.0.0.1:8008;
}
如果你使用的是其它面板(比如宝塔)或程序,完整的配置文件结构应该是这样的:
http {
# 其他全局配置...
# 定义真实IP变量
map $http_cf_connecting_ip $real_ip {
"" $remote_addr;
default $http_cf_connecting_ip;
}
# 允许头部包含下划线
underscores_in_headers on;
# 设置真实IP头部
real_ip_header nz-realip;
# 定义上游服务器
upstream dashboard {
keepalive 512;
server 127.0.0.1:8008;
}
# 其他全局配置...
server {
# server配置...
# 反向代理配置(location块)...
}
}
三、1Panel 域名反向代理源文
打开 1Panel 你添加的哪吒面板域名——反向代理源文,全部替换代码:
location ^~ / {
proxy_pass http://127.0.0.1:8008;
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 $real_ip; # 使用动态确定的真实 IP
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_read_timeout 1800s;
proxy_send_timeout 1800s;
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 on;
add_header Strict-Transport-Security "max-age=31536000";
}
# gRPC 服务
location ^~ /proto.NezhaService/ {
grpc_set_header Host $host;
grpc_set_header nz-realip $real_ip; # 使用动态确定的真实 IP
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 $real_ip; # 使用动态确定的真实IP
proxy_set_header Origin https://$host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1800s;
proxy_send_timeout 1800s;
proxy_pass http://127.0.0.1:8008;
}
这个配置的关键点:
使用 map 指令创建一个名为 $real_ip 的变量;
该变量会自动检测是否存在 $http_cf_connecting_ip:如果存在(开启 CDN),则使用 $http_cf_connecting_ip;如果不存在(关闭 CDN),则使用 $remote_addr。
在所有需要使用真实 IP 的地方,统一使用$real_ip 变量。
四、哪吒监控面板后台设置
前端真实 IP 请求头设置为 nz-realip
Agent 真实 IP 请求头设置为 nz-realip
无论是否开启 CDN 都不需要再修改这个设置,因为我们通过 Nginx 的 map 指令动态确定了 nz-realip 头部的值:
开启 CDN 时:nz-realip 的值是 Cloudflare 提供的 $http_cf_connecting_ip(访问者真实 IP);
关闭 CDN 时:nz-realip 的值是 $remote_addr(直接访问的 IP)。
至此,所有设置完成。
这个统一配置的好处是简化了管理,不需要在切换 CDN 状态时手动修改 Nginx 配置和哪吒面板设置,系统会自动选择正确的 IP 来源。

Comments NOTHING