Docker NGINX加載Geoip模板方式
更新時(shí)間:2026年04月08日 09:59:40 作者:還是那片西瓜嗎
本文介紹了在Docker環(huán)境下配置NGINX使用GeoIPIP模塊的方法,包括下載GeoIPIP庫(kù)、配置Nginx和子配置文件、啟動(dòng)Docker容器等步驟,并進(jìn)行了訪(fǎng)問(wèn)和日志查看測(cè)試
前提環(huán)境:
- Docker 環(huán)境
涉及參考文檔:
一、下載GeoIP IP庫(kù)

二、配置Nginx主配置文件
vim /data/nginx/MangoMoh/dos/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
load_module modules/ngx_http_geoip_module.so; #加載geoip模塊
load_module modules/ngx_stream_geoip_module.so; #加載geoip模塊
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
geoip_country /var/log/nginx/geoip/GeoCountry.dat; #數(shù)據(jù)放在日志文件一起掛載進(jìn)行。實(shí)際個(gè)人自定義即可
geoip_city /var/log/nginx/geoip/GeoCity.dat;
geoip_proxy 0.0.0.0/0;
geoip_proxy_recursive on;
log_format main escape=json '{'
'"msec": "$msec", ' # 以秒為單位的時(shí)間,日志寫(xiě)入時(shí)分辨率為毫秒
'"connection": "$connection", ' # 連接序列號(hào)
'"connection_requests": "$connection_requests", ' # ???過(guò)連接發(fā)出的當(dāng)前請(qǐng)求數(shù)
'"pid": "$pid", ' # process pid
'"request_id": "$request_id", ' # the unique request id
'"request_length": "$request_length", ' # 請(qǐng)求長(zhǎng)度(包括請(qǐng)求行、標(biāo)頭和請(qǐng)求正文)
'"remote_addr": "$remote_addr", ' # 客戶(hù)地址
'"remote_user": "$remote_user", ' # 身份驗(yàn)證提供的用戶(hù)名
'"remote_port": "$remote_port", ' # 客戶(hù)端端口
'"time_local": "$time_local", ' # 通用日志格式的本地時(shí)間
'"time_iso8601": "$time_iso8601", ' # ISO 8601 標(biāo)準(zhǔn)格式的本地時(shí)間
'"request": "$request", ' # 完整的原始請(qǐng)求行
'"request_uri": "$request_uri", ' # 完整的原始請(qǐng)求 URI(帶參數(shù))
'"args": "$args", ' # 請(qǐng)求行中的參數(shù)
'"status": "$status", ' # 響應(yīng)狀態(tài)
'"body_bytes_sent": "$body_bytes_sent", ' # 發(fā)送到客戶(hù)端的字節(jié)數(shù),不計(jì)算響應(yīng)標(biāo)頭;
'"bytes_sent": "$bytes_sent", ' # 發(fā)送到客戶(hù)端的字節(jié)數(shù)
'"http_referer": "$http_referer", ' # 防盜鏈
'"http_user_agent": "$http_user_agent", ' # user agent
'"http_x_forwarded_for": "$http_x_forwarded_for", ' # 訪(fǎng)問(wèn)真實(shí)IP
'"http_host": "$http_host", ' # 官方未找到,具體使用需要驗(yàn)證
'"server_name": "$server_name", ' # 接受請(qǐng)求的服務(wù)器的名稱(chēng)
'"request_time": "$request_time", ' # 請(qǐng)求處理時(shí)間
'"upstream": "$upstream_addr", ' # 上游服務(wù)器,保留 IP 地址和端口
'"upstream_connect_time": "$upstream_connect_time", ' # 保留與上游服務(wù)器建立連接所花費(fèi)的時(shí)間
'"upstream_header_time": "$upstream_header_time", ' # 保持時(shí)間 用于從上游服務(wù)器接收響應(yīng)標(biāo)頭
'"upstream_response_time": "$upstream_response_time", ' # 保留從上游服務(wù)器接收響應(yīng)所花費(fèi)的時(shí)間
'"upstream_response_length": "$upstream_response_length", ' # 保留從上游服務(wù)器獲得的響應(yīng)長(zhǎng)度
'"upstream_cache_status": "$upstream_cache_status", ' # 保持訪(fǎng)問(wèn)響應(yīng)緩存的狀態(tài)
'"ssl_protocol": "$ssl_protocol", ' # TLS protocol
'"ssl_cipher": "$ssl_cipher", ' # TLS cipher
'"scheme": "$scheme", ' # 請(qǐng)求方式
'"request_method": "$request_method", ' # 請(qǐng)求方式
'"server_protocol": "$server_protocol", ' # 請(qǐng)求協(xié)議
'"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
'"gzip_ratio": "$gzip_ratio", '
'"http_cf_ray": "$http_cf_ray",'
'"geoip_country_code": "$geoip_country_code", '
'"geoip_city": "$geoip_city"'
'}';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
三、配置子NGINX 配置文件
vim /data/nginx/web.conf
server {
listen 80;
listen [::]:80;
server_name $host;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 6;
gzip_min_length 200;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
server_tokens off;
expires 1d;
access_log /var/log/nginx/website.access.log main;
error_log /var/log/nginx/website.error.log;
# 請(qǐng)求到入口頁(yè)面
location /myip {
default_type text/plain;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Real-IP;
real_ip_recursive on;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
四、啟動(dòng)Docker 容器
docker run -d --name=nginx \ -v /data/nginx/web.conf:/etc/nginx/conf.d/default.conf \ -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \ -v /data/docker_logs/web:/var/log/nginx \ -p 80:80 \ nginx:latest
五、訪(fǎng)問(wèn)測(cè)試
1、查看Web
https://IP/myip

2、查看日志

3、查看Grafana

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
使用docker構(gòu)建一個(gè)redis鏡像的方法詳解
Redis是一個(gè)開(kāi)源的使用ANSI C語(yǔ)言編寫(xiě)、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value型NoSQL數(shù)據(jù)庫(kù),本文介紹了docker創(chuàng)建redis鏡像的方法,分享給大家,需要的朋友可以參考下2024-06-06
如何修改docker官方鏡像內(nèi)部?jī)?nèi)容并重新build鏡像
這篇文章主要介紹了如何修改docker官方鏡像內(nèi)部?jī)?nèi)容并重新build鏡像問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
docker安裝redis(鏡像安裝)的實(shí)現(xiàn)示例
這篇文章主要介紹了docker安裝redis(鏡像安裝)的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2019-02-02
解決Docker容器異常退出Exit(253)問(wèn)題
這篇文章主要介紹了解決Docker容器異常退出Exit(253)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
通過(guò)Docker運(yùn)行AnythingLLM的方法
這篇文章主要介紹了通過(guò)Docker運(yùn)行AnythingLLM的方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
使用Docker run的選項(xiàng)以覆蓋Dockerfile中的設(shè)置詳解
今天小編就為大家分享一篇關(guān)于使用Docker run的選項(xiàng)以覆蓋Dockerfile中的設(shè)置詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
Docker Registry搭建私有鏡像倉(cāng)庫(kù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Docker Registry搭建私有鏡像倉(cāng)庫(kù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

