《Nginx系列》之青铜入门篇 反向代理 负载均衡 动静分离就是这么简单

1.什么是Nginx?

Nginx是一个高性能的自由、开源的HTTP和反向代理服务器,特色是***占用内存少***,并发性能强css

nginx能干吗?

  • 能够做为IMAP、POP三、SMTP的代理服务器;
  • 能够做为HTTP服务器进行网站的发布处理;
  • 能够做为反向代理进行负载均衡的实现;

2.Nginx的安装

2.1 将Nginx相关安装包上传到服务器上

[root@localhost nginx-1.12]# ls -l
总用量 2956
-rw-r--r--. 1 root root  981687 12月 21 16:09 nginx-1.12.2.tar.gz
-rw-r--r--. 1 root root 2041593 12月 21 16:09 pcre-8.37.tar.gz
复制代码

nginx-1.12.2.tar.gz:nginx源码包,用于安装Nginxhtml

pcre-8.37.tar.gz:Perl库, 是一个用C语言编写的正则表达式函数库 。java

2.2 安装pcre

  • 解压pcre源码安装包
[root@localhost nginx-1.12]# tar zxf pcre-8.37.tar.gz
复制代码
  • 编译安装pcre
[root@localhost pcre-8.37]# ./configure 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking for style of include used by make... GNU
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/nginx-1.12/pcre-8.37': configure: error: no acceptable C compiler found in $PATH See `config.log' for more details
复制代码

若是出现上述错误,表示咱们没有C的编译器,须要安装gcc/gcc-c++(gcc是C的编译器,gcc-c++是C++的编译器)linux

  • 安装gcc和gcc-c++
root@localhost pcre-8.37]# yum install gcc gcc-c++ -y
复制代码
  • 从新编译pcre
[root@localhost pcre-8.37]# ./configure 
[root@localhost pcre-8.37]# make && make install
复制代码

./configure:会检查当前系统的相关配置,也能够经过参数指定相关配置参数nginx

make:编译c++

make install:安装web

若是以上操做没有报错,表示编译安装完成正则表达式

  • 测试pcre是否安装成功
#执行pcre-config --version可用回显版本号,则表示pcre安装成功
[root@localhost pcre-8.37]# pcre-config --version
8.37
复制代码

2.3 安装其余组件

[root@localhost pcre-8.37]# yum install -y make zlib zlib-devel libtool openssl openssl-develb
复制代码

2.4 安装Nginx

  • 解压Nginx源码安装包
[root@localhost nginx-1.12]# tar zxf nginx-1.12.2.tar.gz
复制代码
  • 编译安装Nginx
[root@localhost nginx-1.12]# ls
apache-tomcat-7.0.70.tar.gz  nginx-1.12.2  nginx-1.12.2.tar.gz  pcre-8.37  pcre-8.37.tar.gz
[root@localhost nginx-1.12]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.12.2]# ./configure
[root@localhost nginx-1.12.2]# make && make install
复制代码

./configure:会检查当前系统的相关配置,也能够经过参数指定相关配置参数算法

make:编译数据库

make install:安装

2.5 启动Nginx,测试Nginx是否安装成功

  • 启动Nginx
#/usr/local/nginx:nginx源码安装的默认路径
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/sbin/
#nginx:nginx的二进制文件,用于启动、中止服务、从新加载配置文件等
[root@localhost sbin]# ./nginx
复制代码

经过ps -aef | grep nginxf发现相关进程已存在

[root@localhost sbin]# ps -aef | grep nginx
root      24981      1  0 17:16 ?        00:00:00 nginx: master process ./nginx
nobody    24982  24981  0 17:16 ?        00:00:00 nginx: worker process
root      24985   9621  0 17:18 pts/1    00:00:00 grep --color=auto nginx
复制代码

经过netstat -tualnp发现nginx正在监听80端口

Qv3Cxx.png

访问nginx服务器80端口,测试Nginx是否能够正常访问

Qv3owD.png

若是出现上述状况,则是由于防火墙过滤引发的,此时能够经过增长80端口的规则列表,或者关闭防火墙便可解决。

firewall-cmd:查看防火墙

关于firewall-cmd命令可参考: wangchujiang.com/linux-comma…

[root@localhost sbin]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
复制代码
  • 解决方法一:添加80端口到防火墙规则列表中
