Nginx+fastcgi工作原理与nginx-rewrite模块的应用【rewrite模块的跳转】以及apache+nginx动静分离

nginx工作原理与nginx-rewrite模块的应用以及动静分离

文章目录

一、nginx工作原理原理及分类

Nginx 本身做的工作实际很少,当它接到一个 HTTP 请求时, 它仅仅是通过查找配置文件将此次请求映射到一个 location block,而此 location 中所配 置的各个指令则会启动不同的模块去完成工作,因此模块可以看做 Nginx 真正的劳动工作者。Nginx 由内核和模块组成。

1、从结构分类
【1】nginx核心模块

HTTP 模块,EVENT 模块(进程应用的模块),MAIL 模块(邮件使用的模块)

【2】nginx基础模块

HTTP Access 模块、HTTP FastCGI 模块、HTTP Proxy 模块和 HTTP Rewrite 模块

【3】nginx第三方模块

HTTP Upstream Request Hash 模块、Notice 模块和 HTTP Access Key模块等等

2、从功能分类

通常一个 location 中的指令会涉及一个 handler 模块和多个 filter 模块(当然,多个 location 可以复用同一个模块)。handler 模块负责处理请求,完成响应内容的生成,而 filter 模块对响应内容进行处理。 用户根据自己的需要所开发的模块都属于第三方模块。正是有了这么多模块的支撑, Nginx 的功能才会如此强大。

【1】handles(处理模块)

此类模块直接处理请求,并进行输出内容和修改 headers 信息等操作。Handlers 处理器模块一般只能有一个

【2】fliter(过滤器模块)

此类模块主要对其他处理器模块输出的内容进行修改操作,最后由 Nginx 输出

【3】proxies(代理类模块)

此类模块是 Nginx 的 HTTP Upstream 之类的模块,这些模块主要与后端一些服务比如 FastCGI 等进行交互,实现服务代理和负载均衡等功能。

二、nginx进程

在工作方式上,Nginx 分为单工作进程和多工作进程两种模式。在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;在多工作进程模式下,每个工作进程包含多个线程。Nginx 默认为单工作进程模式。

1、master进程

master 进程主要用来管理 worker 进程,主要包含:接收来自外界的信号,向各worker进程发送信号,监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动 重新启动新的worker进程。master进程充当整个进程组与用户的交互接口,同时对进程进行监护,不需要处理网络事件,不负责业务的执行。

2、worker进程

worker进程实现重启服务、平滑升级、更换日志文件、配置文件实时生效等功能。

三、Nginx+FastCGI(快速通用网关接口)运行原理

1、认识FastCGI

FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。
FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等。

2、运行原理

在这里插入图片描述
上图就是运行原理,如果你悟到了你就懂了,悟不到我解释了也是白搭!

四、nginx-rewrite模块介绍

因为目前很多工作前端开发都会选择使用Nginx作为反向代理服务器,但是平时业务需要难免碰到重写URL,Nginx的Rewrite跳转有什么使用场景呢?公司更换域名需要访问旧域名时跳转到新域名请求静态文件跳转到CDN根据用户设备不同跳转到不同站点(pc端,移动端)不得不说的是Apache服务器规则库很强大,做跳转也很简单,但是Nginx使用Rewrite实现跳转效率更高,所以这也是我们需要学习Nginx的Rewrite模块的目的所在。

1、Rewrite是Nginx的静态重写模块,跳转的使用场景有以下几种情形:

可以改变用户访问的URL
可以将动态URL伪装成静态URL提供服务
可以访问旧域名时跳转到新域名
可以根据变量,目录,客户端信息等跳转不同的URL

2、Rewrite的实现跳转

Nginx利用ngx_http_rewrite_module模块解析和处理rewrite请求。Rewrite用于实现URL重写,其实有点类似于重定向功能,可以将用户的请求重写至别的目录,在一定程度上提高了网站安全性。Rewrite支持if条件判断,但不支持else判断。而且Rewrite需要PCRE支持,一次重定向最多可以跳转10次,超过10次将返回500错误。Rewrite模块包含set命令,可以创建变量用来记录条件标识或者传递变量到其他的Location中。Rewrite实际上就是使用Nginx已有的全局变量或者通过set命令设置的变量结合正则表达式实现URL重写。

