首頁 > 軟體

伺服器的負載均衡nginx+tomcat實現動靜分離

2022-03-19 16:00:20

本文實驗內容nginx+tomcat實現網站的動靜分離功能

案例環境

主機資訊

架構圖

案例實施:

部署tomcat伺服器

兩臺tomcat伺服器上準備jdk和tomcat

jdk下載

tomcat下載

mkdir /tools
cd /tools
ls
apache-tomcat-8.5.70.tar.gz  jdk-8u181-linux-x64.tar.gz

安裝jdk

mkdir -p /app/java
tar xf jdk-8u181-linux-x64.tar.gz -C /app/
ln -s /app/jdk1.8.0_181/ /app/jdk
vim /etc/profile
export JAVA_HOME=/app/jdk
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$PATH:$JAVA_HOME/bin

source /etc/profile
java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

部署tomcat

tar xf apache-tomcat-8.5.70.tar.gz -C /app
ln -s /app/apache-tomcat-8.5.70/ /app/tomcat

啟動tomcat並測試,tomcat預設啟動8080埠

/app/tomcat/bin/startup.sh
ss -anpt | grep 8080
LISTEN     0      100       [::]:8080                  [::]:*                   users:(("java",pid=2375,fd=52))

看到上面這個頁面說明部署成功

建立tomcat01站點主頁

rm -rf /app/tomcat/webapps/ROOT/*
cat /app/tomcat/webapps/ROOT/index.jsp 
<%@ page language="java" import="java.util.*」 pageEncoding="UTF-8"%>
<html>
  <head>
    <title>tomcat01</title>
  </head>
  <body>
    <% out.println("192.168.10.4,tomcat01");%>
    <img src="3333.jpg" />
  </body>
</html>

建立womcat02站點主頁

rm -rf /app/tomcat/webapps/ROOT/*
cat /app/tomcat/webapps/ROOT/index.jsp 
<%@ page language="java" import="java.util.*「 pageEncoding="UTF-8"%>
<html>
  <head>
    <title>tomcat02</title>
  </head>
  <body>
    <% out.println("192.168.10.5,tomcat02");%>
    <img src="3333.jpg" />
  </body>
</html>

兩臺tomcat伺服器重啟服務

/app/tomcat/bin/shutdown.sh 
/app/tomcat/bin/startup.sh 

部署nginx伺服器

安裝nginx依賴

yum -y install gcc gcc-c++ autoconf pcre-devel make automake httpd-tools enopssl-devel

設定nginx軟體源

cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

安裝nginx服務並啟動

yum clean all
yum makecache
yum -y install nginx
systemctl enable --now nginx

yum安裝的nginx已啟用組態檔分離功能,我們使用預設的組態檔

cat /etc/nginx/nginx.conf
include /etc/nginx/conf.d/*.conf;
# 修改預設組態檔
vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  192.168.10.3;

建立站點首頁

cd /usr/share/nginx/html/
# 準備一張圖片
ls
3333.jpg  50x.html  index.html
# 設定站點首頁
vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
        <img src="3333.jpg" />
</body>
</html>

重啟nginx服務

systemctl reload nginx

吶,圖片

部署nginx-lb伺服器

安裝步驟與nginx01前三步相同

建立負載均衡組態檔

vim /etc/nginx/conf.d/proxy.conf
upstream tomcat {
	server 192.168.10.4:8080;
	server 192.168.10.5:8080;
}
upstream static {
	server 192.168.10.3:80;
}
server {
	listen 80;
	server_name 192.168.10.2;
	location / {
		proxy_pass http://tomcat;
	}
	location ~* .*.(png|gif|jpg)$ {
		proxy_pass http://static;
	}
}

重啟nginx服務

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl reload nginx

原理為預設會存取tomcat後端伺服器,但當網頁中有靜態圖片時會在static後端中存取

存取測試:達到一個負載均衡且動靜分離的效果

以上就是伺服器的負載均衡nginx+tomcat實現動靜分離的詳細內容,更多關於負載均衡nginx+tomcat動靜分離的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com