如何仅使用 nginx 制作你自己的 '获取我当前 IP 地址' 服务器
注意:在某些配置中,这会为 IPv4 请求返回例如 ::ffff:1.2.3.4,我尚未解决此问题
此 nginx 配置将向用户返回用户的 IP 地址。注意它在反向代理后不起作用,它实际上需要监听其服务域名的 IP 地址的端口 80 和/或 443。
get_my_ip.conf
location = /api/get-my-ip {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 200 '$remote_addr';
}这是返回 JSON 的变体:
get_my_ip_json.conf
location = /api/get-my-ip-json {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 200 '{"ip": "$remote_addr"}';
}Check out similar posts by category:
Networking, Nginx
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow