目录

前言

一、在Linux中创建存放nginx模块文件夹

存在就不需要再次创建。

将所有nginx额外模块包都放到这个目录下

二、配置host文件代理,避免访问github太慢(已配置了跳过)

三、安装 api 库 1.7.1最新版本

四、安装GeoIp模块的Country和City数据库

五、安装GeoIp2模块

六、测试libmaxminddb库地理位置api和数据库功能

七、到此nginx额外模块下载安装完毕!!!

八、安装1.2版本的nginx

1、操作nginx安装包

2、修改nginx的configure默认配置

3、执行./configure命令时出现的问题

问题一:

问题二:

4、编译并安装nginx

5、进入到安装后的nginx文件夹内,开始配置nginx.conf

九、测试访问nginx网页

后记


前言

目的:为了获取访问者IP及访问地区信息。

网上的信息鱼龙混杂,我学习并整合后在这记录一下,方便回忆。同时给需要这项技术的同行留个门。

实践的操作机器系统配置(腾讯云轻量应用服务器)

一、在Linux中创建存放nginx模块文件夹

存在就不需要再次创建。

mkdir /etc/nginx && mkdir /etc/nginx/geoip

将所有nginx额外模块包都放到这个目录下

cd /etc/nginx/geoip

二、配置host文件代理,避免访问github太慢(已配置了跳过)

vim /etc/hosts

在此处段落下添加以下代理:

140.82.113.4 github.com
140.82.121.3 nodeload.github.com
140.82.114.4 api.github.com

ip可以用站长工具查找独立访问github的ip

多个地点ping[github.com]服务器-网站测速-站长工具

保存文件执行命令重启服务器网络

/etc/init.d/network restart

三、安装 api 库 1.7.1最新版本

官方下载地址

  • libmaxminddb(api库) https://github.com/maxmind/libmaxminddb/releases

我这里是下载1.7.1版本

wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz

下载后进行解压并编译安装

  1. tar -zxvf libmaxminddb-1.7.1.tar.gz

  2. cd libmaxminddb-1.7.1/

  3. ./configure && make && make install

  4. echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf

四、安装GeoIp模块的Country和City数据库

官方下载地址

  • DB https://www.maxmind.com/en/account/login 登录后选择Download Databases可下载

我这里是下载20221011版本

wget https://pan.k8scn.work:15443/d/ChinaNetCould/datafiles/geoip-20221011/GeoLite2-City_20221011.tar.gz

wget https://pan.k8scn.work:15443/d/ChinaNetCould/datafiles/geoip-20221011/GeoLite2-Country_20221011.tar.gz

tar -zxvf GeoLite2-City_20221011.tar.gz

tar -zxvf GeoLite2-Country_20221011.tar.gz

mv GeoLite2-City_20221011 GeoLite2-City

mv GeoLite2-Country_20221011 GeoLite2-Country

五、安装GeoIp2模块

官方下载地址

  • geoip2_module https://github.com/leev/ngx_http_geoip2_module/releases

我们这里下载geoip3.4版本

wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz

tar -zxvf 3.4.tar.gz

六、测试libmaxminddb库地理位置api和数据库功能

mmdblookup --file /etc/nginx/geoip/GeoLite2-City/GeoLite2-City.mmdb --ip 8.8.8.8

七、到此nginx额外模块下载安装完毕!!!

八、安装1.2版本的nginx

切换linux路径

cd /usr/local

官方下载地址

Index of /download/

我下载的地址

wget http://nginx.org/download/nginx-1.23.2.tar.gz

nginx的安装可以看我的另一篇有关nginx安装的博客。这里不用yum安装(个人不习惯觉得太乱了)。tar包手动下载安装的好处在于都在同一个文件下/usr/nginx

1、操作nginx安装包

tar -zxvf nginx-1.23.2.tar.gz

cd nginx-1.23.2/

2、修改nginx的configure默认配置

一定要仔细对照配置的文件路径。配置文件很长,各个模块的引用路径一定要正确,copy到记事本上看清楚咯再把命令复制过去,仔细再仔细!!!

照我步骤来解压重命名文件名的可以直接使用这个代码

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --modules-path=/usr/local/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/logs/nginx.pid --lock-path=/usr/local/nginx/logs/nginx.lock --http-client-body-temp-path=/usr/local/nginx/client_body_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-dynamic-module=/etc/nginx/geoip/ngx_http_geoip2_module-3.4

我这里开启了很多模块如果出现没有模块引用库的可以百度再去云下载即可。

3、执行./configure命令时出现的问题

问题一:

./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.

解决:

yum install gd gd-devel

问题二:

/configure: error: the HTTP XSLT module requires the libxml2/libxslt

解决:

yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel

解决完模块无引用文件的问题后再执行./configue修改nginx配置命令 ,成功!!!

4、编译并安装nginx

make && make install

5、进入到安装后的nginx文件夹内,开始配置nginx.conf

