日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实践: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及访问地区信息的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。