Use nginx to obtain your own public IP address

Original link: https://hellodk.cn/post/1133

For broadband Internet access, generally speaking, we are used to using the following websites to obtain our own public IP,

You can also get your own public IP by curling a server address on the command line. What we want to talk about today is this kind of service.

A well-known domain name, I remember, is ip.sb , and you can directly execute curl ip.sb on a terminal that supports curl (the default terminal for Linux and macOS is fine, and you can use Git Bash for Windows).

But ip.sb does not know when it is connected to cloudflare. Sometimes our network may not be well connected to cf’s cdn node, so it will not be returned for a long time. Another important impact is the address returned by curl ip.sb It may be the IP of the proxy node, not the public network IP of the broadband I use . I have a Tencent Cloud Nanjing server, why not use my own domain name to do this kind of function.

It is very simple to implement. Add the file myip.conf under /etc/nginx/sites-enabled with the following content

 server { listen 80; listen [::]:80; server_name host.940304.xyz; server_tokens off; location = /ip { add_header Content-Type text/plain; access_log off; return 200 "$remote_addr\n"; } }

After executing nginx -t and nginx -s reload , you’re done.

Execute curl host.940304.xyz/ip on the terminal to get your own IP address. This is the domain name and address I made myself. I shouldn’t forget it anymore. ?

My IP.jpg

This article is transferred from: https://hellodk.cn/post/1133
This site is only for collection, and the copyright belongs to the original author.