nginx的geo模块应用

nginx也算是可以根据来源IP来进行负载均衡了。
nginx默认安装就是支持geo模块的。
安装完成后编辑nginx.conf这个主配置文件如下:


 user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; geo $cncip { default 1; #未定义IP的默认为1 include cnc.conf; #包含进cnc.conf文件 } server { listen 80; server_name www3.zauc.com; location / { if ($cncip) { rewrite ^ http://www.baidu.com; #未定义在cnc.conf文件中的IP重定向到www,baidu.com } root /home/zauc/web; index index.wml index.html; } } } 

然后创建cnc.conf文件如下
#192.168.30.0/24 0;
192.168.21.0/24 0;

nginx 的wiki对于这个模块的说明在http://wiki.codemongers.com/NginxHttpGeoModule有粗略的说明。我们可以发现在nginx.conf主控制文件内也可以进行分类。

首先是在一开始定义好


 geo $country { default no; include conf/geo.conf; 127.0.0.0/24 us; 127.0.0.1/32 ru; 10.1.0.0/16 ru; 192.168.1.0/24 uk; } 

然后在location里根据上面定义的名字来进行rewrite。