• Nginx启用Geoip2模块实现国家城市识别 —— 筑梦之路


    推荐使用最新版,有些数据会变化

    1. # 安装 api 库 1.6.0版本
    2. tar -zxf libmaxminddb-1.6.0.tar.gz
    3. cd libmaxminddb-1.6.0
    4. ./configure && sudo make && sudo make install
    5. echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
    6. ldconfig
    7. # 下载模块
    8. wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
    9. # 编译nginx
    10. ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --add-module=/data/geoip2/ngx_http_geoip2_module-3.4
    11. make
    12. make install
    13. # 配置示例
    14. #http模块
    15. #确认需要使用的IP,不推荐使用这种方式
    16. #realip取值:若x-forward-for不为空,则取第一个ip;否则取remote_addr
    17. map $http_x_forwarded_for $realip {
    18. ~^(\d+\.\d+\.\d+\.\d+) $1;
    19. default $remote_addr;
    20. }
    21. #http模块
    22. #引入geoip2的配置文件,配置文件内容见下面部分
    23. include /etc/nginx/conf.d/geoip2.conf;
    24. #location模块
    25. #把城市国家信息放入header
    26. #geoip2相关变量定义在下面部分
    27. proxy_set_header Country_Code $geoip2_country_code;
    28. proxy_set_header City_Code $geoip2_city_name_en;
    29. # 添加geoip2基础配置
    30. 解压 GeoLite2-City_20220111.tar.gz GeoLite2-Country_20220111.tar.gz2个文件,把2个.mmdb格式的文件放入任意目录(如/data/geo2_db/)
    31. #配置geoip2配置文件 /etc/nginx/conf.d/geoip2.conf
    32. geoip2 /data/geo2_db/GeoLite2-Country.mmdb {
    33. auto_reload 5m;
    34. $geoip2_metadata_country_build metadata build_epoch;
    35. #国家编码
    36. $geoip2_country_code source=$realip country iso_code;
    37. #国家英文名
    38. $geoip2_country_name_en source=$realip country names en;
    39. #国家中文名
    40. $geoip2_country_name_cn source=$realip country names zh-CN;
    41. }
    42. geoip2 /data/geo2_db/GeoLite2-City.mmdb {
    43. $geoip2_metadata_city_build metadata build_epoch;
    44. #城市英文名,大多是拼音,有重复情况
    45. $geoip2_city_name_en source=$realip city names en;
    46. #城市中文名,部分城市没有中文名
    47. $geoip2_city_name_cn source=$realip city names zh-CN;
    48. #城市id,maxmaind 库里的id,非国际标准
    49. $geoip2_data_city_code source=$realip city geoname_id;
    50. }
    51. # 测试
    52. #添加测试location,把地理位置返回
    53. location = /geo {
    54. default_type text/plain;
    55. # return 200 "haha";
    56. return 200 'countryCode:$geoip2_country_code\n countryNameEn: $geoip2_country_name_en\n countryNameCn: $geoip2_country_name_cn\n cityNameEn: $geoip2_city_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode: $geoip2_data_city_code\n'
    57. }
    58. #测试nginx配置是否正确
    59. sudo /usr/local/nginx/sbin/nginx -t
    60. #若无问题则会输出
    61. #nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    62. #nginx: configuration file /etc/nginx/nginx.conf test is successful
    63. #启动nginx
    64. sudo /usr/local/nginx/sbin/nginx
    65. # #无域名的可以使用host测试
    66. curl -H "X-Forwarded-For: 124.72.237.255,42.248.50.9,202.5.56.71" your.domain.com/geo
    1. #nginx 配置示例
    2. #user nobody;
    3. worker_processes 2;
    4. #error_log logs/error.log;
    5. #error_log logs/error.log notice;
    6. #error_log logs/error.log info;
    7. #pid logs/nginx.pid;
    8. load_module modules/ngx_http_geoip2_module.so;
    9. events {
    10. worker_connections 4096;
    11. }
    12. http {
    13. include mime.types;
    14. default_type application/octet-stream;
    15. log_format main ' 1234mutouren "$http_x_forwarded_for" - $remote_addr - $remote_user [$time_local] "$request" '
    16. '$status $body_bytes_sent "$http_referer" '
    17. '"$http_user_agent" "$http_x_forwarded_for" '
    18. '"$connection" "$request_time"'
    19. ' - "$geoip2_country_name_en" "$geoip2_city_name_en" '
    20. ' = "$geoip2_longitude" "$geoip2_latitude"' ;
    21. #access_log logs/access.log main;
    22. log_format json_logs escape=json
    23. '{"@timestamp":"$time_iso8601",'
    24. '"host":"$hostname",'
    25. '"server_ip":"$server_addr",'
    26. '"client_ip":"$remote_addr",'
    27. '"remote_user":"$remote_user",'
    28. '"xff":"$http_x_forwarded_for",'
    29. '"domain":"$host",'
    30. '"url":"$uri",'
    31. '"referer":"$http_referer",'
    32. '"upstreamtime":"$upstream_response_time",'
    33. '"responsetime":"$request_time",'
    34. '"request_method":"$request_method",'
    35. '"status":"$status",'
    36. '"size":"$body_bytes_sent",'
    37. '"request_length":"$request_length",'
    38. '"protocol":"$server_protocol",'
    39. '"upstreamhost":"$upstream_addr",'
    40. '"file_dir":"$request_filename",'
    41. '"http_user_agent":"$http_user_agent",'
    42. #获取国家英文名称
    43. '"geoip2_country_name_en":"$geoip2_country_name_en",'
    44. '"number":"$status",'
    45. #获取城市英文名称
    46. '"name":"$geoip2_city_name_en",'
    47. #获取经纬度
    48. '"logLat":"$geoip2_longitude,$geoip2_latitude"'
    49. '}';
    50. sendfile on;
    51. #tcp_nopush on;
    52. #keepalive_timeout 0;
    53. keepalive_timeout 300;
    54. client_body_buffer_size 15M;
    55. client_body_temp_path clientpath 3 2;
    56. client_max_body_size 30M;
    57. gzip off;
    58. gzip_static on;
    59. gzip_min_length 10k;
    60. gzip_buffers 4 16k;
    61. gzip_comp_level 6;
    62. gzip_types *;
    63. gzip_disable "MSIE [1-6]\.";
    64. gzip_vary on;
    65. # 配置解析的IP地址,作为获取地理信息的IP地址:
    66. map $http_x_forwarded_for $realip {
    67. ~^(\d+\.\d+\.\d+\.\d+) $1;
    68. default $remote_addr;
    69. }
    70. # 配置国家和城市检索需要的数据文件:
    71. #测试 mmdblookup --file /data/app/geo2db/GeoLite2-City.mmdb --ip 202.175.105.131
    72. geoip2 /data/app/geo2db/GeoLite2-Country.mmdb {
    73. auto_reload 5m;
    74. $geoip2_metadata_country_build metadata build_epoch;
    75. #国家编码
    76. $geoip2_country_code source=$realip country iso_code;
    77. #国家英文名
    78. $geoip2_country_name_en source=$realip country names en;
    79. #国家中文名
    80. $geoip2_country_name_cn source=$realip country names zh-CN;
    81. }
    82. geoip2 /data/app/geo2db/GeoLite2-City.mmdb {
    83. $geoip2_metadata_city_build metadata build_epoch;
    84. #城市英文名,大多是拼音,有重复情况
    85. $geoip2_city_name_en source=$realip city names en;
    86. #城市中文名,部分城市没有中文名
    87. $geoip2_city_name_cn source=$realip city names zh-CN;
    88. #城市id,maxmaind 库里的id,非国际标准
    89. $geoip2_data_city_code source=$realip city geoname_id;
    90. #经度,longitude
    91. $geoip2_longitude source=$realip location longitude ;
    92. #维度,latitude
    93. $geoip2_latitude source=$realip location latitude ;
    94. }
    95. # HTTPS server
    96. #
    97. server {
    98. listen 443 ssl;
    99. server_name XXX.XXX.com;
    100. # server_name XXX.XXX.com;
    101. ssl_certificate cert/data.com.pem;
    102. ssl_certificate_key cert/data.com.key;
    103. ssl_session_timeout 5m;
    104. ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256::!MD5;
    105. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    106. ssl_prefer_server_ciphers on;
    107. access_log /data/log/nginx/access.log json_logs;
    108. location / {
    109. try_files $uri $uri/ /index.html;
    110. root /data/app/pos ;#静态资源目录
    111. index index.html index.htm;
    112. }
    113. location /api/pos/ {
    114. proxy_pass http://10.50.XXX.XXX:9999/;
    115. proxy_redirect default;
    116. proxy_set_header Host $host;
    117. proxy_set_header X-Real-IP $remote_addr;
    118. }
    119. location /WeChat {
    120. alias /data/app/WeChat ;#静态资源目录
    121. index cfca.html;
    122. }
    123. }
    124. server {
    125. listen 80;
    126. location /nginx_status {
    127. stub_status on;
    128. access_log off;
    129. }
    130. access_log logs/access.log json_logs;
    1. # 测试URI
    2. location = /geo {
    3. default_type text/plain;
    4. return 200 'countryCode:$geoip2_country_code\n countryNameEn: $geoip2_country_name_en\n countryNameCn: $geoip2_country_name_cn\n cityNameEn: $geoip2_city_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode: $geoip2_data_city_code\n';
    5. }
    6. }
    7. }
    8. # 测试
    9. #curl -H "X-Forwarded-For: 124.72.XXX.255,42.248.50.9,202.5.XX.71" 10.XX.133.159/geo
    10. countryCode:CN
    11. countryNameEn: China
    12. countryNameCn: 中国
    13. cityNameEn: xxzhou
    14. cityNameCn: XX市
    15. cityCode: 18x0821

     

     参考资料:

    Nginx启用Geoip2,实现国家、城市识别 - 简书

    Nginx的geo2模块实现获取地理信息

    搭建日志聚合grafana&loki 收集Nginx日志_Zz_糖小七的博客-CSDN博客_grafana nginx日志

    Grafana展示精美的nginx访问日志图表_code小博客的技术博客_51CTO博客 

    Nginx配置GeoIP库或者直接通过修改Logstash将日志写入ES - 走看看 

    grafana展示ES中的nginx日志-地图展示_大葱的技术博客_51CTO博客 

    https://www.jb51.net/article/241689.htm 

    Nginx GEOIP Statistic dashboard for Grafana | Grafana Labs

    grafana+elk 监控nginx | 碎玉轩·华裳绕指柔 

  • 相关阅读:
    Elasticsearch对于大数据量(上亿量级)的聚合如何实现?
    【Flink系列】JDBC写入调优
    OpenGL - Coordinate Systems
    win10中ros与ubuntu中ros通信
    华为OD机考题HJ17 坐标移动
    阿里技术官首发的Java核心框架指导手册,为了大厂得码住学起来~
    [NOIP1999 普及组] 导弹拦截
    在职读博|中国社科院-英国斯特大学灵合作办学双证管理学博士
    ABAP 基本数据类型
    计算机操作系统面试题库和答案
  • 原文地址:https://blog.csdn.net/qq_34777982/article/details/125421237