Nginx rewrite模块配置

一、Rewrite简介

1.1Rewrite跳转场景

●URL看起来更规范、合理
●企业会将动态URL地址伪装成静态地址提供服务
●网址换新域名后,让旧的访问跳转到新的域名上
●服务端某些业务调整

1.2Rewrite跳转实现

在这里插入图片描述

1.3Rewrite实际场景

●Nginx跳转需求的实现方式
使用rewrite进行匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转
●rewrite放在server{},if{},location{}段中
location只对域名后边的除去传递参数外的字符串起作用
●对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理

1.4Nginx正则表达式

常用的正则表达式元字符
^:匹配输入字符串的起始位置
$:匹配输入字符串的结束位置
*:匹配前面的字符零次或多次
+:匹配前面的字符一次或多次
?:匹配前面的字符零次或一次
.:匹配除\n之外的任何单个字符 使用[.\n]可以匹配包括\n在内的任意字符
\:转义符
\d:匹配纯数字
{n}:重复n次
{n,}:重复n次或更多次
[c]:匹配单个字符c
[a-z]:匹配a-z小写字母的任意一个
[a-zA-Z]:匹配a-z小写字母或A-Z大写字母的任意一个

二、Rewrite命令

2.1Rewrite命令语法

在这里插入图片描述

2.2flag标记说明

在这里插入图片描述

●last和break比较
在这里插入图片描述

三、location

3.1location优先级

●相同类型的表达式,字符串长的会优先匹配
●按优先级排列(把最先执行的写在配置文件的最上面)
= 类型
^~ 类型表达式
正则表达式(*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其它匹配,任何请求都会匹配到

3.2比较rewrite和location

●相同点
都能实现跳转
●不同点
rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
●rewrite会写在location里,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
●location优先级示例

扫描二维码关注公众号,回复: 11649237 查看本文章
location = / {	'//精确匹配 /,主机名后面不能带任何字符串'
    [configuraion A ]	
}

location / {	'//所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配'
    [configuraion B ]
}

location /documents/ {		'//匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用'
    [configuraion C ]
}

location ~ /documents/abc {		'//匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用'
    [configuraion D ]
}

location ^~ /images/ {	'//以/images/开头的地址,匹配符合后,停止往下匹配'
    [configuraion E ]
}

location ~*\.(gif|jpg|gpeg)$ {	'//匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高'
    [configuraion F ]
}

location /images/abc {	'//最长字符匹配到 /images/abc,优先级最低'
    [configuraion G ]
}

location ~ /images/abc {	'//以/ Images/abc开头的,优先级次之'
    [configuraion H ]
}

location /images/abc/1.html {	'//如果和正则 ~ images/abc/1.htm相比,正则优先级更高'
    [configuraion I ]
}

3.3location常用优先级规则
匹配某个具体文件
(location = 完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)>
(location ~ 完整路径)>(location /)

四、应用实例

环境准备:安装Nginx服务
1.安装nginx源

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安装nginx软件包

[root@localhost ~]# yum -y install nginx

3.修改默认站点配置文件:/etc/nginx/conf.d/default.conf

4.1基于域名的跳转

现在公司旧域名www.old.com有业务需求有变更,需要使用新域名www.new.com代替。
要求:
旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变
实验过程:
1.配置新旧域名的DNS
www.old.com 14.0.0.40
www.new.com 14.0.0.40
2.更改配置文件内容,添加域名跳转,如果访问www.old.com,则跳转到www.new.com

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.old.com;
    location / {
    if ($host = 'www.old.com') {
    rewrite ^/(.*)$ http://www.new.com/$1 permanent;
    }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
[root@localhost ~]# systemctl reload nginx.service

3.客户端访问www.old.com进行测试
在这里插入图片描述

4.2基于客户端IP访问跳转

要求:
公司内部可以用来访问服务器的地址为14.0.0.88,其他IP的客户端访问时跳转到error网页
1.进入主配置文件,写入如下内容

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
server {
   ...省略内容
    #access_log  /var/log/nginx/host.access.log  main;
#判断标志$rewrite
    set $rewrite true;
#允许公司内部访问,更改标志位false(这样不会执行下面if判断的rewrite了)
    if ($remote_addr = "14.0.0.88") {
       set $rewrite false;
     }
#如果不是公司IP,加上后缀地址作为标识
    if ($rewrite = true) {
        rewrite (.+) /error.html;
     }
#精确匹配有/error.html后缀的网页
    location = /error.html {
      root   /usr/share/nginx/html;
    }

2.两台客户端IP地址为14.0.0.77和14.0.0.88,访问网站测试
在这里插入图片描述
在这里插入图片描述

4.3基于旧、新域名跳转并加目录

要求:
将域名http://bbc.old.com下面的发帖都跳转到http://www.old.com/bbs,且域名跳转后保持参数不变
1.进入主配置文件,写入location配置段,先匹配/post再跳转

[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
    server_name  bbc.old.com;

    #access_log  /var/log/nginx/host.access.log  main;
    location /post {
     rewrite (.+) http://www.old.com/bbc$1 permanent;
    }

2.在网页站点/usr/share/nginx/html位置新建bbc目录下post目录下的abc.html网页文件,客户端浏览器访问bbc.old.com/post/abc.html
在这里插入图片描述

4.4基于参数匹配的跳转

要求:
现在访问http://www.test.com/100-(100|200)-100.html
跳转到http://www.test.com页面。
1.在主配置文件中加入内置变量request_uri的正则匹配。
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  www.test.com;
    #access_log  /var/log/nginx/host.access.log  main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
       rewrite (.*) http://www.test.com permanent;
    }
[root@localhost named]# systemctl restart nginx

2.访问www.test.com/100-100-10.html
在这里插入图片描述

4.5基于目录下所有php文件跳转

要求:
访问http://www.kgc.com/upload/1.php跳转到首页
1.使用正则匹配网址中以/upload/.*.php为结尾的网址

[root@localhost named]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.test.com;
    location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.test.com permanent;
    }
[root@localhost named]# systemctl restart nginx

2.客户端访问http://www.kgc.com/upload/1.php
在这里插入图片描述

4.6基于最普通url请求的跳转

要求:
访问一个具体的页面跳转到首页,如浏览器访问http://www.test.com/1/test.html跳转到首页。
1.使用正则匹配网页站点中域名后面是/1/html的网址

[root@localhost named]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.test.com;
    location ~* ^/1/test.html {
     rewrite (.+) http://www.test.com permanent;
    }
[root@localhost named]# systemctl restart nginx

2.浏览器访问http://www.test.com/1/test.html
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/chengu04/article/details/107964509