nginx装配及负载均衡配置

nginx安装及负载均衡配置

   blog迁移至 :http://www.micmiu.com

 

      Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

  •       Nginx 官网:     http://nginx.org/  
  •       Nginx 中文wiki:http://wiki.nginx.org/NginxChs  
  •       Nginx 中文网站:http://www.howtocn.org/nginx

 

[一]、安装步骤

 

1. 安装PCRE library

    安装nginx前需要确保系统中已经安装PCRE包,PCRE library是HTTP Rewrite模块(即是url静态化的包)。

    PCRE library 详细可查阅其官网: http://www.pcre.org 。

    目前最新版本是8.12,本文附件提供下载:pcre-8.12.tar.gz

 

 

#tar -zxvf pcre-8.12.tar.gz
#cd pcre-8.12.tar.gz
#./configure
#make
#make install

 常规标准的安装步骤。

 

2. 安装nginx

   截止目前最新版本为1.0.5 ,可去官网下载,附件提供下载:nginx-1.0.5.tar.gz   

 

#tar -zxvf nginx-1.0.5.tar.gz
#cd nginx-1.0.5
#./configure
#make
#make install

   Tips: 有关nginx的编译选项可参见:nginx编译选项详解 (http://sjsky.iteye.com/blog/1146520)

 

 

[二]、负载均衡配置示例

 

   1. 测试环境的参数:

  •     192.168.8.183 centos5        nginx1.0.5
  •     192.168.8.184 ubuntu10.10 tomcat-6.0.29
  •     192.168.8.185 ubuntu10.10 tomcat-6.0.29

   ps: tomcat的配置启动这里就不再详述了.

 

   2. 修改nginx的配置文件:/usr/local/nginx/conf/nginx.conf

 

    upstream my-web-server {
        server 192.168.8.184:8088;
        server 192.168.8.185:8088;
    }

    server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                 proxy_pass         http://my-web-server;
                 proxy_set_header   Host             $host;
                 proxy_set_header   X-Real-IP        $remote_addr;
                 proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
         }

        #error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
 

  Tips:

        1. 注意配置文件中的参数对应关系:upstream: my-back-server 和 proxy_pass:  http://my-back-server ;

        2. upstream 中的server可设置多个类似:server 192.168.8.184:8088;

            如果是80端口可以不用加端口号,直接写成 server 192.168.8.184;即可

 

[三]、测试

 

       1.修改tomcat下webapp/ROOT/index.html,增加特定标识以便测试。

 

       2.浏览器打开: http://192.168.8.183:8088 刷新页面,一起正常会交替显示如下信息:


nginx装配及负载均衡配置

 
nginx装配及负载均衡配置

 3. 如果停掉其中某一台tomcat,再次访问刷新时,只显示一个tomcat的信息.

 

到此nginx安装及负载均衡配置演示结束。

 

 

本文连接:http://sjsky.iteye.com/blog/1156195

 

 

转载请注明来自:Michael's blog @ http://sjsky.iteye.com

----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------

 

 

 

1 楼 hyneng 2011-08-25  
你在什么系统上安装的?我在ubuntu 10.4 server上安装, 一直不成功
2 楼 sjsky 2011-08-26  
hyneng 写道
你在什么系统上安装的?我在ubuntu 10.4 server上安装, 一直不成功

我是在centos5上安装的nginx,以前在ubuntu10.10也安装过都能成功的,你编译报的错误时什么呢?