实践:nginx代理,通过使用GeoIp模块获取访问者IP及访问地区信息
目录
前言
一、在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
下载后进行解压并编译安装
tar -zxvf libmaxminddb-1.7.1.tar.gz
cd libmaxminddb-1.7.1/
./configure && make && make install
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及访问地区信息的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 在查找预编译头时遇到意外的文件结尾
- 下一篇: tpl文件如何导入ps?tpl文件笔刷怎