Nginx 的(location / rewrite)用法


一、Nginx常见模块

  • http
    http块是Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模块的配置都可以放在这模块中。作用包括:文件引入、MIME-Type定义、日志自定义、是否使用sendfile传输文件、连接超时时间、单连接请求数上限等。
  • server
    server块,虚拟主机(虚拟服务器)。作用:使得Nginx服务器可以在同一台服务器上只要运行一组Nginx进程,就可以运行多个网站。
  • location
    location块是server块的一个指令。作用:基于Nginx服务器接收到的请求字符串,虚拟主机名称(ip,域名)、url匹配,对特定请求进行处理。

二、访问路由location

1、location常用正则表达式

匹配符选项说明
^匹配输入字符串的起始位置
$匹配输入字符串的结束位置
*匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
?匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”
.表示任意一个字符
\转义字符 用于取消特殊符号的含义
\d匹配纯数字
\s匹配空的(空格或者制表符)
{n}匹配前面的子表达式n次
{n,}匹配前面的子表达式不少于n次
{n,m}匹配前面的子表达式n到m次
[ ]匹配括号中的一个字符
[c]匹配单个字符 c
[a-z]匹配 a-z 小写字母的任意一个
[a-zA-Z0-9]匹配所有大小写字母或数字
()表达式的开始和结束位置
|或运算符

2、location的分类

  • 精准匹配:location = / {}
  • 一般匹配:location / {}
  • 正则匹配:location ~ / {}

3、location 常用的匹配规则

表达式符号含义说明
=进行普通字符精确匹配。也就是完全匹配
^~表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他 location
~表示执行一个正则匹配,区分大小写
~*表示执行一个正则匹配,不区分大小写
!~表示执行一个正则匹配,区分大小写不匹配
!~*表示执行一个正则匹配,不区分大小写不匹配

4、location优先级排列说明

  1. 首先精确匹配 =
  2. 其次前缀匹配 ^~
  3. 其次是按文件中顺序的正则匹配 *
  4. 然后匹配不带任何修饰的前缀匹配
  5. 最后是交给 / 通用匹配
location = / {
    [ configuration A ]
}
 
location / {
    [ configuration B ]
}
 
location /documents/ {
    [ configuration C ]
}
 
location ^~ /images/ {
    [ configuration D ]
}
 
location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

5、location 示例

(1)精确匹配

location = / {}
=为精确匹配 / , 主机名后不能携带任何字符串,比如 / 和 /data ,则/匹配,/data不匹配

(2)一般匹配/通用匹配

location / {}
代表以/开头,所以这条规则将匹配所有的请求

location /documents/ {}
匹配任何以/documents/开头的地址,匹配符合后,还会继续往下搜索其他 location,只有其它location后面的正则表达式没有匹配到时,才会用这一条

location /documents/abc {}
匹配任何以/documents/abc开头的地址,匹配符合后,还会继续往下搜索其他 location,只有其它location后面的正则表达式没有匹配到时,才会用这一条

(3)正则匹配

location ^~ /images/ {} 
匹配任何已/images/ 开头的地址,匹配符合后,停止往下搜索,采用这一条

location ~* \.(gif|jpg|jpeg)$ {} 
匹配所有 gif jpg 或 jepg 结尾的请求
如果 有上面的location ^~ /images/ {}匹配  则会先匹配上一请求,所以到不了这一条。

6、location 优先级总结

  • 如果是匹配某个具体文件: (location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) >(location ~ 完整路径) > (location /)通用匹配

7、运用例子

(1)location = / {} 与 location / {}

实例1:location = / {} 和 location / {} ,按道理应匹配前者,但实际确实匹配后者,匹配只写域名精确匹配不生效,接下来一个小例子说明一下

1. #在配置文件添加匹规则
vim  /usr/local/nginx/conf/nginx.conf
 
#添加的网页
        location = / {     
            root   /web/dog/;
        }
 
#默认网页
        location / {
            root   html;
            index  index.html index.htm;        }
 
2. #新建网页站点目录
mkdir -p /web/dog
 
3. #在站点目录下新建index.html
vim /web/dog/index.html
 
this is dog web
 
4. #检查语法并重启
nginx -t
systemctl restart nginx.service 
 
5. #在网页中测试
http://192.168.192.135/
 

在配置文件添加匹规则
在这里插入图片描述
新建网页站点目录,新建index.html文件

在这里插入图片描述

(2) location = /index.html {} 与 location / {}

在 根后 加上index.html 后生效

