2025年3月28日 星期五 甲辰(龙)年 月廿七 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

nginx geoip_module 地域信息读取

时间:07-03来源:作者:点击数:29

nginx geoip_module 地域信息读取

1.安装geoip

  • yum -y install nginx-module-geoip

2.安装完成后在cd /etc/nginx/modules/ 能看到安装的模块

  • # cd /etc/nginx/modules/
  • # ll
  • total 88
  • -rwxr-xr-x 1 root root 21432 Apr 23 22:34 ngx_http_geoip_module-debug.so
  • -rwxr-xr-x 1 root root 21424 Apr 23 22:34 ngx_http_geoip_module.so
  • -rwxr-xr-x 1 root root 16960 Apr 23 22:34 ngx_stream_geoip_module-debug.so
  • -rwxr-xr-x 1 root root 16952 Apr 23 22:34 ngx_stream_geoip_module.so

3.编辑nginx.conf

  • load_module "modules/ngx_http_geoip_module.so"; # 编译geoip
  • load_module "modules/ngx_stream_geoip_module.so"; # 编译geoip
  • user nginx;
  • worker_processes 1;
  • error_log /var/log/nginx/error.log warn;
  • pid /var/run/nginx.pid;
  • events {
  • worker_connections 1024;
  • }
  • http {
  • include /etc/nginx/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"';
  • access_log /var/log/nginx/access.log main;
  • sendfile on;
  • #tcp_nopush on;
  • keepalive_timeout 65;
  • #gzip on;
  • include /etc/nginx/conf.d/*.conf;
  • }

4.下载地域信息文件

  • https://dev.maxmind.com/geoip/geoip2/geolite2/ # geoip免费下载网站
  • cd /etc/nginx/geoip
  • wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz #国家的地域IP
  • wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz #城市的地域IP

5.default.conf

  • geoip_country /etc/nginx/geoip/GeoLite2-Country.tar;
  • geoip_city /etc/nginx/geoip/GeoLite2-City.tar;
  • server {
  • listen 80;
  • server_name localhost;
  • #charset koi8-r;
  • #access_log /var/log/nginx/log/host.access.log main;
  • location / {
  • if ($geoip_country_code != CN) {
  • return 403;
  • }
  • root /usr/share/nginx/html;
  • index index.html index.htm;
  • }
  • location /myip {
  • default_type text/plain;
  • return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
  • }
  • }
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门