本文将介绍如何利用纯 Nginx 搭建 IP 地址查询接口(只查询 IP 地址,不查询 IP 归属地),如果有查询 IP 归属地的需求,可以参考另一位大佬的一篇文章 纯 Nginx 打造 IP 地址查询接口。
安装 Nginx 的方法就不介绍了,直接丢配置文件了
文末有现成的,如果你不想自己搭建的话可以直接使用。
配置示例
获取 IP(JSON)
代码语言:text复制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(纯文本)
代码语言:text复制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
这是额外的内容,闲的没事写的
代码语言:text复制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.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/