一见

如何取得nginx做反向代理时的真实IP?


1. 编译

对于client -> nginx reverse proxy -> apache,

要想在程序中取得真实的IP,在执行nginxconfigure时,必须指定参数“--with-http_realip_module”,示例:

./configure --prefix=/data/nginx --with-http_realip_module --with-stream --with-pcre=/tmp/X/pcre-8.32 --with-openssl=/tmp/X/openssl-1.0.2a

 

参数说明:

--prefix= 指定安装目录,也就是make install后程序文件等的存放目录

--with-http_realip_module 使得程序可以通过环境变量HTTP_X_REAL_IP取得真实的客户端IP地址

--with-stream 表示启用TCP代理

--with-pcre= 指定依赖的pcre,注意为pcre源代码解压后的目录路径,而不是安装路径

--with-openssl= 指定依赖的openssl,注意为openssl源代码解压后的目录路径,而不是安装路径

 

另外,最简单的确认方法是使用nm命令查看nginx程序文件,看看是否有包含real相关的符号,对于版本nginx-1.9.4 ,可以发现存在“0809c54b t ngx_http_realip”。

2. 程序代码

测试程序代码(后续测试基于它):

// g++ -g -o hello.cgi hello.cpp

#include <stdio.h>

#include <stdlib.h>

int main()

{

        printf("Content-Type: text/html; charset=utf-8\r\n\r\n");

        printf("<p>HTTP_X_FORWARDED_FOR: %s\n", getenv("HTTP_X_FORWARDED_FOR"));

        printf("<p>HTTP_X_REAL_IP: %s\n", getenv("HTTP_X_REAL_IP"));

        printf("<p>REMOTE_ADDR: %s\n", getenv("REMOTE_ADDR"));

        printf("<p>");

        return 0;

}

 

测试是在nginx自带配置文件nginx.conf上进行的修改:

proxy_set_header可以添加在nginx.confhttp段,也可以是server段,还可以是location段,一级一级间是继承和覆盖关系。

3. 相关配置

示例:

location / {

#    root   html;

#    index  index.html index.htm;

    proxy_pass http://20.61.28.11:80;

    proxy_redirect off;

    proxy_set_header   Host    $host; 

    proxy_set_header   X-Real-IP   $remote_addr;  # 这个是必须的

    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 

}

 

X-Forwarded-ForX-Real-IP的区别是,如果请求时已带了X-Forwarded-For,则nginx追加方式,这样可以通过它显示转发的轨迹。

当然请求时完全可以构造假的X-Forwarded-For,在配置文件打开了X-Real-IP及编译指定了--with-http_realip_module时,环境变量HTTP_X_REAL_IP总是为真实的客户端IP

 

如果是:

client -> nginx reverse proxy (A) -> nginx reverse proxy (B) -> apache

HTTP_X_REAL_IP又会是什么了?

4. 测试1

假设如下部署:

client(10.6.81.39) -> nginx(10.6.223.44:8080) -> nginx(10.6.208.101:8080) -> apache(10.6.208.101:80)

 

Ø A

假设nginx(10.6.223.44:8080)的配置均为(在nginx默认配置上的修改部分):

server {

    listen       8080;

    server_name  10.6.223.44;

    location / {

    #    root   html;

    #    index  index.html index.htm;

        proxy_pass http://10.6.208.101:8080;

        proxy_redirect off;

        proxy_set_header   Host    $host;

        proxy_set_header   X-Real-IP   $remote_addr;

        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

    }

 

假设nginx(10.6.208.101:8080)的配置均为(在nginx默认配置上的修改部分):

server {

    listen       8080;

    server_name  10.6.208.101;

    location / {

    #    root   html;

    #    index  index.html index.htm;

        proxy_pass http://10.6.208.101:80;

        proxy_redirect off;

        proxy_set_header   Host    $host;

        proxy_set_header   X-Real-IP   $remote_addr;

        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

    }

 

上述测试程序输出的结果为:

<p>HTTP_X_FORWARDED_FOR: 10.6.81.39, 10.6.223.44

<p>HTTP_X_REAL_IP: 10.6.223.44

<p>REMOTE_ADDR: 10.6.81.39

 

Ø B

但如果clientHTTP请求头中加入:

X-FORWARDED-FOR:8.8.8.7

CLIENT-IP:8.8.8.8

X-REAL-IP:8.8.8.10

后输出结果变成:

<p>HTTP_X_FORWARDED_FOR: 8.8.8.7, 10.6.81.39, 10.6.223.44

<p>HTTP_X_REAL_IP: 10.6.223.44

<p>REMOTE_ADDR: 8.8.8.7

 

Ø C

基于A如果只nginx(10.6.223.44:8080)配置注释掉“X-Forwarded-For”,输出结果变成:

<p>HTTP_X_FORWARDED_FOR: 10.6.223.44

<p>HTTP_X_REAL_IP: 10.6.223.44

<p>REMOTE_ADDR: 10.6.223.44

 

Ø D

基于A如果只nginx(10.6.208.101:8080)配置注释掉“X-Forwarded-For”,输出结果变成:

<p>HTTP_X_FORWARDED_FOR: 10.6.81.39

<p>HTTP_X_REAL_IP: 10.6.223.44

<p>REMOTE_ADDR: 10.6.81.39

5. 测试2

基于测试1的配置,

当访问路径变成:client(10.6.81.39) -> nginx(10.6.208.101:8080) -> apache(10.6.208.101:80)时,程序输出结果为:

<p>HTTP_X_FORWARDED_FOR: 10.6.81.39

<p>HTTP_X_REAL_IP: 10.6.81.39

<p>REMOTE_ADDR: 10.6.81.39

 

但如果clientHTTP请求头中加入:

X-FORWARDED-FOR:8.8.8.7

CLIENT-IP:8.8.8.8

X-REAL-IP:8.8.8.10

后输出结果变成:

<p>HTTP_X_FORWARDED_FOR: 8.8.8.7, 10.6.81.39

<p>HTTP_X_REAL_IP: 10.6.81.39

<p>REMOTE_ADDR: 8.8.8.7

 

从上可以看出,只配置正确使用了real-ip功能,除HTTP_X_REAL_IP外,其它内容可以被干扰,client可以篡改它们。

6. 结论

如果正确编译和配置了nginx反向代理,当只有一层nginx反向代理时,可以通过“HTTP_X_REAL_IP”取得client的真实IP

 

如果有二层nginx反向代理,则client的真实IP被包含在“HTTP_X_FORWARDED_FOR”中。

 

最不可信的是“REMOTE_ADDR”,它的内容完全可以被client指定!总之只要编译和配置正确,“HTTP_X_FORWARDED_FOR”总是包含了client的真实IP

 

posted on 2015-09-22 18:10  -见  阅读(408)  评论(0编辑  收藏  举报

导航