通常情况下,一个 Tomcat 站点由于可能出现单点故障以及无法应付过多客户复杂多样的请求等问题,不能单独应用于生产环境中,所以需要一套更可靠的解决方案来完善 Web 站点架构
Nginx 是一款非常优秀的 http 服务器软件,它能够支持高达 50000 个并发连接数的响应,拥有强大的静态资源处理能力,运行十分的稳定,并且内存、CPU 等系统资源消耗非常低
目前很多大型网站都应用 Nginx 服务器作为后端网站程序的反向代理及负载均衡器,来提升整个站点的负载并发能力
案例由 Nginx 作为负载均衡器,Tomcat 作为应用服务器的负载集群的设置方法
1.Nginx配置反向代理,负载均衡主要参数
upstream 服务池名 {
配置后端服务器池,以提供响应数据
}
upstream也称为上游服务器,是在Nginx进行集群开发时配置服务器的称呼
proxy_pass http://服务名
配置将访问请求转发给后端服务器池的服务器处理
2.Nginx 动静分离实现原理
动静分离原理:
服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx直接提供服务给客户端,动态资源Nginx通过反向代理转至后端
3.Nginx 静态处理的优势
主机 | 操作系统 | IP地址 | 主机软件环境 |
---|---|---|---|
Nginx服务器 | CentOS 7.4 x86_64 | 192.168.113.128 | nginx-1.22.0.tar.gz |
Tomcat服务器1 | CentOS 7.4 x86_64 | 192.168.113.129 | ①apache-tomcat-9.0.16.tar.gz / ②jdk-8u201-linux-x64.rpm |
Tomcat服务器2 | CentOS 7.4 x86_64 | 192.168.113.125 | ①apache-tomcat-9.0.16.tar.gz / ② jdk-8u201-linux-x64.rpm |
[root@tomcat1 ~]# systemctl stop firewalld
[root@tomcat1 ~]# systemctl disable firewalld
[root@tomcat1 ~]# setenforce 0
#!/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.22.0.tar.gz -P /opt
tar zxvf /opt/nginx-1.22.0.tar.gz -C /opt
cd /opt/nginx-1.22.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
--with-file-aio \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module
--with-stream
cd /opt/nginx-1.22.0
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
--with-file-aio \ #启用文件修改支持
--with-http_stub_status_module \ #启用状态统计
--with-http_gzip_static_module \ #启用 gzip静态压缩
--with-http_flv_module \ #启用 flv模块,提供对 flv 视频的伪流支持
--with-http_ssl_module #启用 SSL模块,提供SSL加密功能
--with-stream #启用 stream模块,提供4层调
[root@server2 ~]# curl -I 192.168.113.128
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Fri, 29 Jul 2022 08:30:18 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 20 Jul 2022 15:14:37 GMT
Connection: keep-alive
ETag: "62d81bdd-267"
Accept-Ranges: bytes
分流策略
每个请求按时间顺序逐一分配到不同的后端服务器,如果超过了最大失败次数后(max_fails,默认1),在失效时间内(fail_timeout,默认10秒),该节点失效权重变为0,超过失效时间后,则恢复正常,或者全部节点都为down后,那么将所有节点都恢复为有效继续探测,一般来说rr可以根据权重来进行均匀分配。
优先将客户端请求调度到当前连接最少的服务器
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题,但是ip_hash会造成负载不均,有的服务请求接受多,有的服务请求接受少,所以不建议采用ip_hash模式,session 共享问题可用后端服务的 session 共享代替 nginx 的 ip_hash
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
基于用户请求的uri做hash。和ip_hash算法类似,是对每个请求按url的hash结果分配,使每个URL定向到同一个后端服务器,但是也会造成分配不均的问题,这种模式后端服务器为缓存时比较好
weight参数表示权重,权重越高,被分配到的概率越大,例 weight=1
Tomcat服务器1与 Tomcat 服务器2 配置方法基本相同
192.168.113.129配置tomcat
1.安装 JDK ,配置环境
[root@tomcat1 ~]# cd /opt ##上传安装包到/opt 目录
[root@tomcat1 opt]# ls
apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm rh
[root@tomcat1 opt]# rpm -ivh jdk-8u201-linux-x64.rpm ##安装或(tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local ,jdk-8u91-linux-x64.tar.gz)
[root@tomcat1 opt]# vim /etc/profile.d/java.sh 或vim /etc/profile切换至行尾加入变量
##/etc/profile.d/环境变量脚本目录
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat1 opt]# source /etc/profile.d/java.sh 或source /etc/profile
##将脚本导入到环境变量中,使其生效
[root@tomcat1 opt]# java -version ##查看版本
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
2.解包
[root@tomcat1 opt]# tar zxvf apache-tomcat-9.0.16.tar.gz ##解包
[root@tomcat1 opt]# mv /opt/apache-tomcat-9.0.16 /usr/local/tomcat ##转移包位置并改名
3.添加用户设置属主属组
[root@tomcat1 tomcat]# useradd -s /sbin/nologin tomcat
[root@tomcat1 tomcat]# chown tomcat:tomcat /usr/local/tomcat -R
4.新建服务文件
[root@tomcat1 tomcat]# vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
##重新加载服务,并开启,查看是否成功启动
systemctl daemon-reload
systemctl start tomcat
netstat -ntap |grep 8080
新建动态页面站点
5.切换至webapp下,新建test目录
cd /usr/local/tomcat/webapps/
mkdir pxx
6.建立动态页面文件
[root@tomcat1 webapps]# vim pxx/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("自强不息");%>
</body>
</html>
7.修改主配置文件
[root@tomcat1 webapps]# cp /usr/local/tomcat/conf/server.xml /usr/local/tomcat/conf/server.xml.bak ##备份一下以防万一
[root@tomcat1 webapps]# vim /usr/local/tomcat/conf/server.xml ##删除原来的站点模块如图
##164行下添加
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/pxx"
path="" reloadable="true" />
</Host>
8.重启服务,并在网页测试
[root@tomcat1 webapps]# systemctl restart tomcat.service
systemctl stop firewalld
setenforce 0
[root@localhost opt]# rpm -ivh jdk-8u201-linux-x64.rpm
[root@localhost opt]# mkdir /usr/local/tomcat
[root@localhost opt]# mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
[root@localhost opt]# cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
[root@www opt]# vim /etc/profile.d/tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
##刷新让系统识别变量
[root@www opt]# source /etc/profile.d/tomcat.sh
[root@www opt]# vim /usr/local/tomcat/tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN"> ##22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1" ##69行,修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> ##116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
##tomcat1添加
[root@www opt]# vim /usr/local/tomcat/tomcat1/bin/startup.sh
##在二十行下添加插入
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
[root@www opt]# vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
##在二十行下添加插入
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
##tomcat2添加
[root@www opt]# vim /usr/local/tomcat/tomcat2/bin/startup.sh
##在二十行下添加插入
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
[root@www opt]# vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
##在二十行下添加插入
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
[root@www opt]# /usr/local/tomcat/tomcat1/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/tomcat1
Using CATALINA_HOME: /usr/local/tomcat/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat1/bin/bootstrap.jar:/usr/local/tomcat/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[root@www opt]# /usr/local/tomcat/tomcat2/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/tomcat2
Using CATALINA_HOME: /usr/local/tomcat/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat2/bin/bootstrap.jar:/usr/local/tomcat/tomcat2/bin/tomcat-juli.jar
Tomcat started.
##查看是否开启
[root@www opt]# netstat -natp | grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 4089java
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 4159java
tcp6 0 0 :::8009 :::* LISTEN 4089java
tcp6 0 0 :::8010 :::* LISTEN 4159java
tcp6 0 0 :::8080 :::* LISTEN 4089java
tcp6 0 0 :::8081 :::* LISTEN 4159java
[root@localhost opt]# mkdir /usr/local/tomcat/tomcat1/webapps/test
[root@localhostopt]# vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title> #指定为 test1 页面
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>
##由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置
[root@localhost opt]# vim /usr/local/tomcat/tomcat1/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true">
</Context>
</Host>
##关闭重启,tomcat配置完成
[root@localhost opt]# /usr/local/tomcat/tomcat1/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat/tomcat1
Using CATALINA_HOME: /usr/local/tomcat/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat1/bin/bootstrap.jar:/usr/local/tomcat/tomcat1/bin/tomcat-juli.jar
[root@localhost opt]# /usr/local/tomcat/tomcat1/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/tomcat1
Using CATALINA_HOME: /usr/local/tomcat/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat1/bin/bootstrap.jar:/usr/local/tomcat/tomcat1/bin/tomcat-juli.jar
Tomcat started.
##查看是否开启
[root@localhost opt]# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 72039 root 54u IPv6 152152 0t0 TCP *:webcache (LISTEN)
[root@localhost opt]# mkdir /usr/local/tomcat/tomcat2/webapps/test
[root@localhost opt]# vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title> #指定为 test2 页面
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>
##由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置
[root@localhost opt]# vim /usr/local/tomcat/tomcat2/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true">
</Context>
</Host>
##关闭重启,tomcat配置完成
[root@localhost opt]# /usr/local/tomcat/tomcat2/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat/tomcat2
Using CATALINA_HOME: /usr/local/tomcat/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat2/bin/bootstrap.jar:/usr/local/tomcat/tomcat2/bin/tomcat-juli.jar
[root@localhost opt]# /usr/local/tomcat/tomcat2/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/tomcat2
Using CATALINA_HOME: /usr/local/tomcat/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat2/bin/bootstrap.jar:/usr/local/tomcat/tomcat2/bin/tomcat-juli.jar
Tomcat started.
##查看是否开启
[root@localhost opt]# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 72039 root 54u IPv6 152152 0t0 TCP *:webcache (LISTEN)
[root@server2 conf]# cd /usr/local/nginx/html/ ##拖入添加图片
[root@server2 html]# ls
50x.html dpp.jpg index.html
[root@server2 conf]# pwd
/usr/local/nginx/conf
[root@server2 conf]# vim nginx.conf
##配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
##33行下面加入以下内容
#gzip on;
upstream tomcat_server {
server 192.168.113.129:8080 weight=1;
server 192.168.113.125:8080 weight=1;
server 192.168.113.125:8081 weight=1;
}
##在45行下加入下面配置参数server下面配置,添加反向代理,动静分离
#access_log logs/host.access.log main;
location ~ .*\.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
##添加参数解析
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都记录下来
##主配置文件57行下添加下面配置参数,配置Nginx处理静态图片请求,让页面能够加载图片
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html;
expires 10d;
}
location / {
root html;
index index.html index.htm;
}
##检查主配置文件语法并重启服务
[root@server2 conf]# 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@server2 conf]# systemctl restart nginx.service
##火狐静态页面效果