您的位置:首页 > 运维架构 > Nginx

nginx的缓存和第三方模块(Geoip)加载

2012-05-19 16:10 465 查看
运用nginx的第三方模块ngx_cache_purge和http_geoip_module(Geoip模块)

Geoip模块的作用主要是屏蔽某个地区的IP访问,系统自带的nginx一般不带这个模块,所以要下载nginx源代码后自行编译:

wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz tar zxvf GeoIP.tar.gz
cd GeoIP-1.4.8/
./configure
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf
ldconfig
cd ../

然后下载proxy_cache所需的模块ngx_cache_purge:

wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz tar zxf ngx_cache_purge-1.5.tar.gz


wget http://soft.vpser.net/web/pcre/pcre-8.12.tar.gz tar zxf pcre-8.12.tar.gz
cd pcre-8.12
./configure
make && make install
cd ../

wget  http://nginx.org/download/nginx-1.0.10.tar.gz tar zxf nginx-1.0.10.tar.gz
cd nginx-1.0.10
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.5 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_geoip_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
make && make install
cd ../

当安装好之后还需要下载IP 数据库

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gunzip GeoIP.dat.gz
还有城市IP数据库详见: http://www.howtoforge.com/using-geoip-with-nginx-on-debian-squeeze-ubuntu-11.04

在nginx的http环境中中添加配置

http {
...
geoip_country /path/to/GeoIP.dat;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
...
}

这里并没有配置关于proxy_cache的事例,接着配置一个关于IP访问,通过Geoip模块获取得到如果是中国的IP就转向google

server {
·····
if ($geoip_country_code ~ ^CN) {
return http://www.google.com;
}
······

}

启动nginx,看下模块是否加载成功:

/usr/local/nginx/sbin/nginx -V
nginx: nginx version: nginx/1.0.10
nginx: built by gcc 4.1.2 20080704 (Red Hat 4.1.2-52)
nginx: TLS SNI support disabled
nginx: configure arguments: --user=www --group=www
--add-module=../ngx_cache_purge-1.5
--prefix=/usr/local/nginx
--with-http_stub_status_module
--with-http_geoip_module
--with-http_ssl_module
--with-http_gzip_static_module
--with-ipv6

这样都是正常的,再访问打开的是跳到google就对了。
本文出自 “milanlinux” 博客,请务必保留此出处http://cclinux.blog.51cto.com/1215045/869190
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: