使用nginx搭建实现tomcat动静分离

环境:

两台centos7服务器:

nginx 192.168.247.206

tomcat 192.168.247.160

[root@lamp ~]# hostnamectl set-hostname nginx
[root@lamp ~]# su
[root@nginx ~]# 
[root@nginx ~]# hostnamectl set-hostname tomcat
[root@nginx ~]# su
[root@tomcat ~]# 

实验步骤:

1.先配置nginx

1.1 关闭防火墙

[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# setenforce 0
1.2 安装编译包,创建系统用户nginx,手动编译安装nginx
[root@nginx ~]# yum install gcc gcc-c++ make pcre-devel zlib-devel -y
[root@nginx ~]# useradd -M -s /sbin/nologin nginx
[root@nginx ~]# mkdir /abc
[root@nginx ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@nginx ~]# cd /abc
[root@nginx abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx abc]# cd /opt
[root@nginx opt]# cd nginx-1.12.0/
[root@nginx nginx-1.12.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx nginx-1.12.0]# make && make install

1.3 优化nginx

[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.0]# 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 nginx-1.12.0]# cd /etc/init.d/
[root@nginx init.d]# ls
functions  jexec  netconsole  network  README
[root@nginx init.d]# vim nginx

1.4 编写启动脚本

#!/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

1.5 开启服务

[root@nginx init.d]# service nginx start
[root@nginx init.d]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      34990/nginx: master 
2.此时nginx端完成,接下来做tomcat端

2.1 解压tomcat包

[root@tomcat ~]# systemctl stop firewalld
[root@tomcat ~]# setenforce 0
[root@tomcat ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@tomcat ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@tomcat ~]# cd /abc
[root@tomcat abc]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /opt
[root@tomcat abc]# cd /opt
[root@tomcat opt]# ls
data  jdk1.8.0_91  nginx-1.12.2  rh
[root@tomcat opt]# cd jdk1.8.0_91/
[root@tomcat jdk1.8.0_91]# cd ..
[root@tomcat opt]# mv jdk1.8.0_91/ /usr/local/
[root@tomcat opt]# cd /usr/local/
[root@tomcat local]# ls
bin  etc  games  include  jdk1.8.0_91  lib  lib64  libexec  nginx  sbin  share  src
[root@tomcat local]# ls jdk1.8.0_91/
bin        include         lib      README.html  THIRDPARTYLICENSEREADME-JAVAFX.txt
COPYRIGHT  javafx-src.zip  LICENSE  release      THIRDPARTYLICENSEREADME.txt
db         jre             man      src.zip
[root@tomcat local]# vim /etc/profile

2.2在末尾加入下面环境变量,指定家目录,虚拟机家目录,函数库

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

2.3 优化tomcat

[root@tomcat local]# source /etc/profile
[root@tomcat local]# cd /abc
[root@tomcat abc]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local
[root@tomcat abc]# cd /usr/local
[root@tomcat local]# ls
apache-tomcat-8.5.16  etc    include      lib    libexec  sbin   src
bin                   games  jdk1.8.0_91  lib64  nginx    share
[root@tomcat local]# mv apache-tomcat-8.5.16/ tomcat
[root@tomcat local]# ls
bin  etc  games  include  jdk1.8.0_91  lib  lib64  libexec  nginx  sbin  share  src  tomcat
[root@tomcat local]# cd bin/
[root@tomcat bin]# cd ..
[root@tomcat local]# cd /usr/local/tomcat/bin/
[root@tomcat bin]# ls
bootstrap.jar                 configtest.bat    setclasspath.sh  tomcat-native.tar.gz
catalina.bat                  configtest.sh     shutdown.bat     tool-wrapper.bat
catalina.sh                   daemon.sh         shutdown.sh      tool-wrapper.sh
catalina-tasks.xml            digest.bat        startup.bat      version.bat
commons-daemon.jar            digest.sh         startup.sh       version.sh
commons-daemon-native.tar.gz  setclasspath.bat  tomcat-juli.jar
[root@tomcat bin]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@tomcat bin]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
[root@tomcat bin]# startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat bin]# netstat -natp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      46578/java 

2.4 先验证一下

在这里插入图片描述
在这里插入图片描述

3.接下来就开始做读写分离的操作
[root@nginx init.d]# cd /usr/local/nginx/
[root@nginx nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@nginx nginx]# vim conf/nginx.conf

加入在server{}中加入下面内容

 location ~.*.jsp$ {
          proxy_pass http://192.168.247.160:8080;
          proxy_set_header Host $host;
        }

创建静态页面

[root@nginx nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@nginx nginx]# cd html/
[root@nginx html]# vim index.html 
<!DOCTYPE html>
<html>
<head>
<title>静态页面</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }   
</style>
</head>
<body>
<h1>静态网站</h1>
<p><em>这是一个静态页面</em></p>
</body>
</html>
[root@nginx html]# service nginx stop
[root@nginx html]# service nginx start

在这里插入图片描述

静态页面完成,接下来简单做一个动态页面

[root@tomcat bin]# cd /usr/local/tomcat/
[root@tomcat tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@tomcat webapps]# mkdir test
[root@tomcat webapps]# ls
docs  examples  host-manager  manager  ROOT  test
[root@tomcat webapps]# cd test/
[root@tomcat 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>动态页面</title>
</head>
<body>
<div>动态页面</div>
</body>
</html>

访问验证

在这里插入图片描述

此时在nginx后面跟上/test/index.jsp

在这里插入图片描述
在这里插入图片描述

会访问到tomcat

此时便初步实现了动静分离

[root@tomcat test]# vim index.jsp
<div>动态页面</div><br>
<img src="qq.jpg"/>
5.引用一张图片,图片按理来说会放到index.jsp的同级别目录test下,但是其实可以是放在nginx下

接下来修改nginx的配置文件

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf

在proxy前面加入

        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
        root html;
        expires 30d;
        }

加入图片,加入时要注意图片的目录名称要和java项目名称一致,webapps是项目,test是项目名称

[root@tomcat test]# pwd
/usr/local/tomcat/webapps/test
[root@nginx html]# ls
50x.html  index.html
[root@nginx html]# mkdir test
[root@nginx html]# cd test/
[root@nginx test]# cp /abc/qq.jpg /usr/local/nginx/html/test/
[root@nginx test]# ls
qq.jpg
[root@nginx test]# service nginx stop
[root@nginx test]# service nginx start

在这里插入图片描述

ex.html
[root@nginx html]# mkdir test
[root@nginx html]# cd test/
[root@nginx test]# cp /abc/qq.jpg /usr/local/nginx/html/test/
[root@nginx test]# ls
qq.jpg
[root@nginx test]# service nginx stop
[root@nginx test]# service nginx start


[外链图片转存中...(img-MJTASDDU-1581050225246)]



































































发布了93 篇原创文章 · 获赞 26 · 访问量 7915

猜你喜欢

转载自blog.csdn.net/Lfwthotpt/article/details/104208485