- 更新 .env.local 中的域名配置 - 更新 infra/deploy/.env.local - 更新 compose.local.yaml 中的环境变量 - 更新 admin-web 构建参数 VITE_API_BASE_URL - 更新小程序 app.ts 中的 apiBaseUrl
174 lines
4.6 KiB
Markdown
174 lines
4.6 KiB
Markdown
# FRP服务器Nginx配置修复任务
|
|
|
|
## 问题描述
|
|
乐活公益平台(lhgy.net)的HTTPS访问出现问题。通过域名访问API时,路径 `/api/v1/...` 被错误地修改为 `/v1/...`(缺少 `/api/` 前缀),导致所有API请求返回404错误。
|
|
|
|
**症状**:
|
|
- `https://lhgy.net/api/v1/admin/auth/login` → 返回 `Cannot POST /v1/admin/auth/login`
|
|
- 本地访问正常:`https://127.0.0.1/api/v1/test` → 成功
|
|
- 公网IP访问失败:`https://47.116.99.193/api/v1/test` → 同样失败
|
|
|
|
**根本原因**: FRP服务器上的nginx配置有问题,正在修改请求路径。
|
|
|
|
## 任务步骤
|
|
|
|
### 第一步:诊断问题
|
|
请执行以下命令检查nginx配置:
|
|
|
|
```bash
|
|
# 1. 查看完整的nginx配置
|
|
sudo nginx -T
|
|
|
|
# 2. 检查是否有rewrite规则修改路径
|
|
sudo nginx -T | grep -i "rewrite"
|
|
|
|
# 3. 检查所有location块配置
|
|
sudo nginx -T | grep -A 20 "location /api"
|
|
|
|
# 4. 检查proxy_pass配置
|
|
sudo nginx -T | grep -B 5 -A 10 "proxy_pass"
|
|
|
|
# 5. 查看nginx配置文件列表
|
|
sudo ls -la /etc/nginx/
|
|
sudo ls -la /etc/nginx/conf.d/
|
|
sudo ls -la /etc/nginx/sites-enabled/
|
|
sudo ls -la /etc/nginx/sites-available/
|
|
```
|
|
|
|
### 第二步:定位问题配置
|
|
根据诊断结果,找到正在修改路径的配置。常见问题包括:
|
|
|
|
1. **有rewrite规则剥离 `/api/` 前缀**
|
|
```nginx
|
|
# 问题配置示例(需要删除或修改):
|
|
rewrite ^/api/(.*)$ /$1 break;
|
|
```
|
|
|
|
2. **proxy_pass带有尾部斜杠**
|
|
```nginx
|
|
# 问题配置示例(需要移除尾部斜杠):
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:3000/; # 注意尾部斜杠
|
|
}
|
|
```
|
|
|
|
3. **有多个server块冲突**
|
|
- 检查是否有多个配置监听443端口
|
|
- 检查是否有default配置干扰
|
|
|
|
### 第三步:修复配置
|
|
找到问题后,编辑相应的nginx配置文件:
|
|
|
|
```bash
|
|
# 编辑主配置文件或conf.d下的配置文件
|
|
sudo nano /etc/nginx/nginx.conf
|
|
# 或
|
|
sudo nano /etc/nginx/conf.d/*.conf
|
|
```
|
|
|
|
**修复原则**:
|
|
- 删除或注释掉修改路径的rewrite规则
|
|
- 确保 `proxy_pass` 后面没有路径和尾部斜杠
|
|
- 确保 `location /api/` 块的配置正确
|
|
|
|
**正确的配置示例**:
|
|
```nginx
|
|
server {
|
|
listen 443 ssl;
|
|
server_name lhgy.net www.lhgy.net;
|
|
|
|
ssl_certificate /etc/nginx/ssl/your-cert.pem;
|
|
ssl_certificate_key /etc/nginx/ssl/your-key.pem;
|
|
|
|
# API代理 - 保留完整路径
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:3000; # 不要有尾部斜杠
|
|
proxy_http_version 1.1;
|
|
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 X-Forwarded-Proto $scheme;
|
|
proxy_set_header Connection "";
|
|
}
|
|
|
|
# 管理后台
|
|
location / {
|
|
proxy_pass http://127.0.0.1:5173;
|
|
proxy_http_version 1.1;
|
|
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 X-Forwarded-Proto $scheme;
|
|
proxy_set_header Connection "";
|
|
}
|
|
}
|
|
```
|
|
|
|
### 第四步:验证修复
|
|
修复后执行以下步骤:
|
|
|
|
```bash
|
|
# 1. 测试nginx配置语法
|
|
sudo nginx -t
|
|
|
|
# 2. 重载nginx
|
|
sudo nginx -s reload
|
|
|
|
# 3. 验证API访问
|
|
curl -sk https://lhgy.net/api/v1/admin/auth/login \
|
|
-X POST \
|
|
-H "Content-Type: application/json" \
|
|
-H "Origin: https://lhgy.net" \
|
|
-d '{"username":"admin","password":"lehuo@123456"}'
|
|
|
|
# 期望返回包含session信息的JSON响应
|
|
# 如果仍然失败,检查nginx错误日志
|
|
sudo tail -20 /var/log/nginx/error.log
|
|
```
|
|
|
|
### 第五步:记录修改
|
|
```bash
|
|
# 备份修改前的配置
|
|
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak.$(date +%Y%m%d_%H%M%S)
|
|
|
|
# 记录修改内容
|
|
echo "修复时间: $(date)" >> /tmp/nginx-fix-log.txt
|
|
echo "修改文件: xxx" >> /tmp/nginx-fix-log.txt
|
|
echo "修改内容: 删除了xxx规则" >> /tmp/nginx-fix-log.txt
|
|
```
|
|
|
|
## 注意事项
|
|
1. 修改配置前务必备份
|
|
2. 修改后先用 `nginx -t` 测试语法
|
|
3. 使用 `nginx -s reload` 而不是重启服务
|
|
4. 如果无法确定问题,请提供完整的 `nginx -T` 输出
|
|
|
|
## 验证成功的标准
|
|
修复后,以下命令应该返回包含session信息的JSON响应:
|
|
|
|
```bash
|
|
curl -sk https://lhgy.net/api/v1/admin/auth/login \
|
|
-X POST \
|
|
-H "Content-Type: application/json" \
|
|
-H "Origin: https://lhgy.net" \
|
|
-d '{"username":"admin","password":"lehuo@123456"}'
|
|
```
|
|
|
|
期望响应示例:
|
|
```json
|
|
{
|
|
"success": true,
|
|
"data": {
|
|
"user": {
|
|
"publicId": "admin-final-test",
|
|
"displayName": "系统管理员",
|
|
"roles": ["SUPER_ADMIN"]
|
|
},
|
|
"session": {
|
|
"token": "...",
|
|
"expiresAt": "..."
|
|
}
|
|
}
|
|
}
|
|
```
|