使用Nginx+Tomcat7在RHEL6.3下实现负载均衡

来源:互联网 发布:java项目怎么上线 编辑:程序博客网 时间:2024/06/06 02:40
使用Nginx+Tomcat7在RHEL6.3下实现负载均衡


1、安装Nginx服务器(使用root用户操作)


1.1、获取安装Nginx需要的软件源

软件源可以在 http://nginx.org/en/download.html 页面找到
如果你看不懂英文,就从以下地址下载安装rpm包就可以搞定软件源了:)
http://nginx.org/packages/rhel/6/noarch/RPMS/nginx-release-rhel-6-0.el6.ngx.noarch.rpm

1.2、安装Nginx

#yum install nginx

提示:
nginx-1.2.6-1.el6.ngx.x86_64      安装成功

1.3、查询Nginx软件文件安装路径

#rpm -ql nginx-1.2.6-1.el6.ngx.x86_64

查询结果:
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/conf.d/example_ssl.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/sysconfig/nginx
/usr/sbin/nginx
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

1.4、查询服务安装状态

# chkconfig --list | grep nginx

显示结果:
nginx          0:off1:off2:on3:on4:on5:on6:off

说明Nginx服务已经安装成功,并且是开机启动的:)

1.5、查询新建用户状态

# less /etc/passwd | grep nginx

显示结果:
nginx:x:496:493:nginx user:/var/cache/nginx:/sbin/nologin

说明新建了一个nginx用户组和一个nginx用户,用于执行nginx服务

2、配置Nginx服务器做为负载平衡(使用root用户操作)


2.1、修改配置文件 /etc/nginx/nginx.conf 

user  nginx;worker_processes  8;            # 工作的子进程数量,默认为1(通常设置为等于CPU数量或者2倍于CPU)error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;# add by guoruiworker_rlimit_nofile    8192;  # 在nginx级别上提高打开的文件句柄限制,必须大于 worker_connections 的值events {   # add by guorui   use epoll;                  # 使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue   worker_connections  2048;   # 每个进程允许的最多连接数,必须小于 worker_rlimit_nofile 的值                               # 服务器的最大访问客户数 max clients = worker_processes * worker_connections }http {   include       /etc/nginx/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  /var/log/nginx/access.log  main;   sendfile        on;   #tcp_nopush     on;   keepalive_timeout  65;   #gzip  on;   # add by guorui                                # 添加 upstream 模块,参考PS1   upstream mtserver {                            # mtserver 是服务器集群名称,后面会用到       server localhost:8081 fail_timeout=5;      # 添加多个负载服务器的地址、端口及其他负载参数       server localhost:8080;   }   include /etc/nginx/conf.d/*.conf;}



2.2、修改配置文件 /etc/nginx/conf.d/default.conf 

location / {
   proxy_pass http://mtserver;# 添加这一行,引用上面定义的 服务器集群 mtserver
   #root   /usr/share/nginx/html;# 注释掉
   #index  index.html index.htm;# 注释掉
}

3、配置Tomcat7服务器


3.1、服务器A

修改 server.xml 文件,把以下内容

<Engine name="Catalina" defaultHost="localhost">

修改为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-a">

3.2、服务器B

修改 server.xml 文件,把以下内容

<Engine name="Catalina" defaultHost="localhost">

修改为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-b">

3.3、其他修改项(如果两台服务器在一台机器上,需要做下面的修改,主要是为了避免端口冲突)

3.3.1、把以下内容

<Server port="8005" shutdown="SHUTDOWN">


改为

<Server port="8006" shutdown="SHUTDOWN">

3.3.2、把以下内容

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

改为

<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

3.3.3、把以下内容

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

改为

<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

4、手动操作Nginx服务器


启动 nginx
关闭 nginx -s stop

================================完=================================

PS1:upstream 语法参考
语法: upstream name { ... }
默认值:
上下文: http
定义一组服务器。 这些服务器可以监听不同的端口。 而且,监听在TCP和UNIX域套接字的服务器可以混用。

例子:

upstream backend {
   server backend1.example.com weight=5;
   server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
   server unix:/tmp/backend3;
}
默认情况下,nginx按加权轮转的方式将请求分发到各服务器。 在上面的例子中,每7个请求会通过以下方式分发: 

5个请求分到backend1.example.com, 一个请求分到第二个服务器,一个请求分到第三个服务器。 与服务器通信

的时候,如果出现错误,请求会被传给下一个服务器,直到所有可用的服务器都被尝试过。 如果所有服务器都返回

失败,客户端将会得到最后通信的那个服务器的(失败)响应结果。

语法: server address [parameters];
默认值:
上下文: upstream
定义服务器的地址address和其他参数parameters。 地址可以是域名或者IP地址,端口是可选的,或者是指定“unix:”

前缀的UNIX域套接字的路径。如果没有指定端口,就使用80端口。 如果一个域名解析到多个IP,本质上是定义了多

个server。

你可以定义下面的参数:

weight=number
设定服务器的权重,默认是1。
max_fails=number
设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就

认为服务器不可用。在下一个fail_timeout时间段,服务器不会再被尝试。 失败的尝试次数默认是1。设为0就会停止统

计尝试次数,认为服务器是一直可用的。 你可以通过指令proxy_next_upstream、 fastcgi_next_upstream和 

memcached_next_upstream来配置什么是失败的尝试。 默认配置时,http_404状态不被认为是失败的尝试。
fail_timeout=time
设定
统计失败尝试次数的时间段。在这段时间中,服务器失败次数达到指定的尝试次数,服务器就被认为不可用。
服务器被认为不可用的时间段。
默认情况下,该超时时间是10秒。
backup
标记为备用服务器。当主服务器不可用以后,请求会被传给这些服务器。
down
标记服务器永久不可用,可以跟ip_hash指令一起使用。


PS2:nginx命令语法

Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
 -?,-h         : this help
 -v            : show version and exit
 -V            : show version and configure options then exit
 -t            : test configuration and exit
 -q            : suppress non-error messages during configuration testing
 -s signal     : send signal to a master process: stop, quit, reopen, reload
 -p prefix     : set prefix path (default: /etc/nginx//)
 -c filename   : set configuration file (default: /etc/nginx/nginx.conf)

 -g directives : set global directives out of configuration file