编译并安装后的文件就是这个nginx文件夹,与nginx-1.23.2文件夹同级

cd /usr/local/nginx

配置nginx.conf文件。

vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;
#添加geoip2模块
load_module /usr/local/nginx-1.23.2/objs/ngx_http_geoip2_module.so;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for",经纬度:"$geoip2_longitude" "$geoip2_latitude"';log_format json_logs escape=json'{''"访问时间":"$time_iso8601",''"访问者IP":"$remote_addr",''"访问页面":"$uri",''"访问者所处国家英文名":"$geoip2_country_name_cn | $geoip2_country_name_en",''"访问者所在城市英文名":"$geoip2_city_name_cn | $geoip2_city_name_en",''"访问者所处经纬度":"$geoip2_longitude,$geoip2_latitude"''"请求返回时间":"$request_time /S",''"请求方法类型":"$request_method",''"请求状态":"$status",''"请求体大小":"$body_bytes_sent /B",''"访问者搭载的系统配置和软件类型":"$http_user_agent",''"虚拟服务器IP":"$server_addr","$http_x_forwarded_for"''}';    access_log  logs/access.log  main;sendfile        on;keepalive_timeout  60;#请求体缓存大小client_body_buffer_size 20M;client_body_temp_path clientpath 3 2;#请求体最大内存大小client_max_body_size 20M;#nginx反攻击策略# 创建一个10MB大小的请求记录zone, 限制同一IP的访问每秒钟10次limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;#同时限制虚拟服务器的请求处理速率:每秒钟30次limit_req_zone $server_name zone=perserver:10m rate=30r/s; #gzip压缩消耗服务器cpu性能压缩网页静态资源,换取用户获取大文件资源速度#开启gzipgzip  on;  #低于100kb的资源不压缩 gzip_min_length 100k;#压缩级别1-9,越大压缩率越高,同时消耗cpu资源也越多,建议设置在5左右。 gzip_comp_level 5; #需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片.gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;  #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)gzip_disable "MSIE [1-6]\.";  #是否添加“Vary: Accept-Encoding”响应头gzip_vary on; proxy_set_header Host $host;#Remote Address :HTTP协议没有IP的概念, Remote Address 来自于TCP连接,表示与服务端建立TCP连接的设备IP,因此,Remote Address无法伪造。#X-Real-IP :HTTP代理用于表示与它产生TCP连接的设备IP,可能是其他代理,也可能是真正的请求端proxy_set_header X-Real-IP $remote_addr;#X-Forwarded-For:Nginx追加上去的,但前面部分来源于nginx收到的请求头,这部分内容不是 很可信.符合IP格式的才可以使用,否则容易引发 XSS 或者 SQL注入漏洞.proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#配置realip模块真实ip获取方式#配置代理服务器,避免获取到代理服务器ipset_real_ip_from 43.143.148.109;real_ip_header X-Forwarded-For;#获取最后一个非信任服务器ip作为客户真实ipreal_ip_recursive on;# 配置解析的IP地址,作为获取地理信息的IP地址:map $http_x_forwarded_for $realip {~^(\d+\.\d+\.\d+\.\d+) $1;default $remote_addr;}# 配置国家和城市检索需要的数据文件:#测试 mmdblookup --file /etc/nginx/geoip/GeoLite2-City/GeoLite2-City.mmdb --ip 116.7.96.148geoip2 /etc/nginx/geoip/GeoLite2-Country/GeoLite2-Country.mmdb {#国家编码$geoip2_country_code source=$realip country iso_code;#国家英文名$geoip2_country_name_en source=$realip country names en;#国家中文名$geoip2_country_name_cn source=$realip country names zh-CN;}geoip2 /etc/nginx/geoip/GeoLite2-City/GeoLite2-City.mmdb {#城市英文名,大多是拼音,有重复情况$geoip2_city_name_en source=$realip city names en;#城市中文名,部分城市没有中文名$geoip2_city_name_cn source=$realip city names zh-CN;#经度,longitude$geoip2_longitude source=$realip location longitude ;#维度,latitude$geoip2_latitude source=$realip location latitude ;}server {listen       80;server_name  localhost; #使用格式化后的日志输出access_log logs/Access.log json_logs;location / {#proxy_pass http://8.8.8.8/;root   html;index  index.html index.htm;# 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面proxy_intercept_errors on;}error_page 404 403 /404.html;location /404.html{root   html;}error_page 500 502 503 504 /50x.html;location /50x.html{root   html;}}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

保存文件后进入到sbin路径开启nginx服务

cd /usr/local/nginx/sbin/

./nginx

出现进程占用的情况下

ps -ef |grep nginx

kill -9 PID

再重新启动nginx

九、测试访问nginx网页

cd /usr/local/nginx/logs

tail -f 999 access.log

成功打印!!!

后记

主要难点分为两块:

1、执行./configure命令的时候会出现各种没有依赖的问题,需百度后yum安装

