location /ip {
default_type text/plain;
return 200 $remote_addr;
}
location /ip_json {
default_type application/json;
return 200 "{\"ip\":\"$remote_addr\"}";
}
From scratch:
# Fedora/CentOS/RHEL:
sudo dnf install nginx -y
sudo nano /etc/nginx/nginx.conf
# Debian/Ubuntu:
sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/default
location /ip {
default_type text/plain;
return 200 $remote_addr;
}
location /ip_json {
default_type application/json;
return 200 "{\"ip\":\"$remote_addr\"}";
}
sudo systemctl start nginx
curl 0/ip # Test
curl 0/ip_json # Test
sudo systemctl reload nginx # If you need to apply changes
# From your local machine:
curl MY_SERVER_IP_HERE/ip # Test