# 添加80端口到防火墙规则中
[root@localhost sbin]# firewall-cmd --permanent --add-port=80/tcp
success
# 从新载入防火墙,不会中断已经创建的链接
[root@localhost sbin]# firewall-cmd --reload
success
复制代码
  • 解决方法二:将防火墙关掉(推荐)
# 中止防火墙
[root@localhost sbin]# systemctl stop firewalld
# 禁止防火墙的开机自启
[root@localhost sbin]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
复制代码

再次访问nginx服务器80端口,此时Nginx能够正常访问

QvGWqK.png

或者经过curl nginx服务器的ip地址,也能够测试

[root@localhost sbin]# curl 192.168.245.130
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
复制代码

3.经常使用命令

注意:nginx的命令操做,默认必须在nginx的安装目录下的sbin目录中操做(能够经过配置环境变量解决) 默认位置在**/usr/local/nginx/sbin**下

若是不知道nginx的目录位置,可使用whereis命令获取

  • 获取nginx文件位置
[root@localhost sbin]# whereis nginx
nginx: /usr/local/nginx
复制代码
  • 查看nginx版本信息
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.12.2
复制代码
  • 启动nginx
[root@localhost sbin]# ./nginx 
复制代码
  • 关闭nginx
[root@localhost sbin]# ./nginx -s stop
复制代码
  • 从新加载nginx(不须要重启nginx服务器,从新读取nginx.conf配置文件)
[root@localhost sbin]# ./nginx -s reload
复制代码

4.配置文件解析

nginx的主配置文件位于:/usr/local/nginx/conf/nginx.conf

4.1文件结构

  • 全局块

从配置文件开始到events块之间的内容,主要设置一些影响nginx服务器总体运行时的配置指令

#user nobody;

#worker_processes:值越大,可处理的并发数据量也就越多
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
复制代码
  • events块

nginx服务器与用户的网络链接配置信息

#worker_connections:nginx支持的最大链接数
events {
    worker_connections  1024;
}
复制代码
  • http块

用于代理、缓存和日志等相关功能和第三方模块的配置。好比咱们常说的反向代理、负载均衡等等,都是经过配置http块实现的。http块中又包含http全局块server块

http全局块

包含文件引入、MIME-TYPE定义、日志自定义、链接超时时间、单连接请求数上限等内容。

include       mime.types;
default_type  application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile        on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout  65;

#gzip on;
复制代码

server块

每一个http块能够包含多个server块,每一个server块至关于一个虚拟主机。

虚拟主机:能够理解为经过nginx将一个物理的服务器(nginx服务器),经过server块的方式划分为多个虚机服务器对用户提供访问。

server {
	#server全局块
	#配置虚拟主机的监听配置和虚拟主机的名称和IP配置
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

	#location块
	#根据接收到的请求字符串对虚拟主机名称以外的字符串进行匹配,
	#对特定的请求进行处理、地址重定向、数据缓存和应答控制等功能
    location / {
        root   html;
        index  index.html index.htm;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    	root   html;
    }
}
复制代码

5.配置实例

5.1反向代理

什么是反向代理?

反向代理是指以代理服务器来接收来自互联网上的链接请求,而后将请求转发给内部网络上的服务器,并将从内部服务器上获得的结果经过代理服务器返回给来自互联网上请求链接的客户端,此时代理服务器对外的表现形式就是一个反向代理服务器。

image.png

好比咱们(用户)去租房子(Web服务器),一般状况下都是经过平台(代理服务器)去租。这个时候咱们是跟平台去联系的,并不知道房东是谁。还有一种多是房东委托本身的朋友去管理,这个时候跟咱们联系的也不是房东本人,而是他的朋友(代理服务器),这个过程就叫反向代理。而房东的朋友也就承担了“代理服务器”的这个角色。

案例一

需求:

​ 访问192.168.245.130:80端口,代理到192.168.245.131:8080端口

1. 安装JDK(tomcat须要依赖于JDK环境)

JDK是Java语言的软件开发工具包,JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

安装JDK,由于Tomcat须要JDK的环境支持

  • 将JDK和tomcat上传到服务器。
[root@nginx-02 ~]# ls -l
总用量 196020
-rw-r--r--. 1 root root   9830232 12月 22 15:40 apache-tomcat-8.0.33.zip
-rw-r--r--. 1 root root 190890122 12月 22 15:41 jdk-8u171-linux-x64.tar.gz
复制代码
  • 解压jdk-8u171-linux-x64.tar.gz/usr/local目录中
