3

I need to get the ip address of client using my application in the request of hapijs node... we are using pm2 and Nginx for server to run live and we have used the request.info.address but it gives the server local ip as 127.0.0.1 but what i need was the client's IP who uses my application eg: 192.x.x.111 ...

sunilsmith
  • 111
  • 2
  • 15
  • Possible duplicate of [How to determine a user's IP address in node](http://stackoverflow.com/questions/8107856/how-to-determine-a-users-ip-address-in-node) – str May 19 '17 at 08:16

4 Answers4

4

In case if you are running the server behind a Nginx proxy server you would have to use

    req.headers['x-real-ip']

else you can use

req.info.remoteAddress
Partinder Singh
  • 221
  • 2
  • 6
  • i tried this bro but still returns **Undefined** do i need to add any extra header feature in my ajax to get the ip in my handler or is there any special way to get the IP in req for Hapi.Js because all the answers which i surfed are mostly for express.js – sunilsmith May 31 '17 at 10:57
  • Best would be to console your request/header/connection objects and see which all properties are available. Request.info works for hapi but you might want to examine the whole request object to see why it's not working, or you can post your request object here for all to check. – Partinder Singh May 31 '17 at 12:00
4

You should check the configuration on the reverse proxy (nginx) and see if you are sending the ip and how you're doing it.

For example (nginx conf):

server {
    listen       0.0.0.0:80;
    server_name  [your_domain];
    root /webroot/[your_domain or webapp name];
    access_log  /var/log/nginx/[your_domain or webapp name].log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:[port assigned to node];
        proxy_redirect off;
    }
}

In this case, you will get the client's IP via the header X-Real-IP.

So in HapiJS, where you have access to the request object:

const ip = request.headers['x-real-ip'] || request.info.remoteAddress;
luismatute
  • 181
  • 3
  • 7
2

I always get client IP address with this function :)

    return request.headers['x-forwarded-for'] ||
    request.connection.remoteAddress ||
    request.socket.remoteAddress ||
    request.connection.socket.remoteAddress;

Hope it helps.

Sparw
  • 2,688
  • 1
  • 15
  • 34
  • sorry bro @Sparw i have tried these and the results are given below : req.headers['x-forwarded-for'] => 'undefined' request.connection.remoteAddress || request.socket.remoteAddress || request.connection.socket.remoteAddress => these things throwing below error **'Debug: internal, implementation, error TypeError: Uncaught error: Cannot read property 'remoteAddress' of undefined '** – sunilsmith May 19 '17 at 09:26
  • Maybe you have to replace all "request" by "req" ? – Sparw May 22 '17 at 10:47
1

I'm running the same stack for my hapi apps with nginx and PM2. Proxying requests through nginx to a node app on the same host will always result in 127.0.0.1 for the remote address, because nginx forwards the request on the same host.

Solution: there's a dedicated plugin hapi-geo-locate that determines the actual client IP address even though you're running your app behind a reverse proxy (like nginx).

hapi-geo-locate supports various proxies and HTTP headers, so you should be save and get the user's IP running your app on different platforms.

Hope that helps!

Marcus Poehls
  • 461
  • 2
  • 7