Simple My IP Service with NginX

This is a nifty little trick I figured out with the help of Emil on the #nginx irc channel.

We will be returning the visitor ip without hosting any files or using any client or server-side scripting. Only nginx config. This is why I like this solution, the simplicity of it.

nginx.conf

Set up a new vhost (and dns record if you are using a subdomain) and add the following:

location = / {
    add_header Content-Type text/plain; return 200 $remote_addr;
}

Test nginx config:

> sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
> 

Restart nginx:

> sudo systemctl restart nginx.service

Test

Finally, test your service:

> curl ip.devzero.be
178.21.118.33
>
comments powered by Disqus