Nginx是一款非常优秀的http服务器软件,它能够支持高达50000个并发连接数的相应,Nginx+Tomcat负载均衡集案列是应用于生产环境的一套可靠的Web站点解决方案,对Nginx Tomcat负载均衡集群相关知识感兴趣的朋友一起看看吧
正文
Nginx+Tomcat负载均衡集群安装配置案例详解
前言
介绍tomcat及nginx+tomcat负载均衡集群,tomcat的应用场景,然后重点介绍tomcat的安装配置。nginx+tomcat负载均衡集案列是应用于生产环境的一套可靠的web站点解决方案。
一、nginx+tomcat
通常情况下,一个tomcat站点由于可能出现单点故障及无法应付过多客户复杂多样的请求等问题,不能单独应用于生产环境下,所以我们需要一套更可靠的解决方案来完善web站点架构。
nginx是一款非常优秀的http服务器软件,它能够支持高达50000个并发连接数的相应,拥有强大的静态资源的处理能力,运行稳定,并且内存、cpu等系统资源消耗非常低。目前很多大型网站都应用nginx服务器为后端网站程序的反向代理及负载均衡器,来提升整个站点的负载并发能力。
部署环境
| 主机 | 操作系统 | ip地址 | 主要软件 |
|
nginx服务器 |
centos 7.4 x86_64 |
192.168.196.146 |
nginx-1.12.2.tar.gz |
|
tomcat服务器1 |
centos 7.4 x86_64 |
192.168.196.147 |
①apache-tomcat-9.0.16.tar.gz / ②jdk-8u201-linux-x64.rpm |
|
tomcat服务器2 |
centos 7.4 x86_64 |
192.168.196.153 |
①apache-tomcat-9.0.16.tar.gz / ② jdk-8u201-linux-x64.rpm |
二、配置nginx服务器
1.关闭防火墙相关服务
|
1
2
3
4
5
6
|
[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# systemctl disable firewalld[root@localhost ~]# setenforce 0 [root@localhost ~]# vim /etc/resolv.confnameserver 114.114.114.114 |
2.安装依赖包
|
1
|
[root@localhost ~]# yum install -y gcc gcc-c++ pcre-devel zlib-devel make |
3.编译安装nginx
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz -c /opt/ [root@localhost ~]# cd /opt/nginx-1.12.2/[root@localhost nginx-1.12.2]# ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module [root@localhost nginx-1.12.2]# make && make install [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/[root@localhost ~]# useradd -m -s /sbin/nologin nginx |
4.添加nginx系统服务
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost ~]# vim /lib/systemd/system/nginx.service [unit]description=nginxafter=network.target[service]type=forkingpidfile=/usr/local/nginx/logs/nginx.pidexecstart=/usr/local/nginx/sbin/nginxexecrreload=/bin/kill -s hup $mainpidexecrstop=/bin/kill -s quit $mainpidprivatetmp=true[install]wantedby=multi-user.target [root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service #赋权,除了root以外的用户都不能修改[root@localhost ~]# systemctl start nginx.service[root@localhost ~]# systemctl enable nginx.service |
5.网页测试

三、部署tomcat应用服务器
1.实施准备
|
1
2
3
|
[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# systemctl disable firewalld.service[root@localhost ~]# setenforce 0 |
2.安装jdk,配置java环境
|
1
|
[root@localhost ~]# rpm -ivh jdk-8u201-linux-x64.rpm |
3.设置jdk环境
|
1
2
3
4
5
6
7
8
|
[root@localhost ~]# vim /etc/profile...#插入三行内容export java_home=/usr/java/jdk1.8.0_201-amd64export classpath=.:$java_home/lib/tools.jar:$java_home/lib/dt.jarexport path=$java_home/bin:$path [root@localhost ~]# source /etc/profile |
4.安装配置tomcat
|
1
2
3
|
[root@localhost ~]# tar zxvf apache-tomcat-9.0.16.tar.gz -c /opt/[root@localhost ~]# cd /opt/[root@localhost opt]# mv apache-tomcat-9.0.16/ /usr/local/tomcat |
5. 优化管理
|
1
2
|
[root@localhost ~]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/[root@localhost ~]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/ |
6.启动服务startup.sh


tomcat1的配置
1.创建一个测试目录
|
1
|
|
2.动态页面的配置
|
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~]# vim /usr/local/tomcat/webapps/test/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> |
|
1
2
3
4
5
6
7
|
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml...<context docbase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> #大约160行插入... [root@localhost ~]# shutdown.sh[root@localhost ~]# startup.sh |
tomcat2配置
1.创建一个测试目录
|
1
|
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/test |
2.动态页面的配置
|
1
2
3
4
5
6
7
8
9
10
|
[root@localhost ~]# vim /usr/local/tomcat/webapps/test/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> |
|
1
2
3
4
5
6
7
|
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml...<context docbase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> #大约160行插入... [root@localhost ~]# shutdown.sh[root@localhost ~]# startup.sh |
3.nginx准备静态页面
|
1
|
[root@localhost ~]# echo '<html><body><h1>静态界面...</h1></body></html>' > /usr/local/nginx/html/index.html |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf...upstream tomcat_server { server 192.168.192.147:8080 weight=1; server 192.168.192.153:8080 weight=1;} 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@localhost ~]#systemctl restart nginx.service |
4.网页测试效果



总结
可将两个或多个tomcat server 放到 nginx 的 upstream 中组成一个负载均衡集群,然后通过 proxy_pass 这种 web 代理的方式在 location 中设置集群站点,然后再通过 weight 值来分别对 tomcat server 进行权重的设置。
在生产环境中,tomcat server 的硬件配置可能不尽相同,可以通过修改相应服务器的 weight 值,对配置较高或配置较低的服务器的访问请求进行分配控制
到此这篇关于nginx+tomcat负载均衡集群安装配置案例详解的文章就介绍到这了,更多相关nginx tomcat负载均衡集群内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
原文链接:https://blog.csdn.net/m0_59439550/article/details/120850393

发表评论