I'm making an Aqueduct server. I'd like to know the IP address of the client request for monitoring logs and for providing general location based content.
In Aqueduct I don't see how to get the IP address.
I was going to say that I tried X, Y, and Z and that didn't work, but I just found the answer so I will add it below.
In your controller you can get the IP address from the Request
.
String ipAddress = request.connectionInfo.remoteAddress.address;
Note that if your server is running behind an nginx proxy then you will have to configure nxinx to forward the real IP to Aqueduct. To do that you can add the X-Real-IP
header with the remote address to your proxied API location in the server block.
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
}
Then in Aqueduct you can get the IP address from the raw headers:
String ipAddress = request.raw.headers['x-real-ip'].first;
More info here: