fix(nginx): 补充反向代理故障排查和 proxy_pass 陷阱说明
This commit is contained in:
@@ -408,6 +408,55 @@ cat /etc/letsencrypt/options-ssl-nginx.conf
|
|||||||
|
|
||||||
## 八、故障排查
|
## 八、故障排查
|
||||||
|
|
||||||
|
### 反向代理常见错误
|
||||||
|
|
||||||
|
#### "URL 拼写可能存在错误"
|
||||||
|
|
||||||
|
这个错误**不是 Nginx 本身返回的**,而是后端服务(如 Spring Boot、Node.js、Vite 等)返回的。
|
||||||
|
|
||||||
|
**排查步骤:**
|
||||||
|
```bash
|
||||||
|
# 第1步:直接测试后端服务(排除 Nginx 影响)
|
||||||
|
curl -v http://127.0.0.1:1234/
|
||||||
|
|
||||||
|
# 如果直接访问也报错 → 问题在后端,与 Nginx 无关
|
||||||
|
# 如果直接访问正常 → 问题在 Nginx 配置
|
||||||
|
```
|
||||||
|
|
||||||
|
#### proxy_pass 路径陷阱
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
# ❌ 错误:末尾多了斜杠,会把 /app/ 替换成 /
|
||||||
|
location /app/ {
|
||||||
|
proxy_pass http://127.0.0.1:8080/;
|
||||||
|
}
|
||||||
|
|
||||||
|
# ✅ 正确:末尾无斜杠,保持原路径
|
||||||
|
location /app/ {
|
||||||
|
proxy_pass http://127.0.0.1:8080; # 不加 /
|
||||||
|
}
|
||||||
|
|
||||||
|
# 特殊情况:需要去掉前缀时
|
||||||
|
location /api/ {
|
||||||
|
# rewrite + proxy_pass 末尾斜杠 = 去掉 /api 前缀
|
||||||
|
rewrite ^/api/(.*)$ /$1 break;
|
||||||
|
proxy_pass http://127.0.0.1:8080/; # 这里需要斜杠
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 必备的代理请求头
|
||||||
|
|
||||||
|
```nginx
|
||||||
|
location / {
|
||||||
|
proxy_pass http://127.0.0.1:1234;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### 配置测试失败
|
### 配置测试失败
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -444,3 +493,24 @@ sudo certbot renew --force-renewal
|
|||||||
# 或者重新申请
|
# 或者重新申请
|
||||||
sudo certbot certonly --nginx -d example.com
|
sudo certbot certonly --nginx -d example.com
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 快速诊断命令
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 检查 Nginx 和后端端口
|
||||||
|
sudo lsof -i :1234
|
||||||
|
sudo netstat -tlnp | grep nginx
|
||||||
|
|
||||||
|
# 2. 直接测试后端
|
||||||
|
curl -v http://127.0.0.1:1234/
|
||||||
|
|
||||||
|
# 3. 测试 Nginx 配置
|
||||||
|
sudo nginx -t
|
||||||
|
|
||||||
|
# 4. 查看实时日志
|
||||||
|
sudo tail -f /var/log/nginx/access.log
|
||||||
|
sudo tail -f /var/log/nginx/error.log
|
||||||
|
|
||||||
|
# 5. 查看完整的已加载配置
|
||||||
|
sudo nginx -T
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user