Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

php - Nginx replace REMOTE_ADDR with X-Forwarded-For

I am quite new to Nginx, and it seems all so confusing. I have my server setup perfectly, but the problem is, since my server is protected using a HTTP proxy; instead of logging the real users IP's, it's logging the proxy server IP.

What I tried doing was setting $_SERVER['REMOTE_ADDR']; to $_SERVER['X-Forwarded-For']; but I'm getting a undefined index error, so I'm guessing I have to define X-Forwarded-For in Nginx? But I am not aware how to do so, I have a simple setup, it's just Nginx with PHP. Nothing more, nothing less.

I've searched all over the web, but can't actually find some information that is friendly to understand.

I have access to the source code, if that somewhat helps. I've tried many solutions, but to no avail.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The correct way of doing this is by setting the real_ip_header configuration in nginx.

Example with trusted HTTP proxy IP:

set_real_ip_from 127.0.0.1/32;
real_ip_header X-Forwarded-For;

This way, the $_SERVER['REMOTE_ADDR'] will be correctly filled up in PHP fastcgi.

Documentation link - nginx.org


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...