配置多个tomcat服务,并用nginx做负载均衡
运行环境:redhat6.5,tomcat6,nginx1.4.2
tomcat包最好是tar包,二进制包不建议这种方法
配置现在开始:
复制整个tomcat文件,换文件夹名称。
例如:
第一个叫apache-tomcat1-6.0.41
第二个叫apache-tomcat2-6.0.41,以此类推
第一个的环境和配置文件端口号完全不用更改,只需该后添加的tomcat服务端口即可。
先添加全局环境变量,
.修改/etc目录下的profile文件,添加一组java环境变量,和两组CATALINA环境变量;修改后的profile文件示例如下:
JAVA_HOME=/usr/java/jdk
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JAVA_HOME/bin
export JAVA_HOME CLASSPATH
CATALINA_BASE=/usr/local/apache-tomcat1-6.0.41
CATALINA_HOME=/usr/local/apache-tomcat1-6.0.41
export CATALINA_BASE CATALINA_HOME
CATALINA_2_BASE=/usr/local/apache-tomcat2-6.0.41
CATALINA_2_HOME=/usr/local/apache-tomcat2-6.0.41
export CATALINA_2_BASE CATALINA_2_HOME
TOMCAT_HOME=/usr/local/apache-tomcat1-6.0.41
export TOMCAT_HOME
TOMCAT_2_HOME=/usr/local/apache-tomcat2-6.0.41
export TOMCAT_2_HOME
端口:8049->8050
9080
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />]\
端口:8009->8010
protocol="AJP/1.3" redirectPort="8443" />
保存退出后,
#source /etc/profile.
export CATALINA_HOME=$CATALINA_2_HOME 利用profile中第二组设置
export CATALINA_BASE=$CATALINA_2_BASE 利用profile中第二组设置
保存退出即可。
配置nginx.conf
进入当前环境下的nginx安装目录,配置nginx.conf文件如下:
user nginx;
worker_processes auto;
worker_rlimit_nofile 100000;
error_log logs/error.log;
events {
use epoll;
multi_accept on;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
charset UTF-8;
access_log on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" "$http_x_forwarded_for"';
access_log logs/access.log main;
error_log logs/error.log crit;
open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
proxy_temp_path /web/cache/tmp;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
#设置负载均衡,使用的是轮询策略(策略方案百度)
upstream jsht{
server 180.180.3.12:8080 fail_timeout=1s max_fails=3; #这是你的tomcat的服务器地址
server 180.180.3.12:8090 fail_timeout=1s max_fails=3; #
}
proxy_cache_path /web/cache levels=1:2 keys_zone=mycache:20m
max_size=2048m inactive=60m;
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
error_log logs/error.log debug;
#status
location /nginx_status{
stub_status on;
access_log on;
}
#error_page
error_page 404 /404.html;
location = /404.html {
root /web/error;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /web/error;
}
# location / {
# root /web/application;
# index index.html index.htm;
# }
#picture
# location ~.*\.(gif|jpeg|jpg|png|bmp|swf)$ {
# root /web/images;
# expires 30d;
# }
location / {
proxy_redirect off;
proxy_pass http://jsht;
#设置反向代理服务器
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
重启nginx服务,多路tomcat负载均衡配置完成了