Skip to content

Instantly share code, notes, and snippets.

@lidopaglia
Created August 7, 2021 15:56
Show Gist options
  • Save lidopaglia/e790e2b31a6ec003554c406445a1a95c to your computer and use it in GitHub Desktop.
Save lidopaglia/e790e2b31a6ec003554c406445a1a95c to your computer and use it in GitHub Desktop.
NGINX script for making your own public IP API

NGINX script for making your own public IP API

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

Paste inside the "server {" encapsulation:

  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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment