溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Tomcat+nginx群集和動靜分離

發(fā)布時間:2020-10-24 23:40:21 來源:網(wǎng)絡(luò) 閱讀:156 作者:23trl 欄目:云計算

實驗環(huán)境

Nginx服務(wù)器(192.168.13.177)
Tomcat1服務(wù)器(192.168.13.151)
Tomcat2服務(wù)器 (192.168.13.178)
client測試機(jī)

一,負(fù)載均衡

1,在Tomcat1,Tomcat2上安裝Tomcat服務(wù)

[root@tomcat1 ~]# systemctl stop firewalld.service  ##關(guān)閉防火墻
[root@tomcat1 ~]# mkdir /abc
[root@tomcat1 ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/
[root@tomcat1 ~]# cd /abc/tomcat/
[root@tomcat1 tomcat]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/  ##解壓JDK
[root@tomcat1 tomcat]# vim /etc/profile   ##配置環(huán)境變量
##大G末行加入
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat1 tomcat]# source /etc/profile  ##刷新配置文件
[root@tomcat1 tomcat]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/   ##解壓
[root@tomcat1 tomcat]# cd /usr/local/
[root@tomcat1 local]# mv apache-tomcat-8.5.16/ tomcat
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/  

將啟動和關(guān)閉腳本便于系統(tǒng)識別

[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tomcat1 local]# mkdir -p /web/webapp1  ##創(chuàng)建站點(diǎn)
[root@tomcat1 local]# vim /web/webapp1/index.jsp   ##寫入jsp網(wǎng)頁內(nèi)容
##jsp網(wǎng)頁內(nèi)容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <% out.println("Welcome KGC Web");%>    ##輸出信息
 </body>
</html>
[root@tomcat1 local]# vim /usr/local/tomcat/conf/server.xml  ##修改Tomcat配置文件
            <Host name="localhost"  appBase="webapps"
                        unpackWARs="true" autoDeploy="true">
                <Context docBase="/web/webapp1" path="" reloadable="false">  ##添加站點(diǎn)信息
                </Context>
[root@tomcat1 ~]# startup.sh   ##啟動服務(wù)
## Tomcat上網(wǎng)頁內(nèi)容為accp其他配置一樣

2,在Nginx服務(wù)器上安裝Nginx

[root@nginx ~]# systemctl stop firewalld.service    ##關(guān)閉防火墻
[root@nginx ~]# setenforce 0
[root@nginx ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y  ##安裝環(huán)境必要組件
[root@nginx ~]# mkdir /abc
[root@nginx ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/   ##掛載
Password for root@//192.168.100.3/LNMP-C7:  
[root@nginx ~]# cd /abc/
[root@nginx abc]# tar zxvf nginx-1.12.2.tar.gz -C /usr/local/  ##解壓
[root@nginx abc]# useradd -M -s /sbin/nologin nginx   ##創(chuàng)建系統(tǒng)用戶
[root@nginx abc]# cd /usr/local/nginx-1.12.2/
[root@nginx nginx-1.12.2]# ./configure \   ##配置
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-http_flv_module 
[root@nginx nginx-1.12.2]# make && make install   ##編譯安裝 

3,修改Nginx配置文件

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
 #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        upstream tomcat-server {                             #添加地址池
                            server 192.168.13.151:8080 weight=1;
                            server 192.168.13.178:8080 weight=1;    
                        }

        server {
                listen       80;
.....省略
location / {
                        root   html;
                        index  index.html index.htm;
                        proxy_pass http://tomcat-server;         #添加代理,調(diào)用服務(wù)器地址池
                }
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  ##便于系統(tǒng)識別
[root@nginx nginx-1.12.2]# nginx   ##開啟服務(wù)

Tomcat+nginx群集和動靜分離
Tomcat+nginx群集和動靜分離

二,動靜分離

1,在Nginx上修改配置文件

[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx  ##編寫service啟動腳本
#!/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 
[root@nginx nginx-1.12.2]# chkconfig --add nginx
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
server {
...省略...
     location ~.*.jsp$ {       ##匹配jsp頁面跳轉(zhuǎn)代理服務(wù)器池
         proxy_pass http://tomcat-server;
         proxy_set_header Host $host;
     }
            location / {
        root   html/test;   ##修改站點(diǎn)
        index  index.html index.htm;
        proxy_pass http://tomcat-server;
     }
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/html/index.html   ##編寫靜態(tài)網(wǎng)頁
<!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 nginx-1.12.2]# service nginx stop    ##關(guān)閉開啟服務(wù)
[root@nginx nginx-1.12.2]# service nginx start

2,在Tomcat1,Tomcat2上創(chuàng)建jsp動態(tài)頁面

[root@tomcat1 ~]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/test/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)頁面</title>
</head>
<body>
<div>動態(tài)頁面1</div>
</body>
</html>

在Tomcat2上修改為動態(tài)頁面22

//訪問靜態(tài)
http://192.168.13.177/  
//訪問動態(tài)  
http://192.168.13.177/test/index.jsp

Nginx處理靜態(tài)圖片,Tomcat處理動態(tài)頁面
1,在Tomcat1,Tomcat2上頁面添加圖片

[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/test/index.jsp
<body>
    <div>動態(tài)頁面</div><br><img src="11.jpg"> //添加頁面圖片 
</body>
[root@tomcat01 local]# vim /usr/local/tomcat/conf/server.xml 
#在第149行下追加以下條目,
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="false">
</Context>
[root@tomcat1 test]# shutdown.sh    ##關(guān)閉重啟
[root@tomcat1 test]# startup.sh

2,在Nginx上修改配置文件

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { 
     root html/test;
     expires 30d;
}
[root@nginx nginx-1.12.2]# mkdir /usr/local/nginx/html/test
[root@nginx nginx-1.12.2]# cp /abc/11.jpg /usr/local/nginx/html/test/
#重啟服務(wù)

3,使用client測試

[root@nginx html]# service nginx restart
Tomcat+nginx群集和動靜分離
Tomcat+nginx群集和動靜分離Tomcat+nginx群集和動靜分離

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI