2020-09-06

Nginx动静分离与反向代理
1、反向代理
反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率

2、反向代理的工作原理
反向代理的工作原理是:代理服务器来接受客户端的网络访问连接请求,然后服务器将请求有策略的转发给网络中实际工作的业务服务器,并将从业务服务器处理的结果,返回给网络上发起连接请求的客户端。
3、优点:
3.1提高了内部服务器的安全
外部网络用户通过反向代理访向内部服务器,只能看到反向代理服务器的IP地址和端口号,内部服务器对于外部网络来说是完全不可见。而且反向代理服务器上没有保存任何的信息资源,所有的网页程序都保存在内部服务器上,对反向代理服务器的攻击并不能使真的网页信息系统受到破坏,这样就提高了内部服务器的安全性。
3.2加快了对内部服务器的访问速度
在内部服务器前放置两台反向代理服务器,分别连接到教育网和公网,这样公网用户就可以直接通过公网线路访问学校服务器,从而避开了公网和教育网之间拥挤的链路。同时反向代理服务器的缓存功能也加快了用户的访问速度。 [4]
3.3节约了有限的IP资源
校园网内部服务器除使用教育网地址外,也会采用公网的IP地址对外提供服务,公网分配的IP地址数目是有限的,如果每个服务器有分配-个公网地址,那是不可能的,通过反向代理技术很好的解决了IP地址不足的问题。
4、动静分离
动静分离是指在web服务器架构中,将静态页面与动态页面或者静态内容接口和动态内容接口分开不同系统访问的架构设计方法,进而提升整个服务访问性能和可维护性。
也就是说将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用访问。

5、优点
5.1 api接口服务化:动静分离之后,后端应用更为服务化,只需要通过提供api接口即可,可以为多个功能模块甚至是多个平台的功能使用,可以有效的节省后端人力,更便于功能维护。
§5.2前后端开发并行:前后端只需要关心接口协议即可,各自的开发相互不干扰,并行开发,并行自测,可以有效的提高开发时间,也可以有些的减少联调时间
5.3减轻后端服务器压力:提高静态资源访问速度:后端不用再将模板渲染为html返回给用户端,且静态服务器可以采用更为专业的技术提高静态资源的访问速度。

6、缺点
6.1不利于网站SEO(搜索引擎优化): 搜索引擎的网络爬虫一般是根据url访问页面,获取页面的内容后去掉没用的信息例如:CSS,JavaScript,然后分析剩下的文本内容;动静分离架构模式前端数据即在是由JavaScript来完成,这就会导致网络爬虫得到的信息部分丢失。在开发中可以采用前端缓存不经常变化数据的方式来解决,只有哪些经常发生变化的数据才每次向后端请求。
6.2开发量变大,前后端交流成本升高:后端api返回的数据,往往是有自身逻辑在内的,比如返回数据中的包含status(1-处理中,2-处理成功,3-处理失败),前端需要理解status的不同含义,对应的前端操作需要理解(如,status =1 or status = 2,不可提交)。
6.3在业务高速发展时需要慎重考虑:因为开发量变大,如果在业务开始阶段,缺乏前端又要求开发速度很快,就需要慎重考虑这种方式的实现成本对业务发展的影响

搭建Nginx动静分离和反向代理实验步骤如下:
实验环境:Centos7.3操作系统,
实验步骤:
搭建Nginx动静分离和反向代理
首先我们需要准备三台虚拟机,其中一台搭建好Nginx,并且配置好系统启动项,其余两台使用YUM工具,安装HTTPD服务
首先开始搭建Nginx服务,这里我是用脚本进行搭建。
[root@localhost ~]# vim 1.sh
#!/bin/bash

yum -y remove httpd
yum -y install gcc gcc-c++ proc-devel zlib-devel openssl-devel
useradd -M -s /sbin/nologin nginx
cd /home/yfr/
tar zxf nginx-1.11.5.tar.gz -C /usr/src/
cd /usr/src/nginx-1.11.5/
./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/
ls -l /usr/local/sbin/nginx
nginx -t
nginx
netstat -anpt | grep nginx
~
~
~
:x
[root@localhost ~]# chmod +x 1.sh 给脚本加执行权限
[root@localhost ~]# . 1.sh 执行脚本

启动之后来编写nginx启动脚本,并且添加到系统进程中。
[root@localhost nginx-1.11.5]# vim /etc/init.d/nginx
#!/bin/bash
#####################Welcome to nginx##################
#chkconfig:2345 99 20
#descrition:this is a nginx web server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case “$1” in
start)
$PROG
echo “startting nginx…”
;;
stop)
kill -s QUIT $(cat $PIDF)
echo “stopping nginx…”
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo “reload nginx…”
;;
*)

    echo "USAGE:$0 { start | stop | restart | reload }"
    exit 1

esac
exit 0
:x
[root@localhost nginx-1.11.5]# chmod +x /etc/init.d/nginx 给启动脚本加执行权限
[root@localhost nginx-1.11.5]# chkconfig --add nginx 添加Nginx启动脚本到系统进程中
[root@localhost nginx-1.11.5]# chkconfig nginx on 启动Nginx这个进程
[root@localhost nginx-1.11.5]# chkconfig --list nginx 查看Nginx的启动权限

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.

  If you want to list systemd services use 'systemctl list-unit-files'.
  To see services enabled on particular target use
  'systemctl list-dependencies [target]'.

nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost nginx-1.11.5]# systemctl restart nginx 重启一下服务
[root@localhost nginx-1.11.5]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7302/nginx: master

配置Nginx虚拟主机
[root@localhost ~]# mkdir -p /var/www/baidu 创建Nginx虚拟主机网页目录
[root@localhost ~]# vim /var/www/baidu/index.html

www.baidu.com

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 进入Nginx主配置文件 35 server { 36 listen 80; 37 server_name www.baidu.com; 38 39 #charset koi8-r; 40 41 #access_log logs/host.access.log main; 42 43 location / { 44 root /var/www/baidu; 45 index index.html index.htm; 46 } 47 [root@localhost ~]# 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@localhost ~]# systemctl restart nginx 重启一下Nginx服务器 [root@localhost ~]# netstat -anpt | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8025/nginx: master [root@localhost ~]# firefox 192.168.199.52

第二台服务器上去配置PHP动态页面:

首先我们使用YUM工具直接下载HTTP、PHP以及MySQL
[root@localhost ~]# yum -y install httpd php mysql-server
[root@localhost ~]# systemctl start httpd 启动HTTPD
[root@localhost ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 ::😗 LISTEN 14681/httpd
[root@localhost ~]# firefox 192.168.199.54 验证一下我们的HTTPD服务

[root@localhost ~]# vim /var/www/html/index.php 编辑PHP动态网页

<?php phpinfo(); ?>

~
~
:x
[root@localhost ~]# systemctl restart httpd 重启一下HTTPD服务,让服务读取到我们的网页
[root@localhost ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 ::😗 LISTEN 14948/httpd
[root@localhost ~]# firefox 192.168.199.54 验证一下我们的PHP网页

回到Nginx服务器上,配置反向动静分离。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
57 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
58 #
59 location ~ .php$ {
60 proxy_pass http://192.168.199.54; 这里改成PHP动态页面IP
61 }
62
[root@localhost ~]# 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@localhost ~]# systemctl restart nginx
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7782/nginx: master

客户机验证服务结果

首先访问我们的静态网页:
[root@localhost ~]# firefox 192.168.199.52

然后访问我们的动态网页:
[root@localhost ~]# firefox 192.168.199.52/index.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值