nginx-11 重写模块

在这里插入图片描述

ngx_http_rewrite_module模块允许正则替换URI,返回页面重定向,和按条件选择配置。

rewrite的主要功能是实现URL地址的重定向。如伪静态、网址换新域名后,让旧的访问跳转到新的域名上、隐藏index.php等。

此处最常用的
在这里插入图片描述
隐藏index.php

location / {
	   try_files $uri $uri/ /index.php?$query_string;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php last;
            }

            index  index.html index.htm index.php;
            #autoindex  on;
 }

break和last
break:只匹配一次,停止匹配。

last:如果指令改变了URI,按新的URI查找location。这个循环至多重复10次,之后nginx返回错误500 (Internal Server Error)。

 location /api {
                rewrite  ^/api/(.*)$    /test/$1  last;
}
 location /test {
                rewrite  ^/test/(.*)$    /api/public/index.html/$1  break;
}

当/api/aa 进入location /api时,被修改URI,然后按照新的URI继续查找location去匹配,匹配到location /test中。

伪静态

location /ecshop {
                    root /www;
                   # /ecshop/goods-3.html ---->/ecshop/goods.php?id=3
                   rewrite  goods-(\d+)\.html$    /ecshop/goods.php?id=$1 break;
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值