前端负载均衡器由nginx服务器承担,nginx接收到服务器请求根据内部匹配规则实现动态请求和静态请求的分离,自身完成静态请求和并把动态请求发往后端tomcat服务器进行处理,发送的过程中根据负载均衡设置的权重进行分流。
Nginx 服务器:
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 < /dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mnginx服务运行正常,可 curl 查看\033[0m"
else
echo -e "\033[31mnginx服务运行异常,请检查\033[0m"
fi
查看安装结果
[root@localhost nginx-1.12.2]# curl -I http://192.168.126.11
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sat, 16 Oct 2021 17:04:11 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 16 Oct 2021 17:03:49 GMT
Connection: keep-alive
ETag: "616b05f5-264"
Accept-Ranges: bytes
[root@localhost ~]#cd /opt/ #上传安装包到/opt 目录
[root@localhost ~ /opt]#ls
apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm
[root@localhost ~ /opt]#rpm -ivh jdk-8u201-linux-x64.rpm #安装
[root@localhost ~ /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@localhost ~ /opt]#source /etc/profile.d/java.sh #将脚本导入到环境变量中,使其生效
[[root@localhost ~ /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@localhost ~ /opt]#tar zxvf apache-tomcat-9.0.16.tar.gz #解包
[root@localhost ~ /opt]#mv apache-tomcat-9.0.16 /usr/local/tomcat #转移包位置并改名
[root@localhost ~ /opt]#ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@localhost ~ /opt]#ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@localhost ~ /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@localhost ~ /opt]#netstat -antp |grep 8080 #查看是否开启成功
tcp6 0 0 :::8080 :::* LISTEN 2520/java
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/yj
[root@localhost ~]# vim /usr/local/tomcat/webapps/yj/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> #动态页面的配置
JSP test1 page
<% out.println("动态页面 1,http://www.test1.com");%>
#编辑tomcat主配置文件,添加虚拟主机配置,这里要先删掉原先的主机名等配置
[root@tm2134 /opt]#vim /usr/local/tomcat/conf/server.xml
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
[root@localhost ~ /opt]#shutdown.sh
[root@localhost ~ /opt]#startup.sh #重启服务
[root@localhost ~ /opt]#mkdir /usr/local/tomcat/webapps/yj #创建一个测试的目录
[root@localhost ~ /opt]#vim /usr/local/tomcat/webapps/yj/index.jsp #动态页面的配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JSP test2 page
<% out.println("动态页面 2,http://www.test2.com");%>
#编辑tomcat主配置文件,添加虚拟主机配置,这里要先删掉原先的主机名等配置
[root@tm2134 /opt]#vim /usr/local/tomcat/conf/server.xml
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
[root@localhost ~ /opt]#shutdown.sh
[root@localhost ~ /opt]#startup.sh #重启服务
[root@localhost ~]#echo 'this is static
' > /usr/local/nginx/html/index.html
[root@localhost ~]#cat /usr/local/nginx/html/index.html
this is static
[root@localhost /usr/local/nginx/html]#mkdir /usr/local/nginx/html/picture #上传图片
[root@localhost /usr/local/nginx/html]#cd picture/
[root@localhost /usr/local/nginx/html/picture]#rz -E
[root@localhost /usr/local/nginx/html/picture]#ls
yal.jpg
#主配置文件57行下添加下面配置参数,让页面能够加载图片
[root@localhost /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@localhost /usr/local/nginx/html/picture]#nginx -t
[root@localhost /usr/local/nginx/html/picture]#systemctl restart nginx.service
[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
......
#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
#gzip on; #33行下面加入以下内容
upstream tomcat_server {
server 192.168.126.13:8080 weight=1;
server 192.168.126.14: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@localhost ~]#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@localhost ~]#systemctl restart nginx.service
①可将两个或多个Tomcat server 放到 Nginx 的 upstream 中组成一个负载均衡集群,然后通过 proxy_pass 这种 Web 代理的方式在 location 中设置集群站点,然后再通过 weight 值来分别对 Tomcat server 进行权重的设置。
②在生产环境中,Tomcat server 的硬件配置可能不尽相同,可以通过修改相应服务器的 weight 值,对配置较高或配置较低的服务器的访问请求进行分配控制