怎么在nginx中利用geoip實(shí)現(xiàn)區(qū)域限制

這篇文章將為大家詳細(xì)講解有關(guān)怎么在nginx中利用geoip實(shí)現(xiàn)區(qū)域限制,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團(tuán)隊(duì)的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。成都創(chuàng)新互聯(lián)堅(jiān)持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因?yàn)椤皩W⑺詫I(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、電商網(wǎng)站開發(fā),微信小程序開發(fā),軟件按需設(shè)計(jì)網(wǎng)站等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。

環(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

關(guān)于怎么在nginx中利用geoip實(shí)現(xiàn)區(qū)域限制就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞名稱:怎么在nginx中利用geoip實(shí)現(xiàn)區(qū)域限制
當(dāng)前路徑:http://bm7419.com/article2/jdejoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)電子商務(wù)、網(wǎng)站建設(shè)定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)