diff options
author | Dante Catalfamo | 2024-12-02 21:02:11 -0500 |
---|---|---|
committer | Dante Catalfamo | 2024-12-02 21:02:36 -0500 |
commit | acf85c77127fee239e57d1acbd820f527df44c91 (patch) | |
tree | 107179fba9e8aad1ea7036d30aa558ce61b5e6bd | |
parent | c837f34a9224d193bc0911af8a896c2a3f1caf30 (diff) | |
download | blog-acf85c77127fee239e57d1acbd820f527df44c91.tar.gz blog-acf85c77127fee239e57d1acbd820f527df44c91.tar.bz2 blog-acf85c77127fee239e57d1acbd820f527df44c91.zip |
ip-nginx: create article
-rw-r--r-- | content/posts/what-is-my-ip-with-nginx/index.org | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/content/posts/what-is-my-ip-with-nginx/index.org b/content/posts/what-is-my-ip-with-nginx/index.org new file mode 100644 index 0000000..3f0c118 --- /dev/null +++ b/content/posts/what-is-my-ip-with-nginx/index.org @@ -0,0 +1,44 @@ +#+TITLE: Creating a "What Is My IP" website in four lines with nginx +#+DATE: 2024-12-02T20:28:47-05:00 +#+DRAFT: false +#+DESCRIPTION: +#+TAGS[]: nginx +#+KEYWORDS[]: nginx +#+SLUG: +#+SUMMARY: + +I recently wanted to create an endpoint on a development machine that +could return my external IP address. Normally I would use one of a +handful of services https://canhazip.com/ or https://ifconfig.ca/, but +I wanted to see how easy it would be to make my own. As it turns out, +it's incredibly easy, and doesn't even require leaving the reverse proxy. + +Using =nginx=, we can just create a block like this. + +#+begin_src +location /ip { + default_type text/plain; + return 200 "$remote_addr\n"; +} +#+end_src + +Where location =/ip= is the path that returns the IP. + +If we were to put that into a full server configuration, it might look +something like this. + +#+begin_src +server { + listen 80 default_server; + + server_name _; + + location / { + default_type text/plain; + return 200 "$remote_addr\n"; + } +} +#+end_src + +It's really that simple. Now nginx will happily return the requesting +IP address. No need to proxy to another application or service. |