73 lines
1.9 KiB
Markdown
73 lines
1.9 KiB
Markdown
# Nginx 反向代理配置说明
|
||
|
||
如果遇到 307 重定向到 `127.0.0.1` 的问题,请检查 Nginx 配置。
|
||
|
||
## 问题现象
|
||
|
||
前端请求 `http://asapi.deepx1.com/api/trades?limit=100&period=7d` 被重定向到 `http://127.0.0.1/api/trades/?limit=100&period=7d`
|
||
|
||
## 可能的原因
|
||
|
||
1. **Nginx 配置中的 `proxy_redirect` 设置错误**
|
||
2. **Location 头的重写规则有问题**
|
||
3. **FastAPI 的自动重定向(已修复)**
|
||
|
||
## 推荐的 Nginx 配置
|
||
|
||
```nginx
|
||
server {
|
||
listen 80;
|
||
server_name asapi.deepx1.com;
|
||
|
||
# 禁用自动重定向到尾部斜杠
|
||
merge_slashes off;
|
||
|
||
location /api {
|
||
proxy_pass http://127.0.0.1:8001;
|
||
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;
|
||
|
||
# 重要:不要重写 Location 头,除非必要
|
||
# proxy_redirect http://127.0.0.1:8001/ http://asapi.deepx1.com/;
|
||
|
||
# 如果必须重写,使用正确的格式
|
||
# proxy_redirect http://127.0.0.1:8001/ http://$host/;
|
||
|
||
# 禁用重定向跟随(让浏览器处理)
|
||
proxy_redirect off;
|
||
}
|
||
}
|
||
```
|
||
|
||
## 检查步骤
|
||
|
||
1. **检查 Nginx 配置**
|
||
```bash
|
||
sudo nginx -t
|
||
sudo nginx -s reload
|
||
```
|
||
|
||
2. **检查 FastAPI 日志**
|
||
```bash
|
||
tail -f backend/logs/api.log
|
||
```
|
||
|
||
3. **测试 API 端点**
|
||
```bash
|
||
curl -v http://asapi.deepx1.com/api/trades?limit=10
|
||
```
|
||
|
||
## 已修复的问题
|
||
|
||
- ✅ FastAPI 应用级别禁用自动重定向(`redirect_slashes=False`)
|
||
- ✅ 路由同时支持有斜杠和无斜杠的路径
|
||
- ✅ 前端 API 调用统一使用无斜杠路径
|
||
|
||
## 如果问题仍然存在
|
||
|
||
1. 检查 Nginx 错误日志:`/var/log/nginx/error.log`
|
||
2. 检查 Nginx 访问日志:`/var/log/nginx/access.log`
|
||
3. 临时禁用 Nginx,直接访问 FastAPI:`http://服务器IP:8001/api/trades`
|