温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx中的Rewrite和location用法详解

发布时间:2020-08-10 21:55:08 来源:网络 阅读:695 作者:23trl 栏目:云计算

Nginx中的Rewrite和location用法详解

rewrite定义

rewrite功能就是使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。
rewrite循环最多可以执行10次,超过过nginx将返回500错误
支持pcre语言,正则表达式,支持重写模块set指令

rewrite 语法

rewrite regex replacement [flag];
正则 跳转后的内容 rewrite支持的flag标记

    如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用proxy_pass反向代理。
   从上 表明看rewrite和location功能有点像,都能实现跳转。主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。

很多情况下rewrite也会写在location里,它们的执行顺序是:

1 执行server块的rewrite指令
2 执行location匹配
3 执行选定的location中的rewrite指令
如果其中某步URI被重写,则重新循环执行1-3,直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误。

rewrite 实用场景

使用rewrite进行匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转
rewrite 放在 sever{},if{},location{}段中
对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理

rewrite常用的正则表达式元字符

Nginx中的Rewrite和location用法详解

flag标记说明

Nginx中的Rewrite和location用法详解

last和break比较

Nginx中的Rewrite和location用法详解

location 详解之分类

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

正则匹配的常用表达式

Nginx中的Rewrite和location用法详解

location优先级

相同类型的表达式,字符串长的会优先匹配

按优先级排列

= 类型
^~ 类型表达式
正则表达式 (~和~*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其他匹配,任何请求都会匹配到

比较rewrite 和 location

相同点

都能实现跳转

不同点

rewrite实在同一个域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_到其他机器

rewrite会写在location里,执行顺序

执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令

http server event 三种消息级别结构

Nginx中的Rewrite和location用法详解

优先级的实列

 location = / {
 [ configuration A ]    ##精确匹配/,主机名后面不能带任何字符串
 }

location / {
 [ configuration B ]    ##所有的地址都以/开头,这条规则将匹配到所有请求,但正则
和最长字符串会优先匹配
 }
 location  /documents/ {
 [ configuration C ]    ##匹配任何以 /documents/ 开头的地址,当后面的正则表达式没有匹配到时,才起作用
 }

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

 }

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

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

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

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

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

location优先级规则

匹配某个具体文件

(location = 完整路径)>(location ^~完整路径)> (location ~完整路径) > (location ~ 目录) > (location 目录) > (location /)

用目录做匹配访问某个文件

(location = 目录)> (location ^~ 目录/) > (location ~ 目录) > (location ~* 目录) > (location 目录) > (location /)

具体使用场景——1.基于域名的跳转

安装nginx官方源

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.VbwudT: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]

安装nginx,配置nginx

[root@localhost ~]# yum install nginx -y
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 

  3     server_name  www.chen.com;  #第3行加入你的老域名

 6     access_log  /var/log/nginx/www.chen.com-access.log  main;  #第6行加入你老域名的日志消息

安装DNS服务解析

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

配置DNS主配置文件

[root@localhost ~]# vim /etc/named.conf 

 13         listen-on port 53 { any; };
 21         allow-query     { any; };

配置DNS区域配置文件

[root@localhost ~]# vim /etc/named.rfc1912.zones 
zone "chen.com" IN {
        type master;
        file "chen.com.zone";
        allow-update { none; };
};

配置DNS区域数据配置文件

