Nginx redirect and geolocalization

boris quiroz asked:

I’ve the following problem with Nginx and GeoIP and will be nice if you can give me some pointers about the solution.

Note: I know this can be solved on DNS side, but that won’t be possible by now so I’m looking for other solution.

The problem
My www.domain.com has 1.1.1.1 IP address located in Amazon running Nginx with a redirect like this:

location / {
proxy_pass http://2.2.2.2
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

On 2.2.2.2 there’s another nginx using geoIP. The problem is that all my clients seems to come from 1.1.1.1, as 2nd nginx is geolocating by $remote_addr.

Is there any way to catch real client IP ($remote_addr on 1.1.1.1) and pass it as a different variable to 2.2.2.2 and use it on geolocalization?

Any tip, comment or RTFM is welcome. I’ll keep looking for a solution to try to answer this question.

Thanks.

My answer:


On your upstream nginx, use the RealIpModule to get the real IP out of the X-Forwarded-For header that you are sending from your downstream nginx.

For example:

server {
    set_real_ip_from 1.1.1.1;
    real_ip_header X-Forwarded-For;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.