Today a need arose for a service that would tell us our public IP address. This is such a common question to ask the Internet that Google and DuckDuckGo provide the answer for you directly in the search results, saving you an extra click and TCP connection or two to one of the many websites offering the same information. (Interestingly, neither Yahoo nor Bing have this feature, but thats a different story).
Instead of relying on one search engine or 3rd party site, we looked at
how we can build something of our own to solve this problem. And what
we came up with was rather obvious - make use of the
directive in nginx rewrite module, which can return any given
status code and text to the client.
Excerpt from the
rewrite module doc concerning the use of
1 2 3 4 5
So, based on that, we arrive at the following location block:
1 2 3 4
And here’s the output it produces:
Neat isn’t it? Hey but what if the consumer of this data is a machine instead of a human? Can we make the output more machine-friendly? Yes we can, this location returns a JSON. Oh my! Or, more accurately, 01001111 01101000 00100000 01101101 01111001 00100001, as its your computer yelling that.
1 2 3 4
Don’t just take our word for it, see it for yourself:
1 2 3 4
Now, nginx is our primary webserver for most websites, but we also have Varnish in front for caching. So, can we return client’s IP directly from Varnish and save one network hop up to nginx? Yes we can, but with a small caveat - we loose the cool Guru Meditation error page if we only want to return the IP address alone.
So here’s the VCL that will do this for your:
1 2 3 4 5 6 7 8 9 10
Load that VCL and see it working. Note how the response doesn’t include
a trailing new line character, so our shell prompt character,
ends up on the same line. Thats due to a limitation in VCL, type IP
doesn’t support appending with the + operator; if you try this you’ll
get the following error from the VCL complier:
Operator + not possible on type IP.
The exercise of writing a VCL to return the IP as JSON is left to the reader. Happy meditation.