Set up your own what is my ip service with just nginx

Revision history
Tags: nginx whatismyip

How not to do it with nginx

I wanted to host my own What is my ip service. At first I made a Node.js web server which returned the remote address of the request.

const app = express()
app.get('/', (req, res) => {
  res.send(`Your IP address is: ${res.locals.clientip}`)

However, while reading the list of variables portion of the nginx documentation, it occured to me how overkill my Node.js solution was, especially since I was already proxying the requests to the Node.js HTTP server via nginx.

The good way

The $remote_addr variable can simply be returned with the return directive:

return 200 $remote_addr\n;

Also appending a newline at the end there. Below is the nginx configuration I use for serving

server {
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;


  ssl_certificate     /letsencrypt/live/;
  ssl_certificate_key /letsencrypt/live/;

  keepalive_requests 0;

  location / {
    default_type text/plain;
    return 200 $remote_addr\n;

The service returns something similar to this

$ curl -i
HTTP/2 200 
server: nginx
date: Fri, 24 Nov 2017 01:16:22 GMT
content-type: text/plain; charset=utf-8
content-length: 13
x-number: 42

If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).

Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.