本文將介紹點样利用純 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; server_name ipv4.ddnsip.cn ipv6.ddnsip.cn ddnsip.cn; 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" ; location / { default_type application/json; return 200 '{"ip":"$remote_addr "}' ; } 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; server_name ipv4.ddnsip.cn ipv6.ddnsip.cn ddnsip.cn; add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" ; location / { default_type text/plain; return 200 $remote_addr ; } 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; server_name ipv4.ddnsip.cn ipv6.ddnsip.cn ddnsip.cn; add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" ; location / { default_type text/plain; return 200 $http_user_agent ; } 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.cn 或 https://ddnsip.cn/ua (JSON) IP: https://ddnsip.cn 或 https://ddnsip.cn/json IPv4: https://ipv4.ddnsip.cn 或 https://ddnsip.cn/json IPv6: https://ipv6.ddnsip.cn 或 https://ipv6.ddnsip.cn/json
Linux底下使用直接睇下面鏈接入面嘅說明:https://www.ddnsip.cn
閒嘅冇嘢搞咗個查IP歸屬嘅(唔保證穩定同準確):https://ip.ddnsip.cn/