whoami with nginx
Out of the cookbook of services that you need from time to time there is another service which I decided to host myself: whoami. While “whoami” is a rather broadly used term with different meanings in this particular case it means: “what is my external IP address?”.
One way to find out is to use a service that returns your external IP address as a text string. Luckily this can be easily implemented in nginx. The configuration is rather simple:
|
|
The most important lines are 5 and 6. These tell nginx to return1 a text string with the remote address (external IP) of the visitor. The \n
after $remote_addr
adds a end-of-line character to prevent issues with programs not recognizing the end of file otherwise. All other lines are commonly used nginx settings in order to use SSL, http2 etc. Further information on these commands is available from the nginx documentation.
So how do I get my IP address once everything is running? For instance you could use curl to get your IPv4 (using the -4
flag) or IPv6 (using the -6
flag) address:
curl -6 https://whoami.mydomain.tld
We make use of the nginx return function which is shipped with the http_rewrite_module module. As long as you have not explicitly disabled building the module it should be available in your installation. ↩︎