欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Nginx中的geo模塊與利用其配置負(fù)載均衡的示例

 更新時間:2016年01月27日 14:18:28   作者:Deidara  
這篇文章主要介紹了詳解Nginx中的geo模塊與利用其配置負(fù)載均衡的示例,文中對模塊的geo指令使用有比較詳細(xì)的介紹,需要的朋友可以參考下

geo指令使用ngx_http_geo_module模塊提供的。默認(rèn)情況下,nginx有加載這個模塊,除非人為的 --without-http_geo_module。
ngx_http_geo_module模塊可以用來創(chuàng)建變量,其值依賴于客戶端IP地址。
geo指令
語法: geo [$address] $variable { ... }
默認(rèn)值: —
配置段: http
定義從指定的變量獲取客戶端的IP地址。默認(rèn)情況下,nginx從$remote_addr變量取得客戶端IP地址,但也可以從其他變量獲得。如

geo $remote_addr $geo {
    default 0;
    127.0.0.1 1;
}
geo $arg_ttlsa_com $geo {
    default 0;
    127.0.0.1 1;
}

如果該變量的值不能代表一個合法的IP地址,那么nginx將使用地址“255.255.255.255”。
nginx通過CIDR或者地址段來描述地址,支持下面幾個參數(shù):

  • delete:刪除指定的網(wǎng)絡(luò)
  • default:如果客戶端地址不能匹配任意一個定義的地址,nginx將使用此值。 如果使用CIDR,可以用“0.0.0.0/0”代替default。
  • include: 包含一個定義地址和值的文件,可以包含多個。
  • proxy:定義可信地址。 如果請求來自可信地址,nginx將使用其“X-Forwarded-For”頭來獲得地址。 相對于普通地址,可信地址是順序檢測的。
  • proxy_recursive:開啟遞歸查找地址。 如果關(guān)閉遞歸查找,在客戶端地址與某個可信地址匹配時,nginx將使用“X-Forwarded-For”中的最后一個地址來代替原始客戶端地址。如果開啟遞歸查找,在客戶端地址與某個可信地址匹配時,nginx將使用“X-Forwarded-For”中最后一個與所有可信地址都不匹配的地址來代替原始客戶端地址。
  • ranges:使用以地址段的形式定義地址,這個參數(shù)必須放在首位。為了加速裝載地址庫,地址應(yīng)按升序定義。
geo $country {
  default    ZZ;
  include    conf/geo.conf;
  delete     127.0.0.0/16;
  proxy     192.168.100.0/24;
  proxy     2001:0db8::/32;
 
  127.0.0.0/24  US;
  127.0.0.1/32  RU;
  10.1.0.0/16  RU;
  192.168.1.0/24 UK;
}
vim conf/geo.conf
10.2.0.0/16  RU;
192.168.2.0/24 RU;

地址段例子:

geo $country {
  ranges;
  default          ZZ;
  127.0.0.0-127.0.0.0    US;
  127.0.0.1-127.0.0.1    RU;
  127.0.0.1-127.0.0.255   US;
  10.1.0.0-10.1.255.255   RU;
  192.168.1.0-192.168.1.255 UK;
}

geo指令主要是根據(jù)IP來對變量進(jìn)行賦值的。因此geo塊下只能定義IP或網(wǎng)絡(luò)段,否則會報錯。

geo模塊實(shí)現(xiàn)全局負(fù)載均衡
server1  : 192.168.6.101
server2  :  192.168.6.102
server3  :  192.168.6.121
 
測試機(jī)1 IP:192.168.6.2
測試機(jī)2 IP:192.168.6.8
測試機(jī)3 IP:192.168.6.189
 
1.在每臺server上都編譯安裝 nginx ,我就不多說了!
server1,與server2 的配置我沒有改動~只把他的主頁改一下,這樣有利于測試!
server1 :

shell $> cd /usr/local/nginx/html
shell $> rm index.html
shell $> echo "192.168.6.101" > index.html

 
server2:

shell $> cd /usr/local/nginx/html
shell $> rm index.html
shell $> echo "192.168.6.102" > index.html

 
把他們的服務(wù)都起來

shell $> /usr/local/nginx/sbin/nginx
 

2.修改 server3 的配置`
 

shell $> cd /usr/local/nginx/conf/
shell $> vim nginx.conf
worker_processes 1;
 
events {
  worker_connections 1024;
}
http {
  include    mime.types;
  default_type application/octet-stream;
  geo $geo {
    default default;
    192.168.6.189/32    uk;
    192.168.6.8/32     us;
#這里的子網(wǎng)碼是 32 是因?yàn)?,我是單網(wǎng)段測試,如果你有VLAN,你可以是24 例如
# 192.168.0.0/24   tw
  }
  upstream  uk.server {
    server 192.168.6.101;
  }
  upstream  us.server {
    server 192.168.6.102;
  }
  upstream  default.server {
    server 192.168.6.121:8080;
  }
  sendfile    on;
  keepalive_timeout 65;

  server {
    listen    80;
    server_name 192.168.6.121;
    index index.html index.htm;
    root html;

    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://$geo.server$request_uri;
    }
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
 
  }
 
  server {
    listen    8080;
    server_name 192.168.6.121;
    location / {
      root  html;
      index index.html index.htm;
    }
  }
}

 
3.測試,在測試機(jī)1 上打開瀏覽器輸入
http://192.168.6.121
顯示

2016127141159022.jpg (685×258)

因?yàn)?測試機(jī)1 IP地址為 192.168.6.2 按照  nginx 配置,他訪問的很明顯是 server3  8080 端口!因?yàn)?server1 server2 的 index.html 我修改了
 
在 測試機(jī)2  上打開瀏覽器~輸入
http://192.168.6.121
顯示

2016127141235231.jpg (293×203)

在 測試機(jī)3上打開瀏覽器~輸入
http://192.168.6.121
測試機(jī)3 IP為 192.168.6.189
顯示:

2016127141258339.jpg (413×218)

很明顯,負(fù)載均衡起到了作用~~~
這樣就可以把 三臺服務(wù)器分別放到不同的IDC 機(jī)房。然后在數(shù)據(jù)同步就可以了~這樣做的好處就是省去了在DNS 上做手腳,因?yàn)橹悄蹹NS 有時候按照來訪IP解析的時候會解析對方的DNS地址,把他匹配到一臺服務(wù)器,如果對方是網(wǎng)通用戶,他用的電信DNS,會直接把他匹配到電信的服務(wù)器,NGINX,確確實(shí)實(shí)的根據(jù)來訪問IP來匹配服務(wù)器的,這樣只要我們把各地區(qū)的IP段收集起來就可以了~~

相關(guān)文章

最新評論