让客户真正的ip落后于上游

时间:2014-05-12 06:12:58

标签: java unix nginx

如何在nginx中将客户端的ip放在上游?

现在我使用下一个配置:

#upstream listener

upstream ts {
        server localhost:841 weight=10 max_fails=3 fail_timeout=10s;
        server 10.1.3.209:8080 backup;
}

#service listener

server {
        listen 80;
        server_name 10.9.24.45;

        location / {
                proxy_pass http://ts;
                proxy_set_header Host $host;
                proxy_set_header X-Real_IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

#app listener

server {
        listen 841;
        server_name localhost;

        charset utf-8;

        access_log /var/log/nginx/access_ts.log;
        error_log /var/log/nginx/error_ts.log;

        client_max_body_size 16M;

        set $www /www;
        set $root $www/apps/ts/ROOT;

        include /etc/nginx/sites-available/locations;

        location / {
                proxy_pass http://localhost:840;
                proxy_set_header Host $host;
                proxy_set_header X-Real_IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                include /etc/nginx/sites-available/location-config;
        }
}

在我的java应用程序中,我使用X-Real_IP标头来获取客户端的ip并且一切正常,但在我开始使用上游的另一个配置后,我只看到127.0.0.0。认为这是正确的,但我需要为我的应用程序获得一个真正的IP。

如何在nginx上游获得真正的IP?

我看到了一些像proxy_set_header realip $ remote_addr这样的答案,但是如果没有我的java应用程序更改,我该怎么做。

1 个答案:

答案 0 :(得分:1)

我有同样的问题,问题是你转发的标题改变

proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

分为:

proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;