Nginx+Tomcat负载均衡集群的实现示例

 更新时间:2021年10月18日 12:06:25   作者:头发莫的了呀  
本文主要介绍了Nginx + Tomcat负载均衡集群的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

引言

通常情况下,一个 Tomcat 站点由于可能出现单点故障以及无法应付过多客户复杂多样的请求等问题,不能单独应用于生产环境中,所以需要一套更可靠的解决方案来完善 Web 站点架构。

一、案例概述

Nginx 是一款非常优秀的 http 服务器软件,它能够支持高达 50000 个并发连接数的响应,拥有强大的静态资源处理能力,运行十分的稳定,并且内存、CPU 等系统资源消耗非常低。

目前很多大型网站都应用 Nginx 服务器作为后端网站程序的反向代理及负载均衡器,来提升整个站点的负载并发能力

案例由 Nginx 作为负载均衡器,Tomcat 作为应用服务器的负载集群的设置方法,架构图如下

在这里插入图片描述

二、环境部署

 案例环境如下

主机 操作系统 IP地址 主要软件
Nginx服务器 CentOS 7.4 x86_64 192.168.8.140 nginx-1.12.2.tar.gz
Tomcat服务器1 CentOS 7.4 x86_64 192.168.8.133 ①apache-tomcat-9.0.16.tar.gz / ②jdk-8u201-linux-x64.rpm
Tomcat服务器2 CentOS 7.4 x86_64 192.168.8.134 ①apache-tomcat-9.0.16.tar.gz / ② jdk-8u201-linux-x64.rpm

关闭防火墙,关闭开机自启

[root@ng133 ~]#systemctl stop firewalld.service            #关闭防火墙及开机自启功能
[root@ng133 ~]#systemctl status firewalld.service
[root@ng133 ~]#setenforce 0								#关闭安全增强系统
[root@ng133 ~]#setenforce: SELinux is disabled

三、Nginx 主机安装

安装 Nginx 服务,这里用的一键部署脚本

#!/bin/bash
iptables -F
yum -y install epel-release && yum clean all && yum makecache
yum -y install pcre-devel zlib-devel gcc gcc-c++ make wget
useradd -M -s /sbin/nologin nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz -P /opt
tar zxvf /opt/nginx-1.12.2.tar.gz -C /opt
cd /opt/nginx-1.12.2

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

cd /opt/nginx-1.12.2
make -j 4 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

chmod 754 /usr/lib/systemd/system/nginx.service
systemctl daemon-reload && systemctl start nginx.service && systemctl enable nginx.service

echo " "
pgrep "nginx" &> /dev/null
if [ $? -eq 0 ];then
        echo -e "\033[32mnginx服务运行正常,可 curl 查看\033[0m"
else
        echo -e "\033[31mnginx服务运行异常,请检查\033[0m"
fi

安装完成后查看结果

[root@ng140 /opt/nginx-1.12.2]#curl -I http://192.168.8.140
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 15 Oct 2021 01:46:17 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 15 Oct 2021 01:45:34 GMT
Connection: keep-alive
ETag: "6168dd3e-264"
Accept-Ranges: bytes

在这里插入图片描述

四、Tomcat 安装及配置

1. 安装 Tomcat

Tomcat 服务器1与 Tomcat 服务器2 配置方法基本相同

安装 JDK ,配置环境

[root@tm1133 ~]#cd /opt/									   #上传安装包到/opt 目录
[root@tm1133 /opt]#ls
apache-tomcat-9.0.16.tar.gz  jdk-8u201-linux-x64.rpm  
[root@tm1133 /opt]#rpm -ivh jdk-8u201-linux-x64.rpm			   #安装
[root@tomcat /opt]#vim /etc/profile.d/java.sh				   #/etc/profile.d/环境变量脚本目录	

export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar		
export PATH=$JAVA_HOME/bin:$PATH

[root@tm1133 /opt]#source /etc/profile.d/java.sh 			   #将脚本导入到环境变量中,使其生效
[root@tm1133 /opt]#java -version							   #查看版本
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

解包

