Nginx 用 X-Forwarded-For 替换 REMOTE_ADDR [英] Nginx replace REMOTE_ADDR with X-Forwarded-For

查看:51
本文介绍了Nginx 用 X-Forwarded-For 替换 REMOTE_ADDR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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.org

这篇关于Nginx 用 X-Forwarded-For 替换 REMOTE_ADDR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