[root@nginx-02 ~]# tar zxf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@nginx-02 jdk1.8.0_171]# pwd
/usr/local/jdk1.8.0_171
[root@nginx-02 jdk1.8.0_171]# ls -l
总用量 25964
drwxr-xr-x. 2 10 143     4096 3月  29 2018 bin
-r--r--r--. 1 10 143     3244 3月  29 2018 COPYRIGHT
drwxr-xr-x. 4 10 143      122 3月  29 2018 db
drwxr-xr-x. 3 10 143      132 3月  29 2018 include
-rw-r--r--. 1 10 143  5203779 3月  29 2018 javafx-src.zip
drwxr-xr-x. 5 10 143      185 3月  29 2018 jre
drwxr-xr-x. 5 10 143      245 3月  29 2018 lib
-r--r--r--. 1 10 143       40 3月  29 2018 LICENSE
drwxr-xr-x. 4 10 143       47 3月  29 2018 man
-r--r--r--. 1 10 143      159 3月  29 2018 README.html
-rw-r--r--. 1 10 143      424 3月  29 2018 release
-rw-r--r--. 1 10 143 21098592 3月  29 2018 src.zip
-rw-r--r--. 1 10 143   106782 3月  29 2018 THIRDPARTYLICENSEREADME-JAVAFX.txt
-r--r--r--. 1 10 143   145180 3月  29 2018 THIRDPARTYLICENSEREADME.txt
复制代码
  • 编辑/etc/profile配置JAVA环境变量,在尾部加入如下内容

/etc/profile

# 配置JAVA环境变量
export JAVA_HOME=/usr/local/jdk1.8.0_171
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
复制代码
  • 输入javac -version,Java -version看是否有版本回显,有则表示JDK已安装完成
