Nginx的rewrite模块配置、域名自动跳转

Rewrite概述

Rewrite跳转场景

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

Rewrite跳转实现

在这里插入图片描述

Rewrite实用场景

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

Nginx正则表达式

常用的正则表达式元字符

字符 说明
^ 匹配输入字符串的起始位置
$ 匹配输入字符串的结束位置
* 匹配前面的字符零次或多次
+ 匹配前面的字符一次或多次
? 匹配前面的字符零次或一次
. 匹配除"In"之外的任何单个字符
\ 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用
\d 匹配纯数字
{n} 重复n次
{n,} 重复n次或更多次
[c] 匹配单个字符c
[a-z] 匹配a-z小写字母的任意一个
[a-zA-Z] 匹配a-z小写字母或A-Z大写字母的任意一个

Rewrite命令

Rewrite命令语法
rewrite  <regex>  <replacement>  [flag]
          正则      跳转后的内容   rewrite支持的flag标记

flag标记说明

标记 说明
last 相当于Apache的[L]标记,表示完成rewrite
break 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url
permanent· 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url

last和break比较

last break
使用场景 一般写在server和if中 一般使用在location中
URL匹配 不终止重写后的url匹配 终止重写后的url匹配

location概述

location分类

location = patt {} [精准匹配]
location patt {} [一般匹配]
location ~ patt {} [正则匹配]

正则匹配的常用表达式

标记 说明
~ 执行一一个正则匹配,区分大小写
~* 执行一个正则匹配,不区分大小写
!~ 执行一个正则匹配,区分大小写不匹配
!~* 执行一个正则匹配,不区分大小写不匹配
^~ 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他location
= 普通字符精确匹配。也就是完全匹配
@ 定义一个命名的location,使用在内部定向时

location优先级

  • 相同类型的表达式,字符串长的会优先匹配
  • 按优先级排列
    = 类型
    ^~ 类型表达式
    正则表达式( ~ 和~*)类型
    常规字符串匹配类型,按前缀匹配
    通用匹配(/) ,如果没有其它匹配,任何请求都会匹配到

rewrite和location比较

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

location优先级示例

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 ]
}

location优先级规则

匹配某个具体文件

  • (location = 完整路径) > (location ^~完整路径) > (location ~*完整路径) > (location ~ 完整路径) > (location 完整路径) > (location /)

Rewrite实验

手工编译安装nginx
或者yum安装nginx

##yum安装nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm	
#安装nginx源
yum install nginx -y

基于域名的跳转

需求
现在公司旧域名www.aaa.com有业务需求有变更,需要使用新域名www.new.com代替,但是旧域名不能废除,需要跳转到新域名上

安装配置解析DNS

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  www.aaa.com;   #域名修改

    #charset koi8-r;
    #access_log  /var/log/nginx/www.aaa.com.access.log  main;  #日志修改

    location / {                #域名重定向
        if ($host = 'www.aaa.com'){
           rewrite ^/(.*)$ http://www.new.com/$1 permanent;
        }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

在dns中添加www.new.com域名解析
进行测试
在这里插入图片描述

基于客户端IP地址的跳转

某公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司IP : 192.168.110.110访问正常。

[root@localhost opt]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.aaa.com;

    #charset koi8-r;
    access_log  /var/log/nginx/www.aaa.com.access.log  main;
    #设置是否合法的IP标志
    set $rewrite true;
    #判断是否为合法IP
    if ($remote_addr = "192.168.110.110"){
       set $rewrite false;
    }
    #对非法IP进行判断打上标记
    if ($rewrite = true){
       rewrite (.+) /error.html;
    }
    #匹配标记进行跳转站点
    location = /error.html {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        }
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

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

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

基于旧域名跳转到新域名后面加目录,例如现在访问的是bbb.aaa.com/post,现在需要将这个域名下面的都跳转到www.new.com/bbb注意保持域名跳转后的参数不变

[root@localhost opt]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  bbb.aaa.com;   #修改域名

    #charset koi8-r;
    access_log  /var/log/nginx/www.aaa.com.access.log  main;

    location /post {       #添加跳转页面
                rewrite (.+) http://www.newc.com.bbb$1 permanent;
        }
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
修改dns域名解析文件
bbb IN  A       192.168.110.15   修改为bbb

基于参数匹配的跳转

[root@localhost named]# vim /etc/nginx/conf.d/default.conf
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){        
       rewrite (.*) http://www.aaa.com permanent;
    }

在浏览器中输入www.aaa.com/100-100-100.html跳转至www.aaa.com页面

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

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

在浏览器中输入www.aaa.com/upload/aaa.php跳转至www.aaa.com页面

基于最普通url请求的跳转

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

在浏览器中输入www.aaa.com/1/test.html跳转至www.aaa.com页面

猜你喜欢

转载自blog.csdn.net/CN_PanHao/article/details/107965313
今日推荐