[root@tm1133 /opt]#tar zxvf apache-tomcat-9.0.16.tar.gz  	    #解包
[root@tm1133 /opt]#mv apache-tomcat-9.0.16 /usr/local/tomcat	#转移包位置并改名

启动并优化管理 Tomcat(创建软连接,优化开启命令)

[root@tm1133 /opt]#ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@tm1133 /opt]#ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tm1133 /opt]#startup.sh 								   #开启
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tm1133 /opt]#netstat -antp |grep 8080					  #查看是否开启成功
tcp6       0      0 :::8080                 :::*                    LISTEN      2520/java  

2. Tomcat 服务器1配置

[root@tm1133 ~]#mkdir /usr/local/tomcat/webapps/gl          			 #创建一个测试的目录
[root@tm1133 ~]#vim /usr/local/tomcat/webapps/gl/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>     #动态页面的配置
<html> 
<head>
<title>JSP test1 page </title>
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>


#编辑tomcat主配置文件,添加虚拟主机配置,这里要先删掉原先的主机名等配置
[root@tm2134 /opt]#vim /usr/local/tomcat/conf/server.xml			   
-------------------------------------------------------------------------------------------------
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" 
#找到这个先删掉,否则最后会出错
-------------------------------------------------------------------------------------------------
#在行尾162行处插入下面配置,需注意结尾的</Host>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
     <Context docBase="/usr/local/tomcat/webapps/gl" path="" reloadable="true" />
</Host>
-------------------------------------------------------------------------------------------------
[root@tm2134 /opt]#shutdown.sh		
[root@tm2134 /opt]#startup.sh			 								#重启服务

3. Tomcat 服务器2配置

[root@tm2134 /opt]#mkdir /usr/local/tomcat/webapps/gl				#创建一个测试的目录
[root@tm2134 /opt]#vim /usr/local/tomcat/webapps/gl/index.jsp			#动态页面的配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page </title>
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>


#编辑tomcat主配置文件,添加虚拟主机配置,这里要先删掉原先的主机名等配置
[root@tm2134 /opt]#vim /usr/local/tomcat/conf/server.xml			   
-------------------------------------------------------------------------------------------------
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" 
#找到这个先删掉,否则最后会出错
-------------------------------------------------------------------------------------------------
#在行尾162行处插入下面配置,需注意结尾的</Host>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
     <Context docBase="/usr/local/tomcat/webapps/gl" path="" reloadable="true" />
</Host>
-------------------------------------------------------------------------------------------------
[root@tm2134 /opt]#shutdown.sh		
[root@tm2134 /opt]#startup.sh			 								#重启服务

五、Nginx server 配置

静态页面配置

[root@ng140 ~]#echo '<html><body><h1>this is static</h1></body></html>' > /usr/local/nginx/html/index.html
[root@ng140 ~]#cat /usr/local/nginx/html/index.html 
<html><body><h1>this is static</h1></body></html>

[root@ng140 /usr/local/nginx/html]#mkdir /usr/local/nginx/html/picture		#上传图片
[root@ng140 /usr/local/nginx/html]#cd picture/
[root@ng140 /usr/local/nginx/html/picture]#rz -E
rz waiting to receive.
[root@ng140 /usr/local/nginx/html/picture]#ls
ha.jpg
#主配置文件57行下添加下面配置参数,让页面能够加载图片
[root@ng140 /usr/local/nginx/html/picture]#vim /usr/local/nginx/conf/nginx.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/picture;
expires 10d;
 }
[root@ng140 /usr/local/nginx/html/picture]#nginx -t
[root@ng140 /usr/local/nginx/html/picture]#systemctl restart nginx.service 

配置nginx主配置文件

[root@ng140 ~]#vim /usr/local/nginx/conf/nginx.conf
......
#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大

#gzip  on;													#33行下面加入以下内容
upstream tomcat_server {
server 192.168.8.133:8080 weight=1;
server 192.168.8.134:8080 weight=1;
}  


															#45行下加入下面配置参数
 45			#access_log  logs/host.access.log  main;

 46         location ~ .*\.jsp$ {
 47         proxy_pass http://tomcat_server; 
 48         proxy_set_header HOST $host;
 49         proxy_set_header X-Real-IP $remote_addr;                
 50         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 51        }            

