nginx+tomcat集群

配置Nginx

配置文件目录:/usr/local/nginx-1.6.1/conf/nginx.conf

 cd /usr/local/nginx-1.6.1/conf

 vi nginx.conf

修改后的配置文件如下:

在#gzip on;后面加入upstream

#gzip on;

upstream tomcat { 
# ip_hash; 
server 192.168.1.249:8080 weight=1; 
server 127.0.0.1:8082 weight=1;
server 192.168.1.248:8081 weight=1;
server 192.168.1.248:8083 weight=1;
}

server {
listen 8088;
server_name localhost;

location / {

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
root /usr/share/nginx/html;
index index.html index.htm ;
proxy_pass http://tomcat;

client_max_body_size  5m;  #表示最大上传5M,需要多大就设置多大

proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_send_timeout 10;
}

}

此处nginx配置的listen坚挺端口是8088,那么等配置好后访问nginx就是192.168.1.249:8088,这样就会显示nginx欢迎页

在与tomcat集群配好后在访问192.168.1.249:8088,会显示tomcat的首页,

要是测试集群是否正常,在每个tomcat的webapps目录下建个test目录,将测试用的index.jsp放在此目录下即可,此时访问http://192.168.1.249:8088/test/

就会显示index.jsp内容。以上说的192.167.1.249是我nginx安装目录所在,自己测试时要根据自己实际的nginx所在服务器的ip来访问。

效果如下图:

访问测试用tomcat的webapps目录下的test/index.jsp的页面

upstream tomcat{ # 负载均衡站点的名称为tomcat,可以自己取
# ip_hash; # 可选,根据来源IP方式选择web服务器,省略的话按默认的轮循方式选择web服务器
server 127.0.0.1:8080 weight=1; # web服务器的IP地址及tomcat发布端口,  #weigth参数表示权值,权值越高被分配到的几率越大
server 127.0.0.1:8082 weight=1;

server 192.168.1.248:8081 weight=1;

server 192.168.1.248:8083 weight=1;

}

 说明:红色字体的tomcat这个名称随便命名,但在location中proxy_pass ,http://后面的名称要一致

  weigth参数表示权值,权值越高被分配到的几率越大

location 中加入有下划线的代码

location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://tomcat; # 负载均衡指向的发布服务tomcat
proxy_redirect default;
proxy_connect_timeout 10; #跟代理服务器连接的超时时间,必须留意这个time out时间不能超过75秒,当一台服务器当掉时,过10秒转发到另外一台服务器。
}

proxy_pass http://tomcat;  #是必须要加的

proxy_connect_timeout  10;   #这个参数是连接的超时时间。 我设置成10,表示是10秒后超时会连接到另外一台服务器

#其他参数自己选吧。

原文地址:https://www.cnblogs.com/shihaiming/p/5983476.html