[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost chen.com.zone
[root@localhost named]# cp -p chen.com.zone accp.com.zone 

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.136.181

[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx
[root@localhost named]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      81426/nginx: master 

去客户端去测试访问Nginx的页面

先将客户端DNS服务器选择我们DNS服务器的地址

Nginx中的Rewrite和location用法详解
Nginx中的Rewrite和location用法详解

配置Nginx主配置文件,做老域名转新域名

[root@localhost named]#vim /etc/nginx/conf.d/default.conf
location / {
#域名重定向
    if ($host = "www.chen.com") {  #如果你访问这个老域名
      rewrite ^/(.*)$ http://www.accp.com/$1 permanent;  #跳转为以/开头匹配多个任意字符结尾,这些参数,都为$1的变量永久性跳转
    }
    root  /usr/share/nginx/html;
    index  index/html index.html;
}

### 再为新域名去添加到DNS服务中去解析
[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};

[root@localhost named]# cp -p chen.com.zone accp.com.zone 

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

再去客户端去验证访问老域名时会转到新域名

Nginx中的Rewrite和location用法详解

具体使用场景——2.基于客户端IP访问跳转

先查看客户端的地址,这个地址为不合法的

Nginx中的Rewrite和location用法详解

配置nginx主配置文件,把原来的删掉 3dd

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 
 if ($host = "www.chen.com") {
          rewrite ^/(.*)$ http://www.accp.com/$1 permanent;
        }

#设置是否合法的IP标志
set $rewrite true;
#判断是否为合法的IP
if ($remote_addr = "192.168.136.140"){  #140我们把这个地址设置成合法的IP
    set $rewrite false;
}
#非法IP进行判断打上标记
if ($rewrite = true){
    rewrite (.+) /main.html;
}
#匹配标记进行跳转站点
location = /main.html {
    root /usr/share/nginx/html;
}

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html  index.html

去创建一个维护页面(给不合法的地址访问的页面)

[root@localhost html]# vim main.html
<h2>this is test web</h2>

去客户端先去测试一下不合法的页面(www.accp.com的地址是192.168.136.139)

Nginx中的Rewrite和location用法详解

再去测试一下合法的页面

先换成固定IP再去访问

Nginx中的Rewrite和location用法详解
Nginx中的Rewrite和location用法详解

具体使用场景——3.基于旧域名跳转到新域名后面加目录,比如现在访问的是http://bbs.accp.com,现在需要将这个域名下面的发帖都跳转到http://www.accp.com/bbs

[root@localhost html]# vim /etc/nginx/conf.d/default.conf
##把原来的删掉14dd

  7     location /post {  #匹配到/post才能进行下面的操作
  8         rewrite (.+) http://www.accp.com/bbs$1 permanent;  #任意字符,www.accp.com/bbs $1调前面任意字符的参数,永久跳转
  9     }
[root@localhost named]# cd /var/named/
[root@localhost named]# vim accp.com.zone 

bbs IN  A       192.168.136.182

[root@localhost named]# systemctl restart nginx
[root@localhost named]# systemctl restart named

去服务器端网站测试一下

Nginx中的Rewrite和location用法详解

具体使用场景——4.基于参数匹配的跳转

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

#记得把原来的删掉
 if ($request_uri ~ ^/100-(100|200)-(\d+).html$){  #$request_uri是内置变量,匹配开头为100到100或200,\d+代表1-9任意的数字,然后为结尾的html
        rewrite (.*) http://www.accp.com permanent;  #就跳转到.*代表0次或多次这个域名的页面永久
}
[root@localhost named]# vim accp.com.zone 
www IN  A       192.168.136.182

[root@localhost named]# systemctl restart nginx
[root@localhost named]# systemctl restart named

去客户端测试一下

Nginx中的Rewrite和location用法详解

具体使用场景——5.基于目录下所有php文件跳转

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 
#把原来的删掉3dd
  location ~* /upload/.*\.php$ {  #匹配正则不区分大小写,upload代表一个目录。匹配php结尾的所有文件,\.转译.这个符号
        rewrite (.*) http://www.accp.com permanent; #永久跳转首页
}
[root@localhost named]# vim /etc/nginx/conf.d/default.conf 
[root@localhost named]# systemctl restart nginx

去客户端测试一下

Nginx中的Rewrite和location用法详解

具体使用场景——6.根据具体的页面跳转首页

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 
#原来的第一条改成自己设置的一个具体页面
  location ~* ^/abc/123.html {    #以/abc/123.html 开头的

[root@localhost named]# systemctl restart nginx

去客户端测试一下

Nginx中的Rewrite和location用法详解

以上就是我们全部的内容了

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI