summaryrefslogtreecommitdiffstats
path: root/content/posts/what-is-my-ip-with-nginx/index.org
blob: 3f0c1189315ecca2111756993f98a0f2988e93a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.