lab/note/资源/DevOps/Nginx/常见错误.md
张育新 241a7c43da docs(note): 新增 excel/正则/Nginx 笔记并整理目录
- 新增 AGENTS.md 仓库说明与 excelize 依赖
- 新增正则表达式、go操作excel、Nginx常见错误、Golang底层架构笔记
- 布尔变量命名迁移至资源/编程/规范,删除随手记旧笔记
2026-08-05 18:32:17 +08:00

46 lines
947 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Nginx 反代未透传客户端 IP 导致服务端日志全量变为 127.0.0.1
### 现象
后端解析IP后全量变成 `127.0.0.1`
### 原因
后端解析IP的顺序是
```
X-Forwarded-For → X-Real-IP → RemoteAddr
```
**Nginx**转发至后端服务的 location 块缺少`proxy_set_header`指令:
```nginx
location /olive3 {
proxy_pass http://127.0.0.1:55430;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
```
### 修复
在缺失的 location 块中补充 header 透传:
```nginx
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:55430;
# ... 其他配置不变
}
```
- `$proxy_add_x_forwarded_for`:将客户端真实 IP 追加到已有 `X-Forwarded-For` 尾部,而非覆盖
- `$remote_addr`Nginx 直接看到的对端地址,作为 `X-Real-IP` 传递