1.动静分离是将网站的静态资源与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用的访问
2.静态资源部署在Nginx,将静态资源部署在Nginx上,后台项目部署到应用服务器上,根据一定规则静态资源的请求,全部去请求nginx服务器,达到动静分离的目标
优点:API接口服务化;前后端开发并行;减轻后端服务器的压力,提高静态资源访问速度
缺点:不利于网站SEO(搜索引擎优化);开发量变大;在业务高速发展时需要慎重考虑
1.由于Tomcat本身处理静态效率不高,还会带来资源消耗,因此使用动静分离,将静态请求交由Nginx处理,动态请求交由Tomcat处理
2.Nginx根据客户端请求的url来判断请求的是否是静态资源,如果请求的url包含jpg、png,则由Nginx处理;如果请求的url是.php或者.jsp等,则被认为是动态的,将转发tomcat处理。即Nginx通过url来区分请求的类型,并且转发给不同的服务端
一台nginx服务器,一台tomcat服务器,一台测试机
yum install pcre-devel zlib-devel gcc gcc-c++ make -y
tar xzvf nginx-1.12.2.tar.gz -C /opt
useradd -M -s /sbin/nologin nginx
cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: -99 20
#description: Nginx Service Control Script
#nginx主程序
PROG="/usr/local/nginx/sbin/nginx"
#nginx的PID号
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
chmod +x /etc/init.d/nginx
chkconfig --add nginx
[root@localhost nginx-1.12.2]# systemctl stop firewalld
[root@localhost nginx-1.12.2]# setenforce 0
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# netstat -natp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6538/nginx: master
[root@localhost ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /opt
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
jdk1.8.0_91 rh
[root@localhost opt]# mv jdk1.8.0_91/ /usr/local/
[root@localhost local]# vim /etc/profile
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
source /etc/profile
tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-8.5.16 bin etc games include jdk1.8.0_91 lib lib64 libexec sbin share src
[root@localhost local]# mv apache-tomcat-8.5.16/ tomcat
ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@localhost local]# systemctl stop firewalld
[root@localhost local]# setenforce 0
[root@localhost local]# 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@localhost local]# netstat -natp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 38090/java
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
vim /usr/local/nginx/html/index.html
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost tomcat]# cd /usr/local/tomcat/webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# ls
docs examples host-manager manager ROOT test
[root@localhost ~]# vim /usr/local/tomcat/webapps/test/index.jsp
vim /usr/local/nginx/conf/nginx.conf
[root@localhost html]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# mkdir test
[root@localhost html]# ls
50x.html index.html test
[root@localhost html]# cd test/
[root@localhost test]# ls
[root@localhost test]# rz
[root@localhost test]# ls
123.jpg
[root@localhost test]# service nginx stop
[root@localhost test]# service nginx start