0

I have an akka-spray server behind nginx in a docker image running on ElasticBeanstalk. I'm trying to get the client IP with this:

  (path("myip") & get) {
      clientIP {     
        ip => complete("Client's ip is " + ip.toOption.map(_.getHostAddress).getOrElse("unknown"))  
      }
  } ~

My config also has remote-address-header = on

My ngingx.conf is

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {

        listen 80;

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

Pointing my browser at the /myip endpoint gives me the wrong ip address. It's giving a 172.something, which I assume is the load balancer. It's certainly not my browser. Should I be using something other than .getHostAddress? Using the raw ip gives the same, wrong, value. Thanks.

thund
  • 1,842
  • 2
  • 21
  • 31
  • That bit of `spray` code looks like the sample code from their [own documentation](http://spray.io/documentation/1.2.4/spray-routing/misc-directives/clientIP/), so I assume it's correct. Your problem is probably using ELB and nginx in conjunction, as `172.x.x.x` is a private IP address, and depending on your nginx configuration, it's likely that `getHostAddress` is actually returning your instance/container's own IP address. Can you show the nginx configuration you've set up in your Docker container? – wkl Dec 19 '16 at 03:24
  • Yes, the spray code is from their own docs, but I wasn't sure whether it had been superseded. Their docs are sometimes not updated. I've edited my question to add my nginx.conf. – thund Dec 19 '16 at 17:15

0 Answers0