Nginx用X-Forwarded-For替换REMOTE_ADDR [英] Nginx replace REMOTE_ADDR with X-Forwarded-For
问题描述
我对Nginx还是很陌生,这一切似乎都令人困惑.我的服务器设置完美,但是问题是,因为我的服务器是使用HTTP代理保护的;而不是记录真实用户的IP,而是记录代理服务器IP.
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.
我尝试做的是将$_SERVER['REMOTE_ADDR'];
设置为$_SERVER['X-Forwarded-For'];
,但是出现未定义的索引错误,所以我猜我必须在Nginx中定义X-Forwarded-For
吗?但是我不知道该怎么做,我有一个简单的设置,那就是带有PHP的Nginx.没什么,没什么.
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.
推荐答案
正确的方法是在nginx中设置real_ip_header
配置.
The correct way of doing this is by setting the real_ip_header
configuration in nginx.
具有受信任的HTTP代理IP的示例:
Example with trusted HTTP proxy IP:
set_real_ip_from 127.0.0.1/32;
real_ip_header X-Forwarded-For;
这样,$ _ SERVER ['REMOTE_ADDR']将在PHP fastcgi中正确填充.
This way, the $_SERVER['REMOTE_ADDR'] will be correctly filled up in PHP fastcgi.
这篇关于Nginx用X-Forwarded-For替换REMOTE_ADDR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!