1. #修改一下配置文件
vim  /usr/local/nginx/conf/nginx.conf
 
        location = /index.html {  # 加上index.html 
            root   /web/dog/;
        }
        
        
2. #重启服务,并网页中测试
systemctl restart nginx.service 

在这里插入图片描述

(3)location = /1.jpg {} 和 location ~* .(gif|jpg|png|jpeg)$ {}比较

1. #修改一下配置文件
vim  /usr/local/nginx/conf/nginx.conf
 
        location = /1.jpg { 
            root   /web/dog/;   #小狗的网页
        }
 
        location ~* \.(jpg|gif|png) {
            root   /web/cat/;  #小猫的网页
        }
 
2. #在刚刚创建的dog的站点下添加图片
cd /web/dog/
拖入图片
 
3. #新建一个cat目录,添加图片
mkdir cat
cd cat
拖入图片
 
 
4. #检查语法,重启服务
nginx -t
nginx -s reload
 
5. #在网页中测试
http://192.168.192.135/1.jpg
结果可以看出=精确匹配

(4)大小写问题

1. #修改配置文件
vim  /usr/local/nginx/conf/nginx.conf
 
location ~ /A.?\.jpg {
            root   /web/image;
            index  index.html index.htm;
        }
 
 
2. #新建站点目录并拖入图片
cd /web
mkdir image
cd image/


 
 - #重启并在网页中测试
nginx -s reload
 

在这里插入图片描述
在这里插入图片描述
windows系统中不用区分大小写,而linux系统中有些匹配项是需要对大小写有要求的
在这里插入图片描述
在这里插入图片描述

8、实际网站使用中的三个匹配规则定义

规则一

  • 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
  • 可以是一个静态首页,也可以直接转发给后端应用服务器
location = / {
	root html;
	index index.html index.htm;
	}

规则二

  • 是处理静态文件请求,这是nginx作为http服务器的强项有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用。
location ^~ /static/ {
       root /webroot/static/ ;
}      
 
location ~* \.(gif|jpg|jpeg)$ {
      root /webroot/static/ ;
}

规则三

  • 就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器。
location  / {
       proxy_pass http://tomcat_server;
}     

三、REWRITE模块

现在 Nginx 已经成为很多公司作为前端反向代理服务器的首选,在实际工作中往往会 遇到很多跳转(重写 URL)的需求。比如更换域名后需要保持旧的域名能跳转到新的域名 上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。如果在后端使用的 Apache 服务器,虽然也能做跳转,规则库也很强大,但是用 Nginx 跳转效率会更高,这也是学习 本章的目的所在。

1、rewrite功能

  • rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。
rewrite 只能在
server {}
location {}
if {}
中,并且默认只能对域名后边的除去传递的参数外的字符串起作用
例如:
http://www.txp.com/abc/bbs/index.html?a=1&b=2 只针对/abc/bbs/index.html重写

2、Rewrite 跳转场景

Rewrite 跳转场景主要的集中案例:

  • 可以调整用户浏览的 URL,看起来更规范,合乎开发及产品人员的需求
  • 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态 URL 地址伪装成静态地址提供服务
  • 网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的 360buy.com会跳转到 jd.com
  • 根据特殊变量、目录、客户端的信息进行 URL 调整等。

3、Rewrite 跳转实现

  1. Nginx 是通过 ngx_http_rewrite_module 模块支持 url 重写、支持 if 条件判断,但不支 持 else。
  2. 另外该模块需要 PCRE 支持,应在编译 Nginx 时指定 PCRE 支持,默认已经安装。
  3. 根据相关变量重定向和选择不同的配置,从一个 location 跳转到另一个 location,不过这样 的循环最多可以执行 10 次,超过后 Nginx 将返回 500 错误。
  4. 同时,重写模块包含 set 指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。

4、Rewrite 执行顺序

  1. 执行server块里面的rewrite指令。
  2. 执行location 匹配。
  3. 执行选定的location中的rewrite指令。

5、Rewrite 语法格式

rewrite <regex> <replacement> [flag];
regex:表示正则匹配规则
replacement:表示跳转后的内容
flag:表示rewrite 支持的 flag 标记
 
flag 标记说明:
last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中
 
break:本条规则匹配完成即终止,不再匹配后面的任何规则。一般用在location中
 
redirect:返回 302 临时重定向,浏览器地址会显示跳转后的 URL 地址,爬虫不会更新 url(因为是临时)。
 
permanent:返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL 地址,爬虫更新 url。

