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 has IP address located in Amazon running Nginx with a redirect like this:

location / {
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 there’s another nginx using geoIP. The problem is that all my clients seems to come from, as 2nd nginx is geolocating by $remote_addr.

Is there any way to catch real client IP ($remote_addr on and pass it as a different variable to 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.


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 {
    real_ip_header X-Forwarded-For;

View the full question and any other answers on Server Fault.

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