解决Nginx反代不转发真实IP的问题


0, X-Forwarded-For表示转发客户真实ip到后端服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream apicluster {
server 172.31.20.21 weight=1 max_fails=2 fail_timeout=10s;
server 172.31.17.233 weight=1 max_fails=2 fail_timeout=10s;
server 172.31.22.225 weight=1 max_fails=2 fail_timeout=10s;
}

server {
listen 80;
server_name xxxx.com;

location / {
proxy_pass http://apicluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}