[root@nginx-02 ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
[root@nginx-02 ~]# javac -version
javac 1.8.0_171
复制代码

2. 安装Tomcat

Tomcat 服务器是一个免费的开放源代码的Web 应用服务器, 一般用于部署Java语言编写的网站应用。

  • 解压Tomcat到/usr/local/目录
#查看该目录下面是否存在tomcat文件
[root@nginx-02 ~]# ls
apache-tomcat-8.0.33.zip  jdk-8u171-linux-x64.tar.gz
#安装unzip工具命令
[root@nginx-02 ~]# yum install -y unzip
#使用unzip命令解压tomcat安装文件到/usr/local目录下
[root@nginx-02 ~]# unzip apache-tomcat-8.0.33.zip -d /usr/local/
#进入/usr/local目录
[root@nginx-02 ~]# cd /usr/local/
#查看是否存在解压后的tomcat文件目录
[root@nginx-02 local]# ls
apache-tomcat-8.0.33  bin  etc  games  include  jdk1.8.0_171  lib  lib64  libexec  sbin  share  src
#为apache-tomcat目录下的全部文件执行755权限
[root@nginx-02 local]# chmod 755 -R apache-tomcat-8.0.33/
#进入tomcat目录下的bin目录
[root@nginx-02 local]# cd apache-tomcat-8.0.33/bin/
[root@nginx-02 bin]# ls
bootstrap.jar  catalina-tasks.xml            configtest.bat  digest.bat        setclasspath.sh  startup.bat      tomcat-native.tar.gz  version.bat
catalina.bat   commons-daemon.jar            configtest.sh   digest.sh         shutdown.bat     startup.sh       tool-wrapper.bat      version.sh
catalina.sh    commons-daemon-native.tar.gz  daemon.sh       setclasspath.bat  shutdown.sh      tomcat-juli.jar  tool-wrapper.sh
#使用bash命令运行startup.sh脚本,启动tomcat服务器
[root@nginx-02 bin]# bash startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.0.33
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.0.33
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.0.33/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_171/jre
Using CLASSPATH:       /usr/local/apache-tomcat-8.0.33/bin/bootstrap.jar:/usr/local/apache-tomcat-8.0.33/bin/tomcat-juli.jar
Tomcat started.
#监测logs目录下catalina.out文件的日志内容,查看是否有报错信息,没有则表示启动成功
[root@nginx-02 bin]# tail -100f ../logs/catalina.out
复制代码

微信截图_20191222171231.png

看到以上内容表示tomcat已成功启动

  • 访问tomcat服务器(192.168.245.131),浏览器显示如下内容,表示tomcat能够正常访问

微信截图_20191222172753.png

若是内容没法正常显示,通常为防火墙问题,执行下面代码,从新刷新访问便可

#将8080端口加入防火墙规则列表
[root@nginx-02 bin]# firewall-cmd --add-port=8080/tcp --permanent
success
#从新载入防火墙配置文件
[root@nginx-02 bin]# firewall-cmd --reload
success
复制代码

3. 配置Nginx反向代理

说明:
主机nginx-01为nginx服务器	IP:192.168.245.130
主机nginx-02为Tomcat服务器	IP:192.168.245.131
浏览器访问主机nginx-01(192.168.245.130)80端口,代理到主机nginx-02(192.168.245.131)8080端口,浏览器显示nginx-02(192.168.245.131)的tomcat内容,表示反向代理成功
复制代码

image.png

  • 编辑nginx.conf主配置文件
vim /usr/local/nginx/conf/nginx.conf
复制代码
#配置server,能够理解为一个虚拟主机
server {
	#监听端口
    listen       80;
    #监听主机
    server_name  192.168.245.130;

	#路径位置
    location / { 
        root   html;
		index  index.html index.htm;
		#被代理的服务器,tomcat服务器地址
        proxy_pass http://192.168.245.131:8080/;
    }   
}
复制代码
  • 从新读取nginx.conf配置文件,使配置生效
[root@nginx-01 sbin]# ls
nginx
[root@nginx-01 sbin]# ./nginx -s reload
复制代码

访问nginx-01(192.168.245.130),查看浏览器效果

微信截图_20191222175515.png

案例二

需求:

​ 访问192.168.245.130/house代理到192.168.245.131:8080/house

​ 访问192.168.245.130/food代理到192.168.245.132:8090/food

微信截图_20191231014351.png

  1. nginx-0二、nginx-03安装Tomcat和JDK

    这个在案列一已经详细说明了,在此再也不重复。

  2. 在nginx-0二、nginx-03配置站点目录

    • nginx-02(host:192.168.245.131:8080)

    微信截图_20191231014351.png

    • nginx-03(host:192.168.245.132:8090)

    微信截图_20191231020227.png

若是是在同服务器部署多个Tomcat,为了不端口冲突,是须要修改Tomcat的对外访问端口8080;

若是是在不一样的服务器部署Tomcat,就不会存在端口的冲突问题,那么端口也就能够不用修改了,固然若是你想修改,也是能够的;

  1. 修改nginx-03的Tomcat服务端口号
[root@nginx-03 conf]# pwd
/usr/local/apache-tomcat-8.0.33-8090/conf
[root@nginx-03 conf]# vim server.xml 
复制代码

微信截图_20191231021031.png

  1. 启动nginx-0二、nginx-03的Tomcat服务
  • nginx-02

    微信截图_20191231021812.png

    微信截图_20191231024605.png

  • nginx-03

    微信截图_20191231021526.png

    微信截图_20191231024719.png

  1. 配置nginx反向代理

    • 编辑nginx.conf文件,添加代理配置

    微信截图_20191231022042.png

server {
    # 监听端口
    listen       80; 
    # 表示监听本地主机,建议写Ip地址
    server_name  localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    # 当请求的url中包含house的内容时,代理到http://192.168.245.131:8080/house站点中
    location ~ /house {
    	proxy_pass http://192.168.245.131:8080;
    }

    # 当请求的url中包含food的内容时,代理到http://192.168.245.132:8090/food站点中
    location ~ /food {
    	proxy_pass http://192.168.245.132:8090;
    }
}
复制代码
  • location指令说明

语法:

location [= | ~ | ~* | ^~ | ^~] uri {

}
复制代码
  1. =:用于不含正则表达式的uri前,要求请求字符串与uri严格匹配,若是匹配成功,就中止继续向下搜索并当即处理该请求。
  2. ~:用于表示uri包含正则表达式,而且区分大小写
  3. ~*:用于表示uri包含正则表达式,而且不区分大小写
  4. ^~:用于不含正则表达式的uri前,要求nginx服务器找到标识uri和请求字符串匹配度最高的location后,当即使用此location处理请求,而再也不使用location块中的正则uri和请求字符串作匹配。

*注意:若是uri包含正则表达式,则必需要有~或者~标识。

  1. 从新加载nginx.conf配置文件,使配置生效
[root@nginx-01 sbin]# ./nginx -s reload
复制代码
  1. 效果展现
  • 访问192.168.245.130/house返回192.168.245.131:8080/house

微信截图_20191231024017.png

  • 访问192.168.245.130/food返回192.168.245.132:8090/food

微信截图_20191231024341.png

5.2负载均衡

什么是负载均衡?

负载平衡(Load balancing)是一种计算机技术,用来在多个计算机(计算机集群)、网络链接、CPU、磁盘驱动器或其余资源中分配负载,以达到最优化资源使用、最大化吞吐率、最小化响应时间、同时避免过载的目的。 使用带有负载平衡的多个服务器组件,取代单一的组件,能够经过冗余提升可靠性。负载平衡服务一般是由专用软件和硬件来完成。 主要做用是将大量做业合理地分摊到多个操做单元上进行执行,用于解决互联网架构中的高并发高可用的问题。

引用于《维基百科》

举个例子:好比某饭店为了提高饭店的服务能力,饭店可能会雇佣多个厨师,而这些厨师就组成了一个厨师集群。而当用户在店内点菜的时候,就须要一个专业人员可以把全部客户的菜单均匀的分配给店内的厨师。这样才能最大程度的提高饭店的服务能力。

image.png

nginx实现负载均衡配置

需求:访问nginx-01(192.168.245.130:80)将流量均衡到nginx-02(192.168.245.131:8080)和nginx-03(192.168.245.132:8080)

  1. 配置nginx-02(192.168.245.131:8080)

image.png

  1. 配置nginx-03(192.168.245.132:8080)

image.png

  1. 配置nginx-01(192.168.245.130:80)nginx负载均衡

image.png

upstream tomcatserver{
    server 192.168.245.131:8080;
    server 192.168.245.132:8080;
}

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
    	proxy_pass http://tomcatserver;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
    }
}
复制代码
  1. 效果展现

