本文將介紹點样利用純 Nginx 搭建 IP 地址查詢接口(只查詢 IP 地址,唔查詢 IP 歸屬地),如果有查詢 IP 歸屬地嘅需求,可以參考另一位大佬嘅一篇文章 純 Nginx 打造 IP 地址查詢接口

好耐冇見,呢兩個月由於啲咩原因(考試、面試)一直冇有更新博客,今日終於有時間咗,就趕緊嚟更新。用 Nginx 嚟攞 IP 呢個方法係去年 8 月份喺處理DDNS Openwrt 插件無法更新 IP 嘅 case 時了解到嘅,當時插件裏頭攞 IP 嘅接口 ns1.dnspod.net:6666 由於係早啲年寫嘅一個程序僅支持 http/0.9 導致 DDNS 插件無法正常攞 IP 地址,之後一位大佬改用 Nginx 嚟攞 IP 地址,嗰次先知道 Nginx 原來仲可以噉用,簡直係學藝唔精哈哈哈。

安裝 Nginx 嘅方法就唔介紹咗,直接丟配置文件咗。

文末有現成嘅,如果你唔想自己搭建嘅話可以直接使用。

配置范例

获取 IP(JSON)

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
server {
listen 80;
listen [::]:80;

listen 443 ssl http2;
listen [::]:443 ssl http2;

# 以支持 HTTP/3,若所用 Nginx 版本支持 HTTP/3,可删掉注释
# listen 443 http3;
# listen [::]:443 http3;

server_name ipv4.ddnsip.cn ipv6.ddnsip.cn ddnsip.cn;

# 以支持 HTTP/3,若所用 Nginx 版本支持 HTTP/3,可删掉注释
# add_header Alt-Svc 'h3=":443"; ma=86400';

# HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

# 许跨域(喺其他站点调用接口时会使到)
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";

# 获取 IP 地址
location / {
default_type application/json;
return 200 '{"ip":"$remote_addr"}';
# 若使緊 CDN 请将$remote_addr改为$http_x_forwarded_for
}

# 证书配置
ssl_certificate /root/.acme.sh/*.ddnsip.cn/fullchain.cer;
ssl_certificate_key /root/.acme.sh/*.ddnsip.cn/*.ddnsip.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
}

获取 IP(纯文本)

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
server {
listen 80;
listen [::]:80;

listen 443 ssl http2;
listen [::]:443 ssl http2;

# 用以支援 HTTP/3,若所用 Nginx 版本支援 HTTP/3,可去除注释
# listen 443 http3;
# listen [::]:443 http3;

server_name ipv4.ddnsip.cn ipv6.ddnsip.cn ddnsip.cn;

# 用以支援 HTTP/3,若所用 Nginx 版本支援 HTTP/3,可去除注释
# add_header Alt-Svc 'h3=":443"; ma=86400';

# HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

# 获取 IP 地址
location / {
default_type text/plain;
return 200 $remote_addr;
# 若使用 CDN 请将$remote_addr改为$http_x_forwarded_for
}

# 证书配置
ssl_certificate /root/.acme.sh/*.ddnsip.cn/fullchain.cer;
ssl_certificate_key /root/.acme.sh/*.ddnsip.cn/*.ddnsip.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
}

获取 UA

呢啲係额外嘅内容,得閒冇嘢做嘅时候写嘅

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
server {
listen 80;
listen [::]:80;

listen 443 ssl http2;
listen [::]:443 ssl http2;

# 用嚟支持 HTTP/3,如果用緊嗰個 Nginx 版本支持 HTTP/3,可以拎走註解
# listen 443 http3;
# listen [::]:443 http3;

server_name ipv4.ddnsip.cn ipv6.ddnsip.cn ddnsip.cn;

# 用嚟支持 HTTP/3,如果用緊嗰個 Nginx 版本支持 HTTP/3,可以拎走註解
# add_header Alt-Svc 'h3=":443"; ma=86400';

# HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

# 攞 UA(純文字格式,想要 JSON 格式就用注釋咗嗰啲碼)
location / {
default_type text/plain;
return 200 $http_user_agent;
}

# 攞 UA(JSON 格式)
# location / {
# default_type application/json;
# return 200 '{"ua":"$http_user_agent"}';
# }

# SSL 配置
ssl_certificate /root/.acme.sh/*.ddnsip.cn/fullchain.cer;
ssl_certificate_key /root/.acme.sh/*.ddnsip.cn/*.ddnsip.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
}

附言

睇落好似好複雜噉,係咪懶得寫(/手動狗頭,冇關係,呢度有現成嘅
UA: https://ua.ddnsip.cnhttps://ddnsip.cn/ua (JSON)
IP: https://ddnsip.cnhttps://ddnsip.cn/json
IPv4: https://ipv4.ddnsip.cnhttps://ddnsip.cn/json
IPv6: https://ipv6.ddnsip.cnhttps://ipv6.ddnsip.cn/json

Linux底下使用直接睇下面鏈接入面嘅說明:
https://www.ddnsip.cn

閒嘅冇嘢搞咗個查IP歸屬嘅(唔保證穩定同準確):
https://ip.ddnsip.cn/