自建一个IP 地址显示 API

发布于 2023-07-24  569 次阅读


使用工具:aapanel+nginx

首先,我们新建一个站点

点击左侧Config,在

access_log  /www/wwwlogs/test.igewu.org.log;
error_log  /www/wwwlogs/test.igewu.org.error.log;

前面插入如下代码:

普通格式返回

location / {
    default_type text/plain;
    return 200 $remote_addr;
}

JSON格式返回

location /json {
    default_type application/json;
    return 200 "{\"ip\":\"$remote_addr\"}";
}

点击Save保存即可

使用curl yoururl 将以普通格式返回

使用curl yoururl/json将以json格式返回

2023/8/25更新:

网站在套用cloudflare cdn后使用以下方法获取真实ip

cd /www/server/nginx/conf

新建文件 cf.conf,添加以下内容保存

#Cloudflare
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;

# use any of the following two
real_ip_header CF-Connecting-IP;
#real_ip_header X-Forwarded-For;

aapanel打开nginx管理,选择配置修改,在http下添加 include cf.conf;然后点击保存。

http
	{


        include       mime.types;
		#include luawaf.conf;

		include cf.conf;

		include proxy.conf;
......

最后reload nginx就好了