Nginx+Tomcat配置集群负载均衡(windows)

Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。
上面的全是Nginx介绍基本上是废话,下面转入正题,图文结合展示基本配置。本文主要基于Nginx下配置两台tomcat,结构如下图:

Nginx+Tomcat配置集群负载均衡(windows)_第1张图片
Paste_Image.png

1、下载地址
http://nginx.org/en/download.html ,这里我们推荐下载稳定版(stable versions),本文采用nginx/Windows-1.10.0。
2、目录结构
Nginx-
|_ conf 配置目录
|_ contrib
|_ docs 文档目录
|_ logs 日志目录
|_ temp 临时文件目录
|_ html 静态页面目录
|_ nginx.exe 主程序
window下安装Nginx极其简单,解压缩到一个无空格的英文目录即可(个人习惯,担心中文出问题),双击nginx启动,这里我安装到:D:\nginx目录,下面涉及到的tomcat也安装在此目录。

Nginx+Tomcat配置集群负载均衡(windows)_第2张图片
Paste_Image.png

两个Tomcat 后面跟的是端口号,www文件夹是网站更目录,nginx-1.10.0文件夹为解压后的nginx-1.10.0.zip

Nginx+Tomcat配置集群负载均衡(windows)_第3张图片
Paste_Image.png

上面介绍几个常用的命令。
3、nginx.conf配置
Nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在D:\nginx\nginx-1.10.0\conf\、默认主配置文件为D:\nginx\nginx-1.10.0\conf\nginx.conf。下面是nginx作为前端反向代理服务器的配置。

#user  nobody;
worker_processes  2;

#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;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream suroot{
        server 127.0.0.1:8090 max_fails=2 fail_timeout=30s;
        server 127.0.0.1:9091 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          root   html;
          index  index.html index.htm index.jsp login.jsp; #定义首页索引文件的名称
          proxy_pass http://suroot/;#请求转向suroot定义的服务器列表
          #以下是一些反向代理的配置可删除.
          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   600;#nginx跟后端服务器连接超时时间(代理连接超时)
          proxy_send_timeout   600; #后端服务器数据回传时间(代理发送超时)
          proxy_read_timeout   600; #连接成功后,后端服务器响应时间(代理接收超时)
          proxy_buffer_size   8k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
          proxy_buffers   4 64k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
          proxy_busy_buffers_size   128k;#高负荷下缓冲大小(proxy_buffers*2)
          proxy_temp_file_write_size  128k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }

        #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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

** 4、Tomcat配置**
对于tomcat大家都很熟悉,只需要修改server.xml配置文件即可,这里我们以apache-tomcat-6.0.44为例,分别在server目录,解压缩并命名为:apache-tomcat-6.0.44-8090、apache-tomcat-6.0.44-8091
第一处端口修改:
Xml代码 :

  
  

第二处端口修改:
Xml代码 :

  
  

第三处端口修改:
Java代码 :

  

8091端口的Tomcat端口设置和上面一样,不要冲突就行。
****5、验证配置与测试负载均衡****
首先测试nginx配置是否正确,测试命令:nginx -t (默认验证:conf\nginx.conf),也可以指定配置文件路径。


Nginx+Tomcat配置集群负载均衡(windows)_第4张图片
Paste_Image.png

其次验证tomcat,启动两个tomcat,不出现端口冲突即为成功。

Nginx+Tomcat配置集群负载均衡(windows)_第5张图片
Paste_Image.png

最后验证配置负载均衡设置

Nginx+Tomcat配置集群负载均衡(windows)_第6张图片
Paste_Image.png

至此window下nginx+tomcat负载均衡配置结束,关于tomcat Session的问题通常是采用memcached,或者采用nginx_upstream_jvm_route ,他是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能。如果tomcat过多不建议session同步,server间相互同步session很耗资源,高并发环境容易引起Session风暴。请根据自己应用情况合理采纳session解决方案。

你可能感兴趣的:(Nginx+Tomcat配置集群负载均衡(windows))