[SOLVED] Meteor be hide Nginx, get client ip address

#1

In a meteor method runs on server, I’m trying to get client ip address but it doesn’t work.

Meteor.methods({
  'testClientIpAddress': () => {
    console.log(this.connection); // I got undefined all the time.
    if (this.connection) {
      return this.connection.clientAddress;
    }
    return null;
  },

here is my nginx config

server {
  listen 443;
  listen [::]:443;
  
  # some ssl, domain goes here
  
  location / {
    # my meteor runs on port 3500
    proxy_pass http://localhost:3500;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forward-Proto http;
    proxy_set_header X-Nginx-Proxy true;

    proxy_redirect off;
  }
}
#2

Did you set the Meteor environment variable HTTP_FORWARDED_COUNT to 1 ?

#3

Here is my service config:

Environment=NODE_ENV=production
Environment=PORT=3500
Environment=HTTP_FORWARDED_COUNT=1
#4

I’ve found the solution: in the method, we can access this variable:

Meteor.server.stream_server.open_sockets[0].headers

it looks like this:

{
  'x-forwarded-for': 'xxx.xxx.xxx.xxx',
  'x-real-ip': 'xxx.xxx.xxx.xxx',
  host: 'domain.com',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 S...'
  'accept-language': 'en-US,en;q=0.9,vi;q=0.8' }
1 Like