3、Rewrite使用场景

在Nginx中使用Rewrite实现跳转有以下三种场景:
【1】直接用Rewrite进行匹配跳转
【2】使用if匹配全局变量进行跳转
【3】使用location匹配再进行跳转
所以说rewrite语句只允许放在server{ },if{ },location{ }中。location只对域名后边的除去传递参数外的字符串起作用,而对域名或参数字符串使用if全局变量匹配,用proxy_pass反向代理。
rewrite语法:

rewrite regex replacement [flag]

rewrite将用户请求的URL基于正则表达式regex进行检查,匹配到时将其替换为正则表达式对应的新的URL。若在同一级配置模块中存在多个rewrite规则,则会自顶向下检查。replacement则为跳转后的内容。[flag]作为标识符用于控制循环机制,如果替换后的URL是以http或者https开头,则会直接301永久重定向。

【1】flag参数介绍

rewrite语句有四种flag状态:redirect、permanent、break、last。前两种属于客户浏览器重新发起对新地址的请求,后两种是在WEB服务器内部实现跳转。
(1)redirect :临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,有客户端重新发起请求,使用相对路径,http://或https://开头,状态码:302
(2)permanent :永久重定向,以永久重定向的方式直接返回重写后生成的新URL给客户端,由客户端重新发起新的请求,状态码:301
(3)last :重写完成后停止对当前location中后续的其他重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
(4)break :重写完成后停止对当前URL在当前location中后续的其他重写操作,而后直接跳转至重写规则匹配块之后的其他配置;结束循环,建议在location中使用。

【2】临时重定向与永久重定向的区别:

临时重定向不会缓存新域名的解析记录,但是永久重定向会缓存新域名的解析记录。

【3】location分类

location = patt {} //精准匹配
location patt {} //一般匹配
location ~ patt {} //正则匹配
(1)正则匹配的常用表达式
在这里插入图片描述
(2)location优先级规则
(location = 完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)>(location ~ 完整路径)>(location 完整路径)>(location /)

五、nginx-rewrite模块配置

1、基于域名跳转

公司旧域名www.kgc.com因为业务需求有变更,需要使用新的域名www.newkgc.com代替,但不能废除旧的域名,从旧的域名跳转到新的域名,且保持其参数不变。

