图文解说:Nginx + tomcat配置集群负载均衡

        开发的应用采用F5负载均衡交换机,F5将请求转发给5台hp unix服务器,每台服务器有多个webserver实例,对外提供web服务和socket等接口服务。之初,曾有个小小的疑问为何不采用开源的apache、Nginx软件负载,F5设备动辄几十万,价格昂贵?自己一个比较幼稚的问题,后续明白:F5是操作于IOS网络模型的传输层,Nginx、apache是基于http反向代理方式,位于ISO模型的第七层应用层。直白些就是TCP UDP 和http协议的区别,Nginx不能为基于TCP协议的应用提供负载均衡。

   了解了二者之间的区别于应用场景,对Nginx产生浓厚的兴趣,阅读张宴的<实战Nginx>(这个85年的小伙子年轻有为羡慕+妒忌),搞明白了大致原理和配置,Ubuntu10.10,window下对Nginx+tomcat负载均衡做了配置尝试,将全部请求转发到tomcat,并未做静态,动态分开,图片防盗链等配置。

Nginx 介绍

       Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。  其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。

      上面的全是Nginx介绍基本上是废话,下面转入正题,图文结合展示基本配置,首先是window环境、其次是Ubuntu环境(Vbox虚拟)。本文主要基于Nginx下配置两台tomcat,结构如下图:

Window xp环境:Nginx + Tomcat6

1、下载地址

http://nginx.org/en/downlaod.xml,这里我们推荐下载稳定版(stable versions),本文采用nginx-1.4.6。

2、目录结构

     window下安装Nginx极其简单,解压到一个无空格的英文目录即可(个人习惯,担心中文问题),双击nginx,这里我安装到:D:\server目录,下面涉及到的tomcat也安装在此目录。

   

Dos环境启动:默认启用80端口

访问欢迎html页

在浏览器中访问http://locahost,可以看到默认的欢迎页面。

若想停止nginx,dos环境运行命令:nginx -s stop

3、nginx.conf配置

Nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在D:\server\nginx-1.4.6\、默认住配置文件为D:\server\nginx-1.4.6\conf\nginx.conf。下面是nginx作为前端反向代理服务器的配置。

nginx.conf代码需要做如下修改:

1)、在#gzip  on;后面加入下面的配置:

upstream backend{
            server localhost:8080;
            server localhost:8088;
            ip_hash;
    }

其中server localhost:8080为第一个Tomcat的启动地址,server localhost:8088为第二个Tomcat的启动地址,ip_hash用于做session同步。

2)、修改第一个server{}配置中的listen 80改为新的端口号,因为我的本机80端口被IIS占用,因此将此处改为listen 800。并将

          location/{

                         root html;

                         index index.html index.htm;

改为:

          server {
        listen       800;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass   http://backend;
            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;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    其中proxy_pass参数和upstream backend{}对应。

    下面先启动两台tomcat,然后双击nginx根目录下的nginx.exe文件或者用start nginx,打开浏览器,输入地址:http://localhost:800,便可看到下面的界面:

    








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章