I recently wanted to create an endpoint on a development machine that could return my external IP address. Normally I would use one of a handful of services https://canhazip.com/ or https://ifconfig.ca/, but I wanted to see how easy it would be to make my own. As it turns out, it's incredibly easy, and doesn't even require leaving the reverse proxy.
Using nginx
, we can just create a block like this.
location /ip {
default_type text/plain;
return 200 "$remote_addr\n";
}
Where location /ip
is the path that returns the IP.
If we were to put that into a full server configuration, it might look something like this.
server {
listen 80 default_server;
server_name _;
location / {
default_type text/plain;
return 200 "$remote_addr\n";
}
}
It's really that simple. Now nginx will happily return the requesting IP address. No need to proxy to another application or service.