全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 572|回复: 1

[经验] nginx 返回访问 IP

[复制链接]
发表于 2022-3-9 16:09:52 | 显示全部楼层 |阅读模式
  location /
  {   
    default_type "text/plain;charset=utf-8";
        return 200 "current ip: $remote_addr\n";
  }
发表于 2022-3-9 16:16:39 | 显示全部楼层
# nginx 安装geoip 扩展,主配置
  1. geoip2 /etc/GeoIP/data/GeoLite2-City.mmdb {
  2.     auto_reload 30m;
  3.     $geoip2_data_city_continent_code source=$ip continent code;
  4.     $geoip2_data_city_continent_name source=$ip continent names en;
  5.     #$geoip2_data_city_continent_name source=$ip continent names zh-CN;
  6.     $geoip2_data_city_country_code source=$ip country iso_code;
  7.     $geoip2_data_city_country_name source=$ip country names en;
  8.     $geoip2_data_city_region_code source=$ip subdivisions 0 iso_code;
  9.     $geoip2_data_city_region_name source=$ip subdivisions 0 names en;
  10.     #$geoip2_data_city_region_name source=$ip subdivisions 0 names zh-CN;
  11.     $geoip2_data_city_location_latitude source=$ip location latitude;
  12.     $geoip2_data_city_location_longitude source=$ip location longitude;
  13.     $geoip2_data_city_postal source=$ip postal code;
  14.     $geoip2_data_city_city_name source=$ip city names en;
  15.     #$geoip2_data_city_city_name source=$ip city names zh-CN;
  16.     $geoip2_data_city_location_timezone source=$ip location time_zone;
  17.     }
  18.    
  19.     geoip2 /etc/GeoIP/data/GeoIP2-ISP.mmdb {
  20.     auto_reload 30m;
  21.     $geoip2_data_isp_isp source=$ip isp;
  22.     $geoip2_data_isp_org source=$ip organization;
  23.     }
  24.    
  25.     geoip2 /etc/GeoIP/data/GeoLite2-ASN.mmdb {
  26.     auto_reload 30m;
  27.     $geoip2_data_asn_asn source=$ip autonomous_system_number;
  28.     $geoip2_data_asn_org source=$ip autonomous_system_organization;
  29.     $geoip2_data_asn_network source=$ip network;
  30.     }
  31.    
  32.     geoip2 /etc/GeoIP/data/GeoLite2-Country.mmdb {
  33.     auto_reload 30m;
  34.     $geoip2_metadata_country_build source=$ip metadata build_epoch;
  35.     $geoip2_data_country_name source=$ip country names en;
  36.     #$geoip2_data_country_name source=$remote_addr country names zh-CN;
  37.     $geoip2_data_country_code source=$ip default=US country iso_code;
  38.     }
  39.    
  40.     geoip2 /etc/GeoIP/data/GeoIP2-Connection-Type.mmdb {
  41.     auto_reload 30m;
  42.     $geoip2_data_connectiontype source=$ip connection_type;
  43.     $geoip2_data_network source=$ip network;
  44.     }
复制代码


#网站配置
  1. set $ip $remote_addr;
  2.     if ($query_string ~ "ip=(\d+\.\d+\.\d+\.\d+)"){
  3.       set $ip "$1";
  4.       
  5.     }
  6.    
  7.     location ~ ^/ {
  8.     default_type text/plain;
  9.     charset utf-8;
  10.     if ($http_x_real_ip != "") {
  11.         set $ip $http_x_real_ip;
  12.     }
  13.     return 200 "$ip $geoip2_data_country_name $geoip2_data_city_region_name $geoip2_data_city_city_name $geoip2_data_isp_isp $geoip2_data_asn_org";
  14.     }
  15.    
  16.     location = /text {
  17.     default_type text/plain;
  18.     charset utf-8;
  19.     if ($http_x_real_ip != "") {
  20.         set $ip $http_x_real_ip;
  21.     }
  22.     return 200 "$ip\n $geoip2_data_country_name\n $geoip2_data_city_country_code\n $geoip2_data_city_region_name\n $geoip2_data_city_region_code\n $geoip2_data_city_city_name\n $geoip2_data_city_location_latitude\n $geoip2_data_city_location_longitude\n $geoip2_data_city_location_timezone\n AS$geoip2_data_asn_asn\n $geoip2_data_isp_isp\n $geoip2_data_asn_org\n $geoip2_data_connectiontype\n $http_user_agent\n";
  23.     }
  24.    
  25.     location = /json {
  26.    
  27.     default_type application/json;
  28.     charset utf-8;
  29.     if ($http_x_real_ip != "") {
  30.         set $ip $http_x_real_ip;
  31.     }

  32.     return 200 '{\n "ip": "$ip",\n "country": "$geoip2_data_country_name",\n "country_code": "$geoip2_data_city_country_code",\n "region": "$geoip2_data_city_region_name",\n "region_code": "$geoip2_data_city_region_code",\n "city": "$geoip2_data_city_city_name",\n "latitude": "$geoip2_data_city_location_latitude",\n "longitude": "$geoip2_data_city_location_longitude",\n "time_zone": "$geoip2_data_city_location_timezone",\n "asn": "AS$geoip2_data_asn_asn",\n "isp": "$geoip2_data_isp_isp",\n "org": "$geoip2_data_asn_org",\n "connection_type": "$geoip2_data_connectiontype",\n "user_agent": "$http_user_agent"\n}';
  33.     }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-5-11 07:10 , Processed in 0.057318 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表