美文网首页
nginx规则解析(location、rewrite)与动静分离

nginx规则解析(location、rewrite)与动静分离

作者: dark68 | 来源:发表于2021-04-22 10:06 被阅读0次

1. location配置规则

1.1 location概述

规则 描述
~ 表示执行一个正则匹配,区分大小写
~* 表示执行一个正则匹配,不区分大小写
^* 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= 进行普通字符精确匹配
@ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

location规则的匹配优先级

  • 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。
  • ^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
  • 正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。
  • 常规字符串匹配类型。按前缀匹配。

2. rewrite规则模块解析

rewrite和location的功能有点相像,都能实现跳转,主要区别在于rewrite常用于同一域名内更改获取资源的路径,而location是对一类路径做控制访问和反向代理,可以proxy_pass到其他服务器。

Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用。

Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Nginx之前,需要编译安装PCRE库。通过Rewrite规则,可以实现规范的URL、根据变量来做URL转向及选择配置。
https://blog.csdn.net/qq_41475058/article/details/89516051

3. nginx结合Apache实现动静分离

3.1 apache的安装与配置

apache下载地址:http://httpd.apache.org/download.cgi#apache24

下载链接:https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz

安装Apache服务器(需要yum -y install apr apr-devel apr-util apr-util-devel,如果编译安装需要指定位置--prefix)

wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.46.tar.gz #下载Apache安装包

tar -zxvf httpd-2.4.46.tar.gz  #解压安装包

cd httpd-2.4.46

./configure --prefix=/usr/local/apache --enable-rewrite --enable-so --enable-headers --enable-expires --with-mpm=worker --enable-modules=most --enable-deflate

make && make install  # 编译安装

cp /usr/local/apache/bin/apachectl /etc/init.d/httpd  #复制启动文件到/etc/init.d目录

service httpd start #启动Apache

因为Apache与nginx默认都会监听80端口,所以我们在安装Apache或者启动Apache之前先关闭nginx,或者先把Apache的配置进行修改

Apache的配置修改

apache的配置目录在/usr/local/apache/conf/httpd.conf(注意:此处为笔者的配置文件地址)

Apache的虚拟主机可以在conf/httpd.conf中进行配置,也可以在conf/extra/httpd-vhost.conf中配置。后者是在httpd.conf文件中通过include指令引入的子配置文件,但是在使用前需要现在httpd.conf中找到如下一行配置取消注释,否则配置将不会生效。

#include conf/extra/httpd-vhost.conf  #去掉前面的#号

然后打开httpd-vhost.conf文件,可以看到Apache提供的默认配置。我们只需要删除多余的配置,添加自己的配置即可:

<VirtualHost *:81>
    DocumentRoot "/www/blog"
    ServerName www.blogs-httpd.com
    ServerAlias blogs-httpd.com
</VirtualHost>

<Directory "/www/blog">
    Require all granted
</Directory>

保存之后,重启Apache。

Apache配置访问php文件

Apache处理PHP动态请求的稳定性高于NGINX+PHP-fpm的方式,这个是因为PHP利用Apache的动态模块机制实现了高度整合。在安装Apache时,编译选项中有一个--enable-so,添加此选项后,Apache就会在bin目录下生成一个apxs程序,apxs是Apache提供的一个扩展工具,用于编译模块,PHP能够利用apxs编译一个用于Apache访问PHP的模块,以实现整合。

因为之前我们已经安装了nginx+php-fpm,这里需要整合php-fpm与Apache,所以对于php需要重新编译

make clean #清除之前安装过的nginx+PHP

./configure --prefix=/usr/localphp --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --enable-zip --enable-mbstring --with-mcrypt --with-mysql --with-mysqli --with-pdo-mysql --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-curl --with-openssl --with-mhash --enable-bcmath --enable-opcache

make && make install

Apache配置的修改:

<FilesMatch "\.php$">
setHandler application/x-httpd-php
</FilesMatch>

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

编译完成之后,使用在之前我们添加的Apache服务下新增访问phpinfo()的PHP代码文件,并访问:


phpinfo页面

3.2 nginx结合Apache动静分离

在web服务器中,动态请求是指请求需要服务器端的程序处理。例如:当用户请求一个PHP脚本文件时,就会调用PHP处理,并返回该脚本的处理结果。而静态请求不需要程序处理,直接读取文件并返回即可。如,html,css,javascript,图片等文件。针对这两种请求各自的特点,可以由nginx提供对外访问,静态请求直接由nginx处理,动态请求转交给Apache处理,这样就实现了动静分离。

  1. Apache的监听端口

当nginx作为前端web服务器后,就会占用80端口。为了让Apache能够正常的运行,需要改变Apache监听的端口号。

vim /usr/local/apache/conf/httpd.conf

listen 81

包括Apache虚拟机主机中的配置

<VirtualHost *:81>
    DocumentRoot "/www/blog"
    ServerName www.blogs-httpd.com
    ServerAlias blogs-httpd.com
</VirtualHost>

<Directory "/www/blog">
    Require all granted
</Directory>
  1. 修改nginx对动态请求的处理
server
{
    listen 80;
    server_name www.blogs.com;
    root /www/blog;
    index  index.html index.htm;

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:81;
        proxy_set_header Host $host;
        proxy_set_header X-Client-IP $remote_addr;
    }
}

相关文章

网友评论

      本文标题:nginx规则解析(location、rewrite)与动静分离

      本文链接:https://www.haomeiwen.com/subject/fvhxrltx.html