主機(jī) | 操作系統(tǒng) | IP地址 | 所需軟件 |
---|---|---|---|
Nginx Server | CentOS7 | 192.168.109.7 | nginx-1.12.0.tar.gz |
Tomcat Server1 | CentOS7 | 192.168.109.22 | apache-tomcat-9.0.16.tar.gz、jdk-8u201-linux-x64.rpm |
Tomcat Server2 | CentOS7 | 192.168.109.23 | apache-tomcat-9.0.16.tar.gz、jdk-8u201-linux-x64.rpm |
#!/bin/bash #腳本說明:編譯安裝nginx服務(wù) #注意:使用前請將nginx-1.12.0.tar.gz放入/opt目錄下 #關(guān)閉防火墻 systemctl stop firewalld systemctl disable firewalld setenforce 0 #安裝所需開發(fā)包和編譯環(huán)境、編譯器 yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make #創(chuàng)建程序用戶,便于準(zhǔn)確控制訪問 useradd -M -s /sbin/nologin nginx #解壓安裝包 cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/ #編譯安裝nginx;指定安裝路徑、指定用戶名、組名、啟用模塊以支持統(tǒng)計(jì)狀態(tài) cd /opt/nginx-1.12.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 make && make install #軟鏈接便于系統(tǒng)識別nginx操作命令 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #添加nginx系統(tǒng)服務(wù) echo '[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' > /lib/systemd/system/nginx.service #賦權(quán)及開啟服務(wù)、開啟開機(jī)自啟 chmod 754 /lib/systemd/system/nginx.service systemctl restart nginx.service systemctl enable nginx.service
2、部署兩臺 Tomcat 應(yīng)用服務(wù)器
#!/bin/bash #安裝Tomcat服務(wù) #關(guān)閉防火墻 systemctl stop firewalld systemctl disable firewalld setenforce 0 #安裝JDK cd /opt rpm -ivh jdk-8u201-linux-x64.rpm #設(shè)置JDK環(huán)境變量 echo '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' > /etc/profile.d/java.sh source /etc/profile #安裝并啟動Tomcat cd /opt tar zxvf apache-tomcat-9.0.16.tar.gz mv apache-tomcat-9.0.16 /usr/local/tomcat /usr/local/tomcat/bin/startup.sh
3、動靜分離配置
請看tomcat
1)Tomcat1 server 配置
mkdir /usr/local/tomcat/webapps/test #配置動態(tài)頁面 vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("動態(tài)頁面 1:www.test1.com");%> </body> </html>
#修改配置文件 vim /usr/local/tomcat/conf/server.xml <Host name="192" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh
2)Tomcat2 server 配置
mkdir /usr/local/tomcat/webapps/test vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test2 page</title> </head> <body> <% out.println("動態(tài)頁面 1:www.test2.com");%> </body> </html>
#修改配置文件 vim /usr/local/tomcat/conf/server.xml <Host name="192" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh
3)Nginx server 配置
#準(zhǔn)備靜態(tài)頁面和靜態(tài)圖片 echo '<html><body><h1>this is static</h1></body></html>' > /usr/local/nginx/html/index.html mkdir /usr/local/nginx/html/long cd /usr/local/nginx/html/long
vim /usr/local/nginx/conf/nginx.conf ...... http { ...... #gzip on; upstream tomcat_server { server 192.168.109.22:8080 weight=1; server 192.168.109.23:8080 weight=1; } server { listen 80; server_name www.long.com; #charset koi8-r; #access_log logs/host.access.log main; #配置Nginx處理動態(tài)頁面請求,將 .jsp 文件請求轉(zhuǎn)發(fā)到Tomcat 服務(wù)器處理 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; } #配置Nginx處理靜態(tài)圖片請求 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { root /usr/local/nginx/html/img/; expires 10d; } location / { root html; index index.html index.htm; } systemctl restart nginx.service
4、訪問測試
測試靜態(tài)頁面效果
瀏覽器訪問 http://192.168.109.7/
瀏覽器訪問 http://192.168.109.7/long.jpg
測試負(fù)載均衡效果,不斷刷新瀏覽器測試
瀏覽器訪問 http://192.168.109.7/test/index.jsp
到此這篇關(guān)于Nginx+Tomcat實(shí)現(xiàn)負(fù)載均衡、動靜分離的原理解析的文章就介紹到這了,更多相關(guān)Nginx負(fù)載均衡、動靜分離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:拉薩 汕頭 鄭州 玉林 開封 石嘴山 晉城 三明
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Nginx+Tomcat實(shí)現(xiàn)負(fù)載均衡、動靜分離的原理解析》,本文關(guān)鍵詞 Nginx+Tomcat,實(shí)現(xiàn),負(fù)載,均衡,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。