Nginx+Tomcat多实例及负载均衡配置
Nginx+Tomcat多实例及负载均衡配置
采用nginx的反向代理负载均衡功能,配合后端的tomcat多实例来实现tomcat WEB服务的负载均衡
01
安装nginx服务
安装所需的pcre库
tar zxf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make && make install
cd ../
编译安装Nginx服务
tar zxf nginx-1.11.3.tar.gz
cd nginx-1.11.3
useradd nginx -s /sbin/nologin -M
yum install openssl openssl-devel -y
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.11.3 --with-http_ssl_module --with-http_stub_status_module
make && make install
nginx服务的相关介绍及编译参数介绍可参考公众号前面的文章,有关服务的详细配置说明都有涉及到
02
安装JDK环境
Tomcat需要JDK的环境,因此在安装之前需要先安装JDK环境,首先下载好相应的JDK软件包
tar xf jdk-8u60-linux-x64.tar.gz -C /application/
##无需编译直接解压到指定目录
ln -s /application/jdk1.8.0_60 /application/jdk
##创建软链接
sed -i.ori '$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
##配置环境变量
source /etc/profile
##生效配置的环境变量
java -version
##查看JDK环境安装是否成功
03
安装配置Tomcat多实例
Tomcat的多实例配置也相当的简单,下载相应版本的软件,解压即可使用
tar xf apache-tomcat-8.0.27.tar.gz -C /application/
ln -s /application/apache-tomcat-8.0.27 /application/tomcat
echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile
多实例安装配置如下
cp -a apache-tomcat-8.0.27 /application/tomcat8_1
cp -a apache-tomcat-8.0.27 /application/tomcat8_2
切换到相关目录修改相应的配置文件即可
(两个实例的配置文件改动的地方相同,只需要修改端口与站点目录即可)
cd /application/tomcat8_2/conf/
diff /application/tomcat/conf/server.xml ./server.xml
22c22
< <Server port="8005" shutdown="SHUTDOWN">
---
> <Server port="8012" shutdown="SHUTDOWN">
69c69
< <Connector port="8080" protocol="HTTP/1.1"
---
> <Connector port="8082" protocol="HTTP/1.1"
123c123
< <Host name="localhost" appBase="webapps"
---
> <Host name="localhost" appBase="/web/www/bbs"
创建站点目录
mkdir /web/www/{www,bbs}/ROOT -p
echo "hello">/web/www/www/ROOT/index.jsp
echo "world">/web/www/bbs/ROOT/index.html
启动多实例
/application/tomcat8_1/bin/startup.sh
/application/tomcat8_2/bin/startup.sh
04
配置nginx
vim /application/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#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 web_pools {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.jsp index.html index.htm;
proxy_pass http://web_pools;
}
}
}
查检语法与启动nginx服务
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx
05
测试负载均衡效果
for i in `echo {1..6}`;do curl 192.168.1.129;done
hello
world
hello
world
hello
world
表明负载均衡配置已经实现
这是一个有温度的公众号