Tomcat+Nginx集群

Posted 幺刀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat+Nginx集群相关的知识,希望对你有一定的参考价值。

# Tomcat+nginx集群
## Tomcat
* 修改/etc/profile中的环境变量
```
export JAVA_HOME=/usr/java/jdk1.7.0_80
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME$/lib/tools.jar
export MAVEN_HOME=/home/developer/apache-maven-3.0.5
export PATH=$PATH:$JAVA_HOME/bin:$CLASSPATH:$MAVEN_HOME/bin

export CATALINA_BASE1=/home/developer/apache-tomcat-7.0.73-1
export CATALINA_HOME1=/home/developer/apache-tomcat-7.0.73-1
export CATALINA_TMPDIR1=/home/developer/apache-tomcat-7.0.73-1
export TOMCAT_HOME1=/home/developer/apache-tomcat-7.0.73-1

export CATALINA_BASE2=/home/developer/apache-tomcat-7.0.73-2
export CATALINA_HOME2=/home/developer/apache-tomcat-7.0.73-2
export CATALINA_TMPDIR2=/home/developer/apache-tomcat-7.0.73-2
export TOMCAT_HOME2=/home/developer/apache-tomcat-7.0.73-2
```

* 增加第二个tomcat的catalina.sh配置
```
# OS specific support. $var _must_ be set to either true or false.
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
```

## nginx
* 修改conf/nginx.conf
```
include vhost/*.conf;
```
* vhost目录下增加文件www.x.com.conf
* www.x.com.conf中的内容
```
server {
default_type ‘text/html‘;
charset utf-8;
listen 80;
autoindex on;
server_name www.x.com;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;

location / {
proxy_pass http://127.0.0.1:8080;
}
}
```

## 负载均衡
weight表示访问权重,默认权重为1
## 修改www.x.com.conf中的内容
```
upstream www.x.com{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:9080 weight=9;
}
server {
default_type ‘text/html‘;
charset utf-8;
listen 80;
autoindex on;
server_name www.x.com;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;

location / {
proxy_pass http://www.x.com;
}
}
```


























































以上是关于Tomcat+Nginx集群的主要内容,如果未能解决你的问题,请参考以下文章

Nginx + Tomcat搭建集群

nginx+tomcat配置集群

Nginx+Tomcat简单集群

Nginx+Tomcat负载均衡集群

nginx+tomcat服务集群

图文详解 配置Nginx+Tomcat负载均衡动静分离集群