1、工作原理
Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat、Resin、apache 处理动态页面。
Resin [ˈrezɪn]是一个非常流行的支持servlets 和jsp的引擎,速度非常快。Resin本身包含了一个支持HTTP/1.1的WEB服务器。虽然它可以显示动态内容,但是它显示静态内容的能力也非常强...
软件类型:应用服务器 |授权协议:GPLv2|开发语言:Java、C/C++
Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。
动静分离从目前实现角度来讲大致分为两种:
一种是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过nginx来分开。这样也是本次课程要讲解的,具体怎么来实现呢?
如下图所示:
说明:
1、 代理服务器和静态服务器即为一台服务器上,这里只是为了明显区分动静分离所处服务器的不同
2、静态服务器中,存放的资源主要是源代码文件、图片、属性、样式以及其它所有非动态的资源文件;
3、调度规则,即为代理服务器,这里是Nginx的服务器调度规则;
4、动态服务器,其种类比较繁多,可以是Apache、Tomcat、IIS以及其它Web服务器,它们一般分别隶属于一台服务器;
实现说明
如上图所示,当客户端访问代理服务器时:
首先,加载和显示存放在静态服务器中的静态资源,这里以html为例;
其次,如果上一步没有匹配对应的资源,我们就认为是动态访问请求,那么就直接访问参与负载均衡的服务器列表中的某一台服务器的动态操作;
最后,Nginx作为Web服务器加载静态资源(html、css、js、image),而静态资源如果需要动态获取数据,并填充到页面显示,会自动去往负载服务器获取并返回,在实现了动态分离的同时,也参与了服务器的负载均衡。
部署配置Nginx+apache+tomcat动静分离
1、拓扑结构图:
2、服务器配置表
| 服务器名 | 安装软件 | IP 地址 |
|---|---|---|
| nginxserver | nginx | 192.168.10.1 |
| LAMPserver | httpd php mariadb-sever | 192.168.10.2 |
| tomcatserver | tomcat | 192.168.10.3 |
| fileserver | NFS | 192.168.10.4 |
3、安装配置nginx server服务器
3-1、配置ip地址:192.168.10.1
3-2、安装nginx服务
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar -zxf nginx-1.12.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin
[root@localhost nginx-1.12.0]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
#chkconfig: 345 85 21
case $1 in
start)
/usr/local/sbin/nginx
;;
stop)
killall -9 nginx
rm -f /var/run/nginx.pid
;;
restart)
$0 stop
$0 start
;;
)
echo "start|stop|restart"
;;
esac
[root@localhost nginx-1.12.0]# chmod a+x /etc/rc.d/init.d/nginx
[root@localhost nginx-1.12.0]# chkconfig --add nginx
[root@localhost nginx-1.12.0]# chkconfig nginx on
[root@localhost nginx-1.12.0]# systemctl start nginx
[root@localhost nginx-1.12.0]# systemctl status nginx
3-3、配置nginx
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
修改:在 server{ }选项中添加两组location选项
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#
location ~ \.php$ {
proxy_pass http://192.168.10.2; #PHP服务器IP地址
}
location ~ \.jsp$ {
proxy_pass http://192.168.10.3:8080; #Tomcat服务器IP地址
}
3-4、重启服务
[root@localhost nginx-1.12.0]# killall -9 nginx
[root@localhost nginx-1.12.0]# nginx
4、安装LAMP server服务器
4-1、配置IP地址:192.168.10.2
4-2、安装服务
[root@localhost ~]# yum -y install httpd php mariadb mariadb-server mariadb-devel**
4-3、配置mariadb服务
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# mysqladmin -uroot password 123.com
[root@localhost ~]# mysql -uroot -p123.com
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.10.%' identified by '123.com';
MariaDB [(none)]> exit
4-4、配置PHP服务
[root@localhost ~]# vim /etc/php.ini
修改:
default_charset = "UTF-8"
short_open_tag = On
4-5、配置httpd服务
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
修改:在以下容器中添加内容
<IfModule dir_module> 行163
DirectoryIndex index.php index.html #添加index.php
</IfModule>
<IfModule mime_module> 行285
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php #添加此行
</IfModule>
[root@localhost ~]# systemctl restart httpd
5、安装配置tomcat
5-1、安装Java
[root@localhost ~]#tar zxf jdk-8u91-linux-x64.tar.gz -C /usr/src/
[root@localhost ~]#cd /usr/src/
[root@localhost ~]# mv jdk1.8.0_91/ /usr/local/java
[root@localhost ~]#rm -f /usr/bin/java
[root@localhost ~]# ln -s /usr/local/java/bin/ /usr/bin/ -f*
[root@localhost ~]#vim /etc/profile
添加:
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
#注意这两行一定不能写错
[root@localhost ~]# source /etc/profile
[root@localhost ~]# . /etc/profile
[root@localhost ~]# java -version
javaversion "1.8.0_91"
Java(TM)SE Runtime Environment (build 1.8.0_91-b14)
JavaHotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
出现上图说明JAVA升级成功
5-2、配置IP地址:192.168.6.30
解压tomcat
[root@localhost ~]# tar zxf apache-tomcat-8.5.82.tar.gz -C /usr/src/
[root@localhost ~]# cp -r /usr/src/apache-tomcat-8.5.82/ /usr/local/tomcat
5-3、添加服务
[root@localhost ~]# ln -s /usr/local/tomcat/bin/* /usr/local/bin
5-4、配置tomcat
[root@localhost ~]# mkdir /myweb
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
在上面添加:
<Context docBase="/myweb" path="" reloadable="false"></Context>
[root@localhost ~]# shutdown.sh
[root@localhost ~]# startup.sh
6、访问测试
6-1、在nginx server服务器上创建html测试页
[root@localhost nginx-1.12.0]# echo "cao ni ma !!!" >> /usr/local/nginx/html/index.html
原本有页面,可以不改
6-2、在 LAMPserver 服务器上创建php测试页
[root@localhost ~]# vim /var/www/html/index.php
添加:
<?php
$link=mysqli_connect('192.168.10.2','root','123.com');
if($link) echo "恭喜你,数据库连接成功啦!!";
?>
[root@localhost ~]# systemctl restart mariadb
6-3、 在tomcat server服务器上创建jsp脚本测试页
[root@localhost ~]# echo"da sha bi!!!" >> /myweb/index.jsp
[root@localhost ~]# shutdown.sh
[root@localhost ~]# startup.sh
测试:启动客户端192.168.6.50
访问测试静态页面测试:
php页面测试:
jsp 脚本测试: