Chinaunix首页 | 论坛 | 博客
  • 博客访问: 78807
  • 博文数量: 12
  • 博客积分: 1560
  • 博客等级: 上尉
  • 技术积分: 146
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-07 07:23
文章分类
文章存档

2011年(2)

2010年(8)

2008年(2)

分类: LINUX

2010-04-27 02:14:55

参考了无数前辈的安装心得,本人也实现了Nginx+GeoIP+pcre+zlib的安装,其中部分配置直接COPY
 
:)
以下为所需要的软件及下载地址:
nginx0.8.36
wget
pcre 8.02
wget
GeoIP C-library1.4.6和当前最新GeoIP db
wget
wget
zlib1.2.4
wget
 
本次安装,nginx要实现的功能有:压缩,缓存,将不同国家IP的访问转移到不同的服务器。
模块依赖性:gzip模块需要zlib库,rewrite模块需要pcre库,实现对IP访问的转移需要用到GeoIP库
 
1.将下载回来的的文件全部解压到/home/xxx目录
2.进入解压后的GeoIP C-library1.4.6目录,配置,编译,安装,./configure,make&make install
如不先安装GeoIP的C-library,安装Nginx时选择的GeoIP模块有可能不能顺便安装。
GeoIP C-library1.4.6在安装完成之后,
需要在/etc/ld.so.conf加入一行:/usr/local/lib
再执行/sbin/ldconfig /etc/ld.so.conf,使修改生效。不然启动nginx的时候会提示
"libGeoIP.so.1: cannot open shared object file: No such file or directory"
3.进入解压后的nginx目录,执行
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module  --with-http_ssl_module --with-zlib=/home/xxx/zlib-1.2.4 --with-http_geoip_module --with-pcre=/home/xxx/pcre-8.02
 
另一种安装方法:通过yum安装Nginx的一些模块所需要支持的,然后再安装Nginx也是可以的,如:
yum -y install pcre pcre-devel zlib zlib-devel openssl-devel openssl GeoIP GeoIP-devel GeoIP-data
当然,通过yum安装之后,几乎不需要再指定特定库的路径,直接用像下面类似的安装配置就行:
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module
--with-http_geoip_module
 
GeoIP的数据库文件GeoIP.dat会存放在/var/lib下面。建议自己下载GeoIP.dat,不然在CentOS的yum资源里面得到的GeoIP.dat,不是最新的。
 
 
下面是重点,嘿嘿
4.vi /usr/local/nginx/conf/nginx.conf
 

user  nobody nobody;
worker_processes  2;
error_log  logs/error.log;
pid        logs/nginx.pid;
#设置工作模式及连接数
events {
use epoll;
worker_connections      1024;
}

http {
include mime.types;
default_type  application/octet-stream;
#指定GeoIP.dat的位置
geoip_country /home/xxx/GeoIP.dat;
log_format main        '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲(可根据自身网站需要设置buffer大小)
client_header_buffer_size    1k;
large_client_header_buffers  4 4k;
#启用gzip模块(可根据自身网站需要设置buffer大小)

gzip on;
gzip_min_length  1100;
gzip_buffers    4 8k;
gzip_types      text/plain;
output_buffers  1 32k;
postpone_output  1460;
#设定access log
access_log  logs/access.log  main;
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout          3m;
sendfile                on;
tcp_nopush              on;
tcp_nodelay            on;
keepalive_timeout  65;
#设定负载均衡的服务器列表
upstream HK {
ip_hash;
     server hk.xxx.com;
     server phk.xxx.com;
     server hk1.xxx.com;
     server hk2.xxx.com;
     server hk3.xxx.com;
     server hk4.xxx.com;
}
upstream CHINA {
     ip_hash;
     server cn1.xxx.com;
     server cn2.xxx.com;
     server cn3.xxx.com;
     server cn4.xxx.com;    
     server cn5.xxx.com;
     server cn6.xxx.com;    
     server cn7.xxx.com;
     server cn8.xxx.com;    
     server cn9.xxx.com;
     server cn.xxx.com;
}
upstream SINGAPORE {
     ip_hash;
     server sg.xxx.com;
     server sg2.xxx.com;
     server sg3.xxx.com;
     server my.xxx.com;    
     server my1.xxx.com;
     server my2.xxx.com;

}
#设定虚拟主机和端口
server {
listen          80;
server_name   xxx223
charset utf8;
#设定本虚拟主机的访问日志
access_log  logs/xxx.access.log  main;
#缓存一些图片,flash及不常更新的文件(可根据网站自身需要设置)
location ~ ^/(img|swf|flv|rm|png|jpg|gif)/  {
root    /nginxcache;
expires 12h;  #设置缓存的过期时间为12小时
}
#对 "/" 启用负载均衡实现从不同国家的IP访问转移到不同的服务器。当然得保证从各个国家访问该服务器的速度,相对于其它服务器是最快的
location / {
                if ($geoip_country_name ~ "(Thailand|HK|Cambodia|Indonesia)") {
                proxy_pass ;
                }
                if ($geoip_country_name ~ "China") {
                proxy_pass ;
                }
                if ($geoip_country_name ~ "Malaysia|Singapore") {
                proxy_pass ;
                }
                if ($geoip_country_name ~ "Japan") {
                rewrite ^/(.*)$ permanent;
                }         #http跳转.
                if ($remote_addr ~ "(x.x.x.*|a.b.c.d)" {
                rewrite ^/(.*)$ permanent;
                } #针对特定的IP做跳转。
 
注意:如果国家名字拼写出错,Nginx会将客户的访问转移到nginx目录下面的index.html               
#(以下参数可根据网站需要设置,如想查看详细功能请考Nginx手册)
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;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout  90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size      4k;
proxy_buffers          4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
location /xxxstatus {
stub_status on;
access_log  on;
}
}
}
 
 
在此基础上应该还可以实现优化
1.如从中国访问的客户,还可以实现针对在哪个省哪个城市的源IP进行分流,将源IP分流到访问速度最快的服务器。同理,其它国家的源IP也可以实现分流负载
2.访问的缓冲值方面,可以根据服务器的硬件配置,访问流量的大小进行调整
3.连接数方面的设置,可将连接数设置为服务器可以随的最大值。不过,最好先看看该服务器上iptables对连接数限制在多少
阅读(5113) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-10-04 15:57:49

多谢~~

chinaunix网友2010-10-04 15:57:49

多谢~~