一、先验证:确认代理已正常工作(关键步骤,避免白配置)

**

1. 基础连通性测试(浏览器 /curl 二选一)

  • 若显示「127.0.0.1:3000」后端的页面 / 接口返回(比如 VEXO 的登录页、接口 JSON 数据),说明转发成功;

  • 若提示「502 Bad Gateway」,先检查后端 127.0.0.1:3000 是否已启动(直接访问 http://127.0.0.1:3000 看能否打开,后端没启动会导致代理失败)。

  • 命令行测试(更精准,适合接口场景):

  1. 打开 PowerShell,输入命令:

curl http://localhost:8080/你的后端接口路径(比如 curl http://localhost:8080/api/user);

  1. 若返回后端接口的正常数据(非错误码),且响应头中能看到 X-Real-IP 是你本机真实 IP(可通过 curl -I http://localhost:8080 查看响应头),说明「真实 IP 传递」也生效了。

2. 验证请求头是否正确传递(确保后端能识别真实请求)

  1. 在后端 127.0.0.1:3000 服务中加一段「打印请求头」的日志(以常见后端为例):

  • 若为 Node.js/Express:在入口文件加

app.use((req, res, next) => { console.log('接收的请求头:', req.headers); next(); });

  • 若为 Java/SpringBoot:在 Controller 加

System.out.println("Host头:" + request.getHeader("Host") + ",真实IP:" + request.getHeader("X-Real-IP"));

  1. 访问 http://localhost:8080 触发请求,查看后端日志:

  • 需看到 Host 为 localhost:8080(或你访问时的域名)、X-Real-IP 为你本机 IP(非 127.0.0.1)、X-Forwarded-* 头(如 X-Forwarded-For)正确携带,说明请求头传递无误。

二、再保障:让 Nginx 代理长期稳定运行(避免重启电脑后失效)

1. 设置 Nginx 开机自启(Windows 环境,不用每次手动双击 nginx.exe)

  1. 新建一个文本文件,复制以下内容,保存为 nginx-auto-start.bat(放在 C:\nginx 目录下):

@echo off
set "nginxPath=C:\nginx\nginx.exe"
tasklist /fi "imagename eq nginx.exe" | find /i "nginx.exe" > nul
if %errorlevel% neq 0 (
    start "" "%nginxPath%"
    echo Nginx已启动
) else (
    echo Nginx已在运行
)
  1. 按 Win+R 输入 shell:startup 打开「启动文件夹」,将上述 nginx-auto-start.bat 复制到该文件夹中;

  1. 测试:重启电脑后,打开任务管理器(Ctrl+Shift+Esc),在「进程」中能看到 nginx.exe,说明自启成功。

2. 定期检查代理状态(避免意外中断)

可创建一个简单的「健康检查脚本」(比如 check-nginx.bat),放在 C:\nginx 目录,定期双击运行:

@echo off
echo 正在检查Nginx进程...
tasklist /fi "imagename eq nginx.exe" | find /i "nginx.exe" > nul
if %errorlevel% equ 0 (
    echo Nginx运行正常,代理端口8080状态:
    netstat -ano | findstr ":8080" | findstr "LISTENING"
) else (
    echo Nginx已停止,正在重启...
    start "" "C:\nginx\nginx.exe"
    echo Nginx重启完成
)
pause

三、按需加:补充增强配置(根据你的后端场景选择)

1. 若后端用 WebSocket(比如实时消息、直播场景)

打开 C:\nginx\conf\nginx.conf,在你的 location / { ... } 内加入以下配置,然后执行 cd C:\nginx && .\nginx -s reload 生效:

location / {
    proxy_pass  http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    # 新增WebSocket支持配置
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 600s;  # 延长超时时间,避免WebSocket断开
}

2. 若后端是 HTTPS(比如 https://127.0.0.1:3000,当前是 HTTP,可备用)

若后续后端改为 HTTPS,需修改 proxy_pass 并加 SSL 验证配置:

location / {
    proxy_pass  https://127.0.0.1:3000;  # 改http为https
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    # 新增HTTPS代理配置
    proxy_ssl_server_name on;  # 让Nginx正确验证HTTPS域名
    proxy_ssl_verify off;  # 若后端是自签证书,加这行避免验证失败(正式环境需开启并配置证书)
}

3. 若前端调用代理时出现跨域(比如前端在 http://localhost:8081 调用 http://localhost:8080

在 location / { ... } 内加入跨域头(根据你的前端域名调整,比如允许 http://localhost:8081 访问):

location / {
    proxy_pass  http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    # 新增跨域配置
    add_header Access-Control-Allow-Origin "http://localhost:8081" always;  # 允许的前端域名
    add_header Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS" always;
    add_header Access-Control-Allow-Headers "Content-Type,Token" always;
    # 处理OPTIONS预请求
    if ($request_method = 'OPTIONS') {
        return 204;
    }
}

四、常见问题排查(遇到问题先看这里)

  1. 访问 http://localhost:8080 提示「404 Not Found」

检查后端 http://127.0.0.1:3000 是否真的有「根路径(/)」的资源,若后端只有 /api 开头的接口,需访问 http://localhost:8080/api 才会有响应。

  1. 执行 .\nginx -s reload 报错「nginx: [error] OpenEvent ("Global\ngx_reload_1234") failed (2: The system cannot find the file specified)」

说明 Nginx 没启动,先执行 .\nginx.exe 启动,再执行重载命令。

  1. 端口 8080 被占用(报错「bind () to 0.0.0.0:8080 failed (10048: Only one usage of each socket address)」)

打开 PowerShell 输入 netstat -ano | findstr ":8080",找到占用端口的进程 PID(最后一列数字),在任务管理器「详细信息」中找到该 PID 并结束进程,或修改 Nginx 配置的 listen 8080 为 8081/8082(改后需重载)。