NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
shanyang
V2EX  ›  NGINX

nginx中proxy_set_header中HTTP_X_FORWARDED_FOR和X-Real-IP作用

  •  
  •   shanyang · Sep 3, 2012 · 18574 views
    This topic created in 5024 days ago, the information mentioned may be changed or developed.
    做了个实验,使用php,test.php代码如下:
    <?
    //php获取ip的算法
    $remoteaddr=$_SERVER["REMOTE_ADDR"];
    echo $remoteaddr;
    echo "---";
    $remotehost=$_SERVER["REMOTE_HOST"];
    echo $remotehost;
    echo "---";
    $xforwardedfor=$_SERVER["HTTP_X_FORWARDED_FOR"];
    echo $xforwardedfor;
    echo "---";
    $xrealip=$_SERVER["HTTP_X_REAL_IP"];
    echo $xrealip;
    ?>
    然后对于配置1
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    server_name bbb.com;
    proxy_pass aaa.com;
    访问bbb.com/test.php得到
    221.214.216.194---221.214.216.194---228.94.9.112, 192.168.0.1---192.168.0.1
    之后去掉proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;这一行,既配置2后在实验
    得到221.214.216.194---221.214.216.194---228.94.9.112---192.168.0.1---1.1
    也就是说$_SERVER["HTTP_X_FORWARDED_FOR"]里面的内容变了少了192.168.0.1这一内网ip
    但是我要怎么配置才能让test.php记录到的地址和不通过反向代理直连的时候一样呢?
    注:直连的时候aaa.com/test.php结果为:228.94.9.112---228.94.9.112---------
    5 replies    1970-01-01 08:00:00 +08:00
    Cofyc
        1
    Cofyc  
       Sep 3, 2012
    web 服务器是 nginx?

    可以了解下 realip 模块:http://wiki.nginx.org/HttpRealipModule
    feiandxs
        2
    feiandxs  
       Sep 3, 2012
    -_____- 后端服务器如果也是nginx的话记得加上
    x-real-ip 前端IP;
    shanyang
        4
    shanyang  
    OP
       Sep 3, 2012
    @Cofyc 被反向代理的web服务器是租用的虚拟主机
    shanyang
        5
    shanyang  
    OP
       Sep 3, 2012
    @feiandxs 我在看看,没注释比较难看懂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2904 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:41 · PVG 13:41 · LAX 22:41 · JFK 01:41
    ♥ Do have faith in what you're doing.