濮阳杆衣贸易有限公司

主頁 > 知識庫 > 在nginx中使用geoip做區(qū)域限制的方法

在nginx中使用geoip做區(qū)域限制的方法

熱門標(biāo)簽:濮陽好的聯(lián)通400電話申請 山東企業(yè)外呼系統(tǒng)公司 靈圖uu電子寵物店地圖標(biāo)注 虛假地圖標(biāo)注 地圖標(biāo)注黃河的位置 地圖標(biāo)注如何改成微信號 承德地圖標(biāo)注公司 百度地圖標(biāo)注公司位置要多少錢 400電話號碼辦理多少錢

該博客為工作筆記

環(huán)境:

nginx version: nginx/1.14.0

centos version: centos7

需求如下:

通過IP區(qū)別國內(nèi)或國外,從而跳轉(zhuǎn)到不同的頁面,最終用nginx的第三方module:geoip來實(shí)現(xiàn),這就不說它的優(yōu)勢了,網(wǎng)上很多解釋,下面看怎么配置

我的系統(tǒng)中是配置了nignx.repo的,我直接用yum來安裝了geoip模塊,沒有用添加模塊重編的方式

yum install nginx-module-geoip

下載geoip的數(shù)據(jù)庫文件

cd /etc/nginx
mkdir geoipdat
cd geoipdat

下載

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

解壓

gunzip GeoIP.dat.gz
gunzip GeoLiteCity.dat.gz

根據(jù)需求配置nginx

首先在nginx.conf中加載geoip的庫,配置如下:

load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";

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;
}

配置虛擬主機(jī)如下:

geoip_country /etc/nginx/geoipdat/GeoIP.dat;
geoip_city /etc/nginx/geoipdat/GeoLiteCity.dat;


server {
  listen    80;
  server_name localhost;
  location / {
 root /opt;
 if ($geoip_country_code = CN){
 rewrite (.*) /zh$1 break;
 }
    rewrite (.*) /en$1 break;
  }
    error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }

}

opt目錄如下

[root@VM_0_15_centos opt]# tree
.
|
└── en
│    └── index.html
└── zh
  └── index.html

上面只是簡單配置一下。。。。

以上這篇在nginx中使用geoip做區(qū)域限制的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

標(biāo)簽:安康 福州 泰安 上海 淮安 鷹潭 樂山 德宏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在nginx中使用geoip做區(qū)域限制的方法》,本文關(guān)鍵詞  在,nginx,中,使用,geoip,做,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在nginx中使用geoip做區(qū)域限制的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于在nginx中使用geoip做區(qū)域限制的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    当雄县| 元朗区| 贺兰县| 贵阳市| 天水市| 东台市| 新丰县| 晋江市| 象山县| 深州市| 阿勒泰市| 琼结县| 山东| 荔波县| 体育| 醴陵市| 东海县| 阿拉尔市| 二连浩特市| 曲阳县| 昭平县| 普兰县| 神木县| 苏尼特左旗| 彭山县| 峨山| 阿瓦提县| 翼城县| 鄂伦春自治旗| 瑞金市| 抚宁县| 吴堡县| 樟树市| 景德镇市| 敖汉旗| 南宁市| 新乐市| 平阴县| 浦北县| 云南省| 满洲里市|