您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Nginx+Tomcat怎么配置負(fù)載均衡和動靜分離,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
支持高達(dá)50000個(gè)并發(fā)連接數(shù)的響應(yīng)
擁有強(qiáng)大的靜態(tài)資源處理能力
運(yùn)行穩(wěn)定
內(nèi)存、CPU等系統(tǒng)資源消耗非常低
目前很多大型網(wǎng)站都應(yīng)用Nginx服務(wù)器作為后端網(wǎng)站程序的反向代理及負(fù)載均衡器,提升整個(gè)站點(diǎn)的負(fù)載并發(fā)能力
配置后端服務(wù)器池,以提供響應(yīng)數(shù)據(jù)
配置將訪問請求轉(zhuǎn)發(fā)給后端服務(wù)器池的服務(wù)器處理
Nginx處理靜態(tài)頁面的效率遠(yuǎn)高于Tomcat的處理能力
如果Tomcat的請求量為1000次,則Nginx的請求量為6000次
Tomcat每秒的吞吐量為0.6M,Nginx的每秒吞吐量為3.6M
Nginx處理靜態(tài)資源的能力是Tomcat處理的6倍,優(yōu)勢可見一斑
服務(wù)端接收來自客戶端的請求中,既有靜態(tài)資源也有動態(tài)資源
Nginx服務(wù)器:192.168.52.135 Tomcat服務(wù)器1:192.168.52.134 Tomcat服務(wù)器2:192.168.52.150
[root@tomcat1 ~]# mkdir /mnt/tools //創(chuàng)建掛載目錄 [root@tomcat1 ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ //掛載共享工具包 Password for root@//192.168.100.100/tools: [root@tomcat1 ~]# cd /mnt/tools/tomcat/ //切換到掛載目錄 [root@tomcat1 tomcat]# ls 12D18CFCD6599AFF0445766ACC4CA231C5025773.torrent apache-jmeter-5.1.zip jdk-8u201-linux-x64.rpm apache-jmeter-5.1 apache-tomcat-9.0.16.tar.gz tomcat優(yōu)化壓測.jmx [root@tomcat1 tomcat]# [root@tomcat1 tomcat]# rpm -ivh jdk-8u201-linux-x64.rpm //直接安裝rpm包
[root@tomcat1 tomcat]# cd /usr/java/jdk1.8.0_201-amd64/ //切換目錄 [root@tomcat1 jdk1.8.0_201-amd64]# vim /etc/profile //修改系統(tǒng)環(huán)境變量文件 ##文件末尾添加環(huán)境變量 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@tomcat1 jdk1.8.0_201-amd64]# source /etc/profile //重新加載配置文件 [root@tomcat1 jdk1.8.0_201-amd64]# 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@tomcat1 jdk1.8.0_201-amd64]#
[root@tomcat1 jdk1.8.0_201-amd64]# cd /mnt/tools/tomcat/ //切換到掛載目錄 [root@tomcat1 tomcat]# tar zxf apache-tomcat-9.0.16.tar.gz -C /opt/ //解壓到“/opt/”目錄 [root@tomcat1 tomcat]# mv /opt/apache-tomcat-9.0.16/ /usr/local/tomcat9 //移動到“/usr/local/”目錄并重命名 [root@tomcat1 bin]# ln -s /usr/local/tomcat9/bin/startup.sh /usr/bin/ //建立啟動腳本軟鏈接 [root@tomcat1 bin]# ln -s /usr/local/tomcat9/bin/shutdown.sh /usr/bin/ //建立關(guān)閉腳本軟鏈接 [root@tomcat1 bin]# [root@tomcat1 bin]# startup.sh //開啟Tomcat服務(wù) Using CATALINA_BASE: /usr/local/tomcat9 Using CATALINA_HOME: /usr/local/tomcat9 Using CATALINA_TMPDIR: /usr/local/tomcat9/temp Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64 Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar Tomcat started. [root@tomcat1 bin]# [root@tomcat1 bin]# netstat -ntap | grep 8080 //查看端口 tcp6 0 0 :::8080 :::* LISTEN 19488/java [root@tomcat1 bin]# [root@tomcat1 bin]# systemctl stop firewalld.service //關(guān)閉防火墻 [root@tomcat1 bin]# setenforce 0 //關(guān)閉增強(qiáng)性安全功能 [root@tomcat1 bin]#
[root@tomcat1 bin]# mkdir -pv /web/webapp1 //創(chuàng)建站點(diǎn)目錄 mkdir: 已創(chuàng)建目錄 "/web" mkdir: 已創(chuàng)建目錄 "/web/webapp1" [root@tomcat1 bin]# vim /web/webapp1/index.jsp //添加首頁文件 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("Welcome to test site,http://www.test1.com");%> </body> </html>
[root@tomcat1 bin]# cd ../conf/ //切換目錄 [root@tomcat1 conf]# vim server.xml //修改配置文件 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> ##在Host標(biāo)簽里,添加下面內(nèi)容 <Context docBase="/web/webapp1" path="" reloadable="false"> </Context> [root@tomcat1 conf]# shutdown.sh //關(guān)閉服務(wù) Using CATALINA_BASE: /usr/local/tomcat9 Using CATALINA_HOME: /usr/local/tomcat9 Using CATALINA_TMPDIR: /usr/local/tomcat9/temp Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64 Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar [root@tomcat1 conf]# startup.sh //開啟服務(wù) Using CATALINA_BASE: /usr/local/tomcat9 Using CATALINA_HOME: /usr/local/tomcat9 Using CATALINA_TMPDIR: /usr/local/tomcat9/temp Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64 Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar Tomcat started. [root@tomcat1 conf]#
[root@tomcat2 ~]# mkdir /mnt/tools //創(chuàng)建掛載目錄 [root@tomcat2 ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ //掛載共享工具包 Password for root@//192.168.100.100/tools: [root@tomcat2 ~]# cd /mnt/tools/tomcat/ //切換到掛載目錄 [root@tomcat2 tomcat]# rpm -ivh jdk-8u201-linux-x64.rpm //直接安裝rpm包
[root@tomcat2 tomcat]# cd /usr/java/jdk1.8.0_201-amd64/ //切換目錄 [root@tomcat2 jdk1.8.0_201-amd64]# vim /etc/profile //修改系統(tǒng)環(huán)境變量文件 ##文件末尾添加環(huán)境變量 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@tomcat2 jdk1.8.0_201-amd64]# source /etc/profile //重新加載配置文件 [root@tomcat2 jdk1.8.0_201-amd64]# 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@tomcat2 jdk1.8.0_201-amd64]#
[root@tomcat2 jdk1.8.0_201-amd64]# cd /mnt/tools/tomcat/ //切換到掛載目錄 [root@tomcat2 tomcat]# tar zxf apache-tomcat-9.0.16.tar.gz -C /opt/ //解壓到“/opt/”目錄 [root@tomcat2 tomcat]# mv /opt/apache-tomcat-9.0.16/ /usr/local/tomcat9 //移動到“/usr/local/”目錄并重命名 [root@tomcat2 tomcat]# ln -s /usr/local/tomcat9/bin/startup.sh /usr/bin/ //建立啟動腳本軟鏈接 [root@tomcat2 tomcat]# ln -s /usr/local/tomcat9/bin/shutdown.sh /usr/bin/ //建立關(guān)閉腳本軟鏈接 [root@tomcat2 tomcat]# startup.sh //開啟服務(wù) Using CATALINA_BASE: /usr/local/tomcat9 Using CATALINA_HOME: /usr/local/tomcat9 Using CATALINA_TMPDIR: /usr/local/tomcat9/temp Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64 Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar Tomcat started. [root@tomcat2 tomcat]# netstat -ntap | grep 8080 //查看端口 tcp6 0 0 :::8080 :::* LISTEN 52941/java [root@tomcat2 tomcat]# [root@tomcat2 tomcat]# systemctl stop firewalld.service //關(guān)閉防火墻 [root@tomcat2 tomcat]# setenforce 0 //關(guān)閉增強(qiáng)性安全功能 [root@tomcat2 tomcat]#
[root@tomcat2 tomcat]# mkdir -pv /web/webapp1 //創(chuàng)建站點(diǎn)目錄 mkdir: 已創(chuàng)建目錄 "/web" mkdir: 已創(chuàng)建目錄 "/web/webapp1" [root@tomcat2 tomcat]# vim /web/webapp1/index.jsp //添加首頁文件 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("Welcome to test site,http://www.test2.com");%> </body> </html>
[root@tomcat2 tomcat]# cd /usr/local/tomcat9/conf/ //切換目錄 [root@tomcat2 conf]# vim server.xml //修改配置文件 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> ##在Host標(biāo)簽里,添加下面內(nèi)容 <Context docBase="/web/webapp1" path="" reloadable="false"> </Context> [root@tomcat2 conf]# shutdown.sh //關(guān)閉服務(wù) Using CATALINA_BASE: /usr/local/tomcat9 Using CATALINA_HOME: /usr/local/tomcat9 Using CATALINA_TMPDIR: /usr/local/tomcat9/temp Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64 Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar [root@tomcat2 conf]# startup.sh //開啟服務(wù) Using CATALINA_BASE: /usr/local/tomcat9 Using CATALINA_HOME: /usr/local/tomcat9 Using CATALINA_TMPDIR: /usr/local/tomcat9/temp Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64 Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar Tomcat started. [root@tomcat2 conf]#
[root@nginx ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ ............//省略過程 [root@nginx ~]#
[root@nginx ~]# mkdir /mnt/tools //創(chuàng)建掛載目錄 [root@nginx ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ //掛載共享目錄 Password for root@//192.168.100.100/tools: [root@nginx ~]# cd /mnt/tools/LNMP/ //切換到掛載目錄 [root@nginx LNMP]# ls Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2 [root@nginx LNMP]# tar zxf nginx-1.12.2.tar.gz -C /opt/ //解壓源碼包 [root@nginx LNMP]# cd /opt/ [root@nginx opt]# ls nginx-1.12.2 rh [root@nginx opt]#
[root@nginx opt]# cd nginx-1.12.2/ //切換目錄 [root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx //創(chuàng)建用戶nginx [root@nginx nginx-1.12.2]# ./configure \ //配置nginx服務(wù) > --prefix=/usr/local/nginx \ //安裝路徑 > --user=nginx \ //屬主 > --group=nginx \ //屬組 > --with-file-aio \ //啟用file aio支持(一種APL文件傳輸格式) > --with-http_stub_status_module \ //獲取nginx自上次啟動以來的工作狀態(tài) > --with-http_gzip_static_module \ //在線實(shí)時(shí)壓縮輸出數(shù)據(jù)流 > --with-http_flv_module \ //為Flash Video(FLV)文件 提供服務(wù)端偽流媒體支持 > --with-http_ssl_module //為HTTPS提供了必要的支持 [root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //為nginx命令建立軟鏈接,方便系統(tǒng)識別 [root@nginx nginx-1.12.2]# [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx //添加管理腳本 #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 [root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx //給腳本添加執(zhí)行權(quán)限 [root@nginx nginx-1.12.2]# chkconfig --add nginx //添加讓service工具能夠識別 [root@nginx nginx-1.12.2]# [root@nginx nginx-1.12.2]# service nginx start //開啟服務(wù) [root@nginx nginx-1.12.2]# service nginx stop //關(guān)閉服務(wù) [root@nginx nginx-1.12.2]#
[root@nginx nginx-1.12.2]# cd /usr/local/nginx/conf/ //切換目錄 [root@nginx conf]# ls fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default fastcgi_params koi-win nginx.conf scgi_params.default win-utf [root@nginx conf]# vim nginx.conf //修改配置文件 upstream tomcat-server { ##添加tomcat服務(wù)器 server 192.168.52.134:8080 weight=1; server 192.168.52.150:8080 weight=1; } location / { root html; index index.html index.htm; proxy_pass http://tomcat-server; ##添加代理 } [root@nginx 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@nginx conf]# [root@nginx conf]# service nginx start //開啟服務(wù) [root@nginx conf]# netstat -ntap | grep nginx //查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 41747/nginx: master [root@nginx conf]# systemctl stop firewalld.service 關(guān)閉防火墻 [root@nginx conf]# setenforce 0 //關(guān)閉增強(qiáng)性安全功能 [root@nginx conf]#
(生產(chǎn)環(huán)境下,兩個(gè)頁面需要相同,這是為了方便演示故意設(shè)置不同)
[root@nginx conf]# vim nginx.conf //修改配置文件 ##不用修改 upstream tomcat-server { server 192.168.52.134:8080 weight=1; server 192.168.52.150:8080 weight=1; } location / { root html; index index.html index.htm; # proxy_pass http://tomcat-server; ##注釋 } ##添加以下內(nèi)容 location ~.*.jsp$ { proxy_pass http://tomcat-server; proxy_set_header Host $host; } location ~.*.\.(gif|jpg|jpeg|png|bmp|swf|css)$ { root html; expires 30d; }
[root@nginx conf]# vim ../html/index.html <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> //支持中文字符集 <title>靜態(tài)頁面</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>靜態(tài)頁面</h2> <p>這是靜態(tài)頁面</p> </body> </html> [root@nginx conf]# service nginx stop [root@nginx conf]# service nginx start [root@nginx conf]#
[root@tomcat1 ~]# cd /usr/local/tomcat9/webapps/ //切換目錄 [root@tomcat1 webapps]# ls docs examples host-manager manager ROOT [root@tomcat1 webapps]# mkdir test //創(chuàng)建站點(diǎn)目錄 [root@tomcat1 webapps]# cd test/ [root@tomcat1 test]# vim index.jsp //添加首頁文件 <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4. 01 Transitional//EN" "http://www.w3.org/TR/ html4/loose. dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>動態(tài)頁面1</title> </head> <body> <div>動態(tài)頁面1</div> </body> </html>
[root@tomcat2 ~]# cd /usr/local/tomcat9/webapps/ //切換目錄 [root@tomcat2 webapps]# ls docs examples host-manager manager ROOT [root@tomcat2 webapps]# mkdir test //創(chuàng)建站點(diǎn)目錄 [root@tomcat2 webapps]# cd test/ [root@tomcat2 test]# vim /index.jsp //添加首頁文件 <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4. 01 Transitional//EN" "http://www.w3.org/TR/ html4/loose. dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>動態(tài)頁面2</title> </head> <body> <div>動態(tài)頁面2</div> </body> </html>
[root@tomcat1 test]# vim index.jsp <div>動態(tài)頁面1</div><br><img src="picture.jpg"> //只需修改此行 [root@tomcat2 test]# vim index.jsp <div>動態(tài)頁面2</div><br><img src="picture.jpg"> //只需修改此行
[root@nginx conf]# cd /mnt/tools/ //切換到掛載點(diǎn) [root@nginx tools]# mkdir -p /usr/local/nginx/html/test //創(chuàng)建站點(diǎn)目錄test,注意必須與Tomcat站點(diǎn)相同 [root@nginx tools]# cp picture.jpg /usr/local/nginx/html/test/ //復(fù)制圖片到站點(diǎn) [root@nginx tools]#
上述就是小編為大家分享的Nginx+Tomcat怎么配置負(fù)載均衡和動靜分離了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。