添加参数解析

location ~ .*\.jsp$ {			
#把nginx接收到的客户端的ip地址赋值给跳转到tomcat请求中的源ip,识别客户的真实ip,并且赋值与跳转
proxy_pass http://tomcat_server; 

proxy_set_header HOST $host;	
#设定后端的web服务器接收到的请求访问的主机名(域名或ip、端口),默认host的值为proxy_pass直连设置的主机名

proxy_set_header X-Real-IP $remote_addr;		
#把$remote_addr复制给X-Real-IP(自定义),来回去源IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;		
#在nginx最为代理服务器时,设置的IP列表,会把经过的及其ip,代理及其ip都记录下来

检查主配置文件语法并重启服务

[root@ng140 ~]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@ng140 ~]#systemctl restart nginx.service

六、验证结果

用火狐浏览器测试静态页面的效果

在这里插入图片描述

测试负载均衡是否生效,刷新网页可以看到在动态页面1和2之间来回切换

http://192.168.8.140/index.jsp

在这里插入图片描述

在这里插入图片描述

总结

可将两个或多个Tomcat server 放到 Nginx 的 upstream 中组成一个负载均衡集群,然后通过 proxy_pass 这种 Web 代理的方式在 location 中设置集群站点,然后再通过 weight 值来分别对 Tomcat server 进行权重的设置。

在生产环境中,Tomcat server 的硬件配置可能不尽相同,可以通过修改相应服务器的 weight 值,对配置较高或配置较低的服务器的访问请求进行分配控制。

到此这篇关于Nginx+Tomcat负载均衡集群的实现示例的文章就介绍到这了,更多相关Nginx+Tomcat负载均衡集群内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 深入探究Nginx体系化之虚拟主机分类及配置实现

    深入探究Nginx体系化之虚拟主机分类及配置实现

    Nginx,这款备受推崇的高性能 Web 服务器,以其强大的性能和灵活的配置而广受欢迎,在实际应用中,虚拟主机是一项重要的功能,允许我们在单个服务器上托管多个网站,本文将深入探讨 Nginx 虚拟主机的分类和配置实现,帮助您构建一个高效多站点托管平台
    2023-08-08
  • Nginx进程调度问题详解

    Nginx进程调度问题详解

    Nginx采用的是固定数量的多进程模型,由一个主进程(MasterProcess)和数量与主机CPU核数相同的工作进程协同处理各种事件。这篇文章主要介绍了Nginx进程调度问题,需要的朋友可以参考下
    2021-09-09
  • iis+nginx实现负载均衡的详细步骤

    iis+nginx实现负载均衡的详细步骤

    这篇文章主要为大家详细介绍了iis+nginx实现负载均衡的详细步骤 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 开启Nginx时端口被占用提示:Address already in use

    开启Nginx时端口被占用提示:Address already in use

    这篇文章主要介绍了开启Nginx时端口被占用提示:Address already in use的解决方法,文中通过两种方法给大家介绍了Nginx的启动、停止与重启 的操作方法 ,需要的朋友可以参考下
    2018-09-09
  • 使用nginx模拟进行蓝绿部署的方式

    使用nginx模拟进行蓝绿部署的方式

    今天小编就为大家分享一篇关于使用nginx模拟进行蓝绿部署的方式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 详解nginx如何配置HTTPS

    详解nginx如何配置HTTPS

    本篇文章主要介绍了详解nginx如何配置HTTPS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • nginx常用配置conf的示例代码详解

    nginx常用配置conf的示例代码详解

    这篇文章主要介绍了nginx常用配置conf,包括配置vue项目,配置接口代理的代码详解,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • Nginx配置文件详解

    Nginx配置文件详解

    Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。这篇文章主要介绍了Nginx配置详解,需要的朋友可以参考下
    2017-05-05
  • nginx 多个location转发任意请求或访问静态资源文件的实现

    nginx 多个location转发任意请求或访问静态资源文件的实现

    这篇文章主要介绍了nginx 多个location转发任意请求或访问静态资源文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Nginx转发socket端口配置详解

    Nginx转发socket端口配置详解

    这篇文章主要介绍了Nginx转发socket端口配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06

最新评论