2、配置nginx.conf文件,这个既是难点也是重点。本文配置各行注释都有加,没听说过的可以百度科普一下知识。

到此nginx集成geoip,city,country,realip,https等集成完成!!

感谢自己。

实践:nginx代理,通过使用GeoIp模块获取访问者IP及访问地区信息相关推荐

  1. php负载均衡如何获得真实ip,nginx负载均衡后端RS中获取真实ip

    nginx负载均衡后端RS中获取真实ip 前端proxy配置 #################### worker_processes  1; events { worker_connections ...

  2. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  3. php 获取 省份缩写,php获取客户端IP,国家,省份,地区,ISP等信息

    php获取客户端IP,国家,省份,地区,ISP等信息,首先获取获取客户端的公网ip,开始本地搭建的环境,本地访问获取到的是127.0.0.1,然后用138的接口可以实现curl模拟客户端访问 http ...

  4. c# MVC API 获取访问者IP 并加以验证

    使用webapi时有时会为了数据安全性加以访问者ip验证,就需要在API项目中进行处理 获取访问者IP地址 代码直接上 不罗嗦 /// <summary>/// 获取访问者IP地址/// ...

  5. php获取访问者ip地址,PHP获取访问者IP地址的二种方法

    本节内容: PHP获取访问者IP地址 方法一:$_SERVER['HTTP_X_FORWARDED_FOR']来获取相应的地址 复制代码 代码示例: function get_onlineip() { ...

  6. 获取客户端IP地址定位城市信息

    获取客户端IP地址定位城市信息 1.首先获取客户端的IP地址 function getIPaddress(){ $IPaddress=''; if (isset($_SERVER)){ if (iss ...

  7. html获取访客ip,jQuery获取访问者IP地址的方法(基于新浪API与QQ查询接口)

    本文实例讲述了jQuery获取访问者IP地址的方法.分享给大家供大家参考,具体如下: $(document).ready(function(){ //通过调用新浪IP地址库接口查询用户当前所在国家.省 ...

  8. java 获取 客户端端口_Java获取客户端IP、端口等信息

    Java获取客户端IP.端口等信息.txt花前月下,不如花钱"日"下.叶子的离开,是因为风的追求还是树的不挽留?干掉熊猫,我就是国宝!别和我谈理想,戒了!Java获取客户端IP.端 ...

  9. 使用Nginx自带的Realip模块获取用户真实IP

    (一)简要说明 如果你的Web服务器前端有代理服务器或CDN时日志中的$remote_addr可能就不是客户端的真实IP了.比较常用的解决方法有以下三几种,本文将主要介绍如何使用Nginx自带real ...

最新文章

  1. SQL Server Profiler工具
  2. 5.4Python数据处理篇之Sympy系列(四)---微积分
  3. 有三AI一周年了,说说我们的初衷,生态和愿景
  4. php和mysql建立链接
  5. 令人惋惜的天才新秀:16岁上剑桥大学,27岁就出名,数学事业一路畅通无阻,但自从结婚后,人生从此翻天覆地······
  6. 关于换行这个动作,win 和 mac 的实现
  7. 在手语世界里,健听人、数字人与听障人的交织
  8. Linux中的Java类,Java基础入门学习-Java中类的属性
  9. 用Android Studio做一个超好玩的拼图游戏,附送超详细注释的源码
  10. linux下cmake使用教程,超详细的cmake教程
  11. 百度NLP词 语相似度接口Demo
  12. 计算机课程设计答辩评语,课程设计评语模板.doc
  13. 组成计算机网络必备的条件是什么,要组成计算机网络必须具备的三要素
  14. 推荐3个开源的快速开发平台,前后端都有,项目经验又有着落了!
  15. Rust use of undeclared crate or module和maybe a missing crate?
  16. android 联系人 拼音,Android 2.1 (Eclair)的中文联系人拼音检索问题
  17. python当前运行目录_Python获取运行目录与当前脚本目录的方法
  18. mysql复杂查询的书_mysql 复杂查询
  19. Caused by: java.sql.BatchUpdateException: ORA-00001: 违反唯一约束条件 (DSPACE.SYS_C007868)
  20. win10锁定计算机后黑屏,Win10系统如何设置锁屏后不睡眠不黑屏

热门文章

  1. 我的XBox手柄怎么有线断连了
  2. 全球与中国1-苄基吡啶嗡-3-羧酸盐市场深度研究分析报告
  3. 神经网络的激活函数总结
  4. 电脑分区不小心格式化了文件恢复教程
  5. 如何利用自己的域名生成短链接
  6. 短信java_Java发送手机短信(附代码和解析,亲测有效,简便易操作)
  7. ENVI-landsat7/8全色与多光谱波段图像融合前,全色波段辐射定标报错指南
  8. linux ls 目录颜色,改变ls 中目录颜色的方法
  9. 积分域为椭球的三重积分的求解方式----广义的极坐标变换
  10. Ajax回调函数无反应及进erro问题记录。