env['REMOTE_ADDR'] with Goliath ruby

178 Views Asked by At

I have an API with Goliath gem (ruby) and I want to get the ip of the movile which is calling to my API. The case is, env['REMOTE_ADDR'] always give me 127.0.0.1 when some device is calling me. It shoud be the ip from the mobile is calling me, right?

Any help please?

Thanks in advance!

1

There are 1 best solutions below

0
On

The problem was with proxying through Nginx. I had to change the Nginx proxy configuration as follows.

upstream app_xxx {
   server 127.0.0.1:3000;
}

server {
   listen 80;

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

The important thing is: The real IP is in the X-Real-IP parameter. So you have to access it as:

env['X-Real-IP']