Nginx-负载均衡.gif

nginx负载均衡策略

nginx为了方便咱们解决负载问题,提供了几种负载均衡的算法模式,使咱们能够根据需求场景去选择负载均衡的方式。

  • 轮询策略(默认算法)
upstream tomcatserver{
    server 192.168.245.131:8080;
    server 192.168.245.132:8080;
    server 192.168.245.133:8080;
    server 192.168.245.134:8080;
}
复制代码

轮询算法是nginx实现负载均衡的默认算法。轮询策略按照顺序选择组内(upstream模块配置的服务器节点)服务器处理请求。若是一个服务器在处理请求的过程当中出现错误,请求会被按照顺序依次交给组内的下一个服务器进行处理,依次类推,直到返回正常的响应为止。若是全部的组内服务器都出现错误,则返回最后一个服务器的处理结果。

  • weight策略(加权轮询)
upstream tomcatserver{
    server 192.168.245.131:8080 weight=5;
    server 192.168.245.132:8080 weight=3;
    server 192.168.245.133:8080;
    server 192.168.245.134:8080;
}
复制代码

为upstream组内的服务器设置权重,权重值高的服务器被优先用于处理请求。此时组内服务器的选择策略为加权轮询。组内全部服务器的权重值默认为1,即采用轮询的方式处理请求。

  • ip_hash策略
upstream tomcatserver{
	ip_hash;
    server 192.168.245.131:8080;
    server 192.168.245.132:8080;
    server 192.168.245.133:8080;
    server 192.168.245.134:8080;
}
复制代码

ip_hash用于实现会话保持功能,将某个客户端的屡次请求重定向到组内同一台服务器上,保证客户端与服务器之间创建稳定的会话。只有当服务器处于无效的状态时(down机),客户端请求才会被下一个服务器接收和处理。注意:使用ip_hash后不能使用weight;

  • fair(第三方算法)
upstream tomcatserver{
	fair;
    server 192.168.245.131:8080;
    server 192.168.245.132:8080;
    server 192.168.245.133:8080;
    server 192.168.245.134:8080;
}
复制代码

按照upstream组内服务器的响应时间来分配请求,响应时间短的优先分配,须要第三方模块的支持

5.3动静分离

什么是动静分离?

谈动静分离以前,先说一下什么是动,什么是静。

通常网站能够分为静态网站、动态网站。静态网站是指不须要去访问数据库资源的网站,一般称为静态网站。须要经过去查询数据库获取数据库的,咱们通常称为动态网站,动态网站可不是网站上能够动的动画效果的网站就是动态网站哈

  • 动静分离前

