通常情况下,一个 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
#!/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 服务器2 配置方法基本相同
[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 #转移包位置并改名
[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
[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 #重启服务
[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 #重启服务
[root@ng140 ~]#echo 'this is static
' > /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
[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
http://192.168.8.140/index.jsp