Tomcat(二):Nginx反向代理实现Tomcat动静分离

  • 来源:新网
  • 更新日期:2018-03-20

摘要:Tomcat(二):Nginx反向代理实现Tomcat动静分离 实验拓扑: 实验环境: OS:CentOS-6.7-x86_64 Nginx: IP:192.168.1.4 Tomcat: IP:192.168.1.5

Tomcat(二):Nginx反向代理实现Tomcat动静分离 实验拓扑:

118.jpg

实验环境:

OS:CentOS-6.7-x86_64

Nginx:

IP:192.168.1.4

Tomcat:

IP:192.168.1.5 实验流程:

Tomcat:

先安装Jvm:openjdk:

[root@node1 ~]# yum install java-1.7.0-openjdk [root@node1 ~]# java -version java version "1.7.0_79" OpenJDK Runtime Environment (rhel-2.5.5.4.el6-x86_64 u79-b14) OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

安装tomcat:

官网:http://tomcat.apache.org

[root@node1 ~]# tar xf apache-tomcat-7.0.65.tar.gz -C /usr/local/ [root@node1 ~]# cd /usr/local/ [root@node1 local]# ln -sv apache-tomcat-7.0.65 tomcat "tomcat" -> "apache-tomcat-7.0.65" [root@node1 ~]# vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH [root@node1 ~]# source /etc/profile.d/tomcat.sh

提供脚本:

[root@node1 ~]# cat /etc/rc.d/init.d/tomcat #!/bin/sh # Tomcat init script for Linux. # # chkconfig: 2345 96 14 # description: The Apache Tomcat servlet/JSP container. # JAVA_OPTS=\'-Xms64m -Xmx128m\' JAVA_HOME=/usr/lib/jvm/jre CATALINA_HOME=/usr/local/tomcat export JAVA_HOME CATALINA_HOME case $1 in start) exec $CATALINA_HOME/bin/catalina.sh start ;; stop) exec $CATALINA_HOME/bin/catalina.sh stop;; restart) $CATALINA_HOME/bin/catalina.sh stop sleep 2 exec $CATALINA_HOME/bin/catalina.sh start ;; *) echo "Usage: `basename $0` {start|stop|restart}" exit 1 ;; esac [root@node1 ~]# chmod +x /etc/rc.d/init.d/tomcat [root@node1 ~]# chkconfig tomcat --add

定义管理页面用户名及密码:

[root@node1 ~]# cd /usr/local/tomcat/conf [root@node1 conf]# vim tomcat-users.xml <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/> [root@node1 conf]# service tomcat start [root@node1 conf]# chkconfig tomcat on

访问测试:

输入图片说明

部署站点文件:

[root@node1 ~]# mkdir -p /tomcat/webapps [root@node1 ~]# unzip JspRun!_6.0.0_GBK.zip [root@node1 ~]# mv upload /tomcat/webapps/jsprun [root@node1 ~]# cd /tomcat/webapps [root@node1 webapps]# chmod -R 777 jsprun [root@node1 webapps]# cd jsprun [root@node1 jsprun]# vim config.properties dbhost = 192.168.1.5 dbport=3306 dbuser = jruser dbpw = jrpass dbname = jrdb

定义虚拟主机,并重启tomcat:

[root@node0 ~]# cd /usr/local/tomcat [root@node0 tomcat]# vim conf/server.xml <Engine name="Catalina" defaultHost="node1.chencer.org"> <Host name="node1.chencer.org" appBase="/tomcat/webapps" unpackWARs="true" autoDeploy="true"> <Context path="/bbs" docBase="jsprun" /> </Host> [root@node1 ~]# service tomcat restart

安装并启动mysql:

[root@node0 mysql-5.5-rpm]# yum install MySQL-shared-compat-5.5.48-1.el6.x86_64.rpm MySQL-client-5.5.48-1.el6.x86_64.rpm MySQL-server-5.5.48-1.el6.x86_64.rpm [root@node0 ~]# service mysql start [root@node0 ~]# chkconfig mysql on

创建数据库,并赋予权限:

[root@node0 ~]# mysql mysql> create database jrdb; Query OK, 1 row affected (0.00 sec) mysql> grant all on jrdb.* to \'jruser\'@\'192.168.1.5\' identified by \'jrpass\'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)

安装站点,并访问测试:

Nginx:

安装并配置Nginx:

[root@node0 ~]# yum install nginx

动态页面重定向至tomcat:

[root@node0 ~]# cd /etc/nginx/conf.d/ [root@node0 conf.d]# vim default.conf server { listen 80; server_name node0.chencer.org; index index.jsp; location ~* .(jsp|do)$ { proxy_pass http://192.168.1.5:8080; } } [root@node0 conf.d]# service nginx start [root@node0 conf.d]# chkconfig nginx on

访问测试:

部署站点静态文件:

[root@node0 ~]# mkdir -p /web/jsprun [root@node0 ~]# scp -r node1:/tomcat/webapps/jsprun/* /web/jsprun/

编辑配置文件:

[root@node0 conf.d]# vim default.conf server { listen 8080; server_name 192.168.1.4; index index.jsp; location / { root /web/jsprun; index index.html; } } server { listen 80; server_name node0.chencer.org; index index.jsp; location / { proxy_pass http://192.168.1.5:8080; } location ~.(html|htm|xml|gif|jpg|jpeg|ico|swf)$ { proxy_pass http://192.168.1.4:8080; } location ~* .(jsp|do)$ { proxy_pass http://192.168.1.5:8080; } } [root@node0 ~]# service nginx restart