image.png

  1. 浏览器去请求Web服务器Tomcat,tomcat上存放着网站运行时,须要使用到的静态资源文件如图片、CSS样式文件、JavaScript脚本文件、.html文件和动态脚本文件JSP。
  2. Tomcat收到用户请求后,若是请求的JSP须要访问数据库的,会去数据库查询相关数据,并进行相关的逻辑处理,处理完成后将执行结果生成html经过Tomcat返回给浏览器。
  3. 浏览器拿到tomcat返回的html进行渲染,遇到图片,CSS、JS脚本资源时,浏览器异步请求tomcat服务器获取文件,tomcat返回浏览器请求的资源文件,浏览器继续渲染,迭代此操做,直到html页面所需的资源所有加载完成。
  4. 浏览器展现渲染效果,也就是用户看到的浏览器渲染后的页面。
  • 动静分离后

image.png

  1. 浏览器请求Nginx服务器,nginx服务器上存放着静态资源,而后nginx直接把用户请求的静态资源响应给浏览器,浏览器拿到.html文件后,进行渲染。渲染过程当中若是还须要加载其余的静态资源就再去请求nginx服务器获取,迭代此操做。
  2. 浏览器当须要请求接口时,先去请求nginx服务器,nginx看到这是后端的接口地址时,把请求经过反向代理发送给后端的tomcat服务器。
  3. tomcat服务器处理来自nginx发送的请求,当tomcat须要去查询数据库时,再去访问数据库。tomcat把nginx发来的接口地址进行解析处理,处理完成后,生成.html/json内容,返回给Nginx服务器,nginx服务器再把结果响应给浏览器。
  4. 浏览器拿到nginx响应的内容进行渲染。
  5. 浏览器展现渲染效果,也就是用户看到的浏览器渲染后的页面。

动静分离的好处

  1. api接口服务化:动静分离以后,后端应用更为服务化,只须要经过提供api接口便可,能够为多个功能模块甚至是多个平台的功能使用,能够有效的节省后端人力,更便于功能维护。
  2. 先后端开发并行:先后端只须要关心接口协议便可,各自的开发相互不干扰,并行开发,并行自测,能够有效的提升开发时间,也能够有些的减小联调时间 。
  3. 减轻后端服务器压力,提升静态资源访问速度:后端不用再将模板渲染为html返回给用户端,且静态服务器能够采用更为专业的技术提升静态资源的访问速度。
  4. 动静分离后, 即便动态服务不可用, 但静态资源不会受到影响

nginx实现动静分离配置

为了更好的模拟动静分离的效果。咱们的需求以下:

  • 访问nginx(192.168.245.130)时,经过nginx代理访问咱们的后端tomcat。
  • 访问静态资源(192.168.245.130/index.html)时,访问nginx上的静态资源站点
  • tomcat作tomcat集群,避免因单台tomcat死掉后,形成其余tomcat服务不可用。
  • tomcat所有死掉,静态页面也能够正常访问。
  1. 配置tomcat服务,使用tomcat7和tomcat8用来模拟后端

    • tomcat7(:192.168.245.132:8080)

    image.png

    • tomcat-8(192.168.245.131:8080)

    image.png

  2. 配置静态服务

image.png

nginx的源码安装默认静态资源目录通常在/usr/local/nginx/html目录下,在目录下默认存放着Nginx的欢迎页面文件

  • 在nginx的配置文件中配置静态资源

image.png

location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
	root /usr/local/nginx/html/food;
}
复制代码

root指令用于指定资源目录的位置,这里的意思是匹配到请求的uri中若是包含html、gif、jpg、png等后缀的静态资源文件时,去访问/usr/local/nginx/html/food这个目录下的资源。

  • 测试访问nginx静态资源(192.168.245.130:80/index.html)

image.png

  1. 在nginx中配置动态服务

image.png

拦截全部非静态的资源请求,经过proxy_pass发送到名为tomcatserver的upstream模块进行负载均衡(默认使用轮询策略),将请求转发给后端tomcat服务器,实现负载和tomcat集群。

至此,基本的动静分离配置就完成了。

说明:

若是此时访问经过nginx访问后端服务若是发生以下状况属于正常现象。

image-20200104210312897.png

缘由以下:访问后端服务时,由于须要加载静态资源文件,就触发了静态资源的访问规则,这个时候就会去/usr/local/nginx/html/food目录下面找,但实际上tomcat所须要的静态资源并不在该目录下,因此就找不到该资源,页面样式资源文件找不到报404也就不足为奇了。可是后端服务是能够正常访问的。一般状况下动静分离后的网站架构,后端只提供接口的访问服务,这里只是为了演示效果。

相关文章
相关标签/搜索