【1】安装nginx源和软件包
[root@localhost ~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y nginx
【2】修改默认站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.kgc.com;
    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    location / {
        if ($host = 'www.kgc.com') {
          rewrite ^/(.*)$ http://www.newkgc.com/$1 permanent;
 }
}

上面配置文件中出现了www.kgc.com域名,所以要配置域名解析服务。如果要在实现跳转之后出现默认界面区域配置文件需要修改。

【3】启动nginx
[root@localhost ~]#systemctl restart nginx
【4】验证

在这里插入图片描述

2、基于IP地址跳转

所有IP访问任何内容都显示一个固定维护页面,只有公司内部ip访问正常定义IP地址192.168.60.33是公司内部IP地址,192.168.60.44不是公司内部IP地址

【1】安装nginx源和软件包
[root@localhost ~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y nginx
【2】修改默认站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.kgc.com;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    
    set $rewrite true;
    if ($remote_addr = "192.168.60.33") {
       set $rewrite false;}    
    if ($rewrite = true) {  
       rewrite (.+) /error.html;}    
    location = /error.html {       
       root    /usr/share/nginx/html;}
【3】启动nginx
[root@localhost ~]#systemctl restart nginx
【4】验证

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

3、基于旧、新主机名跳转并加上目录

将域名http://bbs.kgc.com下面的发帖都跳转到http://www.kgc.com/bbs,切域名跳转后参数不变

【1】安装nginx源和软件包
[root@localhost ~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y nginx
【2】修改默认站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  bbs.kgc.com;
    location /post {
       rewrite (.+) http://www.kgc.com/bbs$1 permanent;
}
location / { 
  root   /usr/share/nginx/html;      
  index  index.html index.htm;
}

上面配置文件中实现的是bbs.kgc.com域名,所以要修改DNS服务的数据配置文件

【3】启动nginx
[root@localhost ~]#systemctl restart nginx
【4】验证

在这里插入图片描述

4、基于参数匹配跳转
【1】安装nginx源和软件包
[root@localhost ~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y nginx
【2】修改默认站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.kgc.com;
    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
          rewrite (.*) http://www.kgc.com permanent;
   } 
   location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...........................
}    
【3】启动nginx
[root@localhost ~]#systemctl restart nginx
【4】验证

在这里插入图片描述

5、基于目录下所有的php文件跳转
【1】安装nginx源和软件包
[root@localhost ~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y nginx
【2】修改默认站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.kgc.com;
    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    location ~* /upload/.*\.php$ {
       rewrite (.+) http://www.kgc.com permanent;
   }    
      location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...........................
}    
【3】启动nginx
[root@localhost ~]#systemctl restart nginx
【4】验证

在这里插入图片描述

6、基于URI请求跳转
【1】安装nginx源和软件包
[root@localhost ~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#yum install -y nginx
【2】修改默认站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.kgc.com;
    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    location ~* ^/abc/123.html {
          rewrite (.+) http://www.kgc.com permanent;
    }
       location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...........................
}   
【3】启动nginx
[root@localhost ~]#systemctl restart nginx
【4】验证

在这里插入图片描述

六、apache+nginx动静分离

1、在192.168.60.60服务器上搭建LAMP架构作为动态页面
systemctl stop firewalld
setenforce 0
【1】线网源安装Apache服务
yum install -y httpd httpd-devel 
systemctl start httpd.service 
【2】线网源安装数据库
yum install -y mariadb-server mariadb-libs mariadb-devel 
systemctl start mariadb.service 
netstat -natp |grep 3306
mysql_secure_installation  //数据库访问权限设置

Enter current password for root (enter for none):空

Set root password? [Y/n] y
New password: 123123
Re-enter new password: 123123

Remove anonymous users? [Y/n] n

Disallow root login remotely? [Y/n] n

Remove test database and access to it? [Y/n] n

Reload privilege tables now? [Y/n] y
【3】线网源安装php服务
yum install php -y
yum install -y php-mysql  //安装连接数据库软件
yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-cmath
cd /var/www/html/
vim index.php

<?php
  phpinfo();
?>

vim /etc/httpd/conf/httpd.conf

ServerName www.test.com:80 
Listen 192.168.60.60:80
#Listen 80

systemctl restart httpd.service 
【4】在浏览器上面访问192.168.60.60出现php信息界面

在这里插入图片描述

2、在192.168.60.20服务器上面手工编辑安装静态页面

实现的效果是:访问192.168.60.20是出现静态界面是本服务器提供的,而访问192.168.60.20/index.php是出现
动态界面php信息界面,由192.168.60.60提供的。

【1】nginx手工编译安装
cd nginx/
tar zxvf nginx-1.12.2.tar.gz
yum install -y gcc gcc-c++ pcre-devel zlib-devel
cd /opt/nginx/nginx-1.12.2/
useradd -M -s /sbin/nologin nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
ln -s /usr/local/nginx/conf/nginx.conf /etc/
nginx -t
【2】service管理
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
prog="/usr/local/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
    ;;
  *)
    exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx
service nginx start
【3】安装网页访问软件
yum install -y elinks
elinks http://192.168.60.20
q表示退出
[root@nginx html]# elinks http://192.168.60.20

使用这个软件访问nginx静态页面如下:
在这里插入图片描述

【4】修改nginx动静分离配置文件
vim /etc/nginx.conf 

location ~ \.php$ {
            proxy_pass   http://192.168.60.60;
        }

service nginx restart
【5】访问

在主机上面访问192.168.60.60出现静态页面
在这里插入图片描述
访问192.168.60.60/index.php出现动态页面
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值