6、Rewrite 实例

(1)基于域名的跳转

  • 例如现在访问的是 域名txp.com启用了顺米网shunmi.com的DNS服务!,现在需要将这个域名下面的发帖都跳转到 http://www.dhc.com,注意保持域名跳转后的参数不变。
1. #修改配置文件
vim  /usr/local/nginx/conf/nginx.conf
 
server {
        listen       80;
        server_name  www.txp.com; 
 
        charset utf-8;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        location /index.html {
            if ($host = 'www.txp.com') {
            rewrite ^/(.*)$ http://www.dhc.com/$1 permanent;
            }
  root   /web/test;
        }
 
2. #新建站点目录
mkdir /web/test
 
3. #新建文件
vim /web/test/index.html
this is test web!!!
 
4. #重启服务
nginx -s reload
 
 
5. #另外开一台虚拟机,修改/etc/hosts文件
vim /etc/hosts
192.168.192.135 www.txp.com www.dhc.com
 
6. #打开新开的虚拟机的浏览器网页测试
http://www.txp.com

在这里插入图片描述
在这里插入图片描述
另外开一台虚拟机,修改/etc/hosts文件
在这里插入图片描述

在这里插入图片描述

(2)基于客户端IP 访问跳转

 - #修改配置文件
vim  /usr/local/nginx/conf/nginx.conf
 
 server {
        listen       80;
        server_name  www.txp.com;
 
        charset utf-8;
 
        #access_log  logs/host.access.log  main;
        set $rewrite true;
        if ($remote_addr = '192.168.192.10'){
        set $rewrite false;
        }
        if ($rewrite = true){
         rewrite (.+) /weihu.html;
        }
 
        location /weihu.html {
            root   /var/www/html;
        }
 
        location / {
            root   html;
           index  index.html index.htm;
        }
 
 - #新建站点目录及文件
mkdir -p /var/www/html
vim /var/www/html/weihu.html
 
we are busing now,please wait for a moment!
 
 - #重启服务                            nginx -s reload
 
 - #在网页测试
http://192.168.192.135/

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

(3)基于旧域名跳转到新域名后面加目录

  • 例如现在访问的是 http://bbs.txp.com/post 现在需要将这个域名下面的发帖都跳转到 http://www.txp.com/bbs/post,注意保持域名跳转 后的参数不变。
1. #修改配置文件
vim  /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.txp.com;
 
        charset utf-80;
 
        #access_log  logs/host.access.log  main;
        location /post {
         rewrite (.+) http://www.txp.com/bbs$1 permanent;
        }
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
2. #新建站点目录,并创建文件
mkdir -p /usr/local/nginx/html/bbs/post
vim /usr/local/nginx/html/bbs/post/index.html
 
this is the test!!
 
3. #开一台服务器,修改/etc/hosts文件
vim /etc/hosts
192.168.192.135 www.txp.com www.dhc.com bbs.txp.com
 
4. #重启服务并在浏览器中测试
nginx -s reload
 http://bbs.txp.com/post

修改配置文件
在这里插入图片描述
新建站点目录

在这里插入图片描述


创建文件

在这里插入图片描述

开一台服务器,修改/etc/hosts文件

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

(4)基于参数匹配的跳转

  • http://www.txp.com/100-(100|200)-100.html 跳转到 域名txp.com启用了顺米网shunmi.com的DNS服务! 页面
1. #修改配置文件
vim  /usr/local/nginx/conf/nginx.conf
 
  server {
        listen       80;
        server_name  www.txp.com;
 
        charset utf-8;
 
    #access_log  logs/host.access.log  main;
      location ~ /100-(100|200)-(\d+).html$ {
          rewrite (.+) http://www.test.com permanent;
       }
 
      location / {
           root   html;
           index  index.html index.htm;
        }
 
       
2. #重启服务
nginx -s reload
 
3. #验证
http://www.txp.com/100-100-100

先修改配置文件
在这里插入图片描述
然后重启服务进并入网页验证

(5)基于URL

  • 要求访问一个具体的页面 如 http://www.txp.com/abc/123.html 跳转到首页
1. #修改配置文件
 server {
        listen       80;
        server_name  www.txp.com;
 
        charset utf-8;
 
        #access_log  logs/host.access.log  main;
        location ~* ^/abc/123.html$ {
          rewrite (.+) http://www.txp.com permanent;
       }
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
2. #重启服务
systemctl restart nginx.service 
 
3. #在网页测试
http://www.txp.com/abc/123.html 

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值