嘟嘟社区

[经验] nginx 返回访问 IP


  location /
  {   
    default_type "text/plain;charset=utf-8";
        return 200 "current ip: $remote_addrn";
  }
# 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 "$ipn $geoip2_data_country_namen $geoip2_data_city_country_coden $geoip2_data_city_region_namen $geoip2_data_city_region_coden $geoip2_data_city_city_namen $geoip2_data_city_location_latituden $geoip2_data_city_location_longituden $geoip2_data_city_location_timezonen AS$geoip2_data_asn_asnn $geoip2_data_isp_ispn $geoip2_data_asn_orgn $geoip2_data_connectiontypen $http_user_agentn";
  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.     }

复制代码