Nginx rewrite——rewrite+if+location的不同使用方法

本文详细介绍了Nginx的rewrite模块,包括基本操作、location的分类和优先级,以及多种跳转场景的实验,如基于域名、IP地址、参数和目录的重定向,帮助读者掌握Nginx的URL管理和运维技巧。

一、概述

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

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

  • 跳转实现
            Nginx是通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。另外该模块需要 PCRE支持,应在编译Nginx时指定PCRE 支持,默认已经安装。根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后Nginx将返回500错误。同时,重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。rewrite功能就是,使用Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。

在这里插入图片描述

■Nginx跳转需求的实现方式

  • 使用rewrite进行匹配跳转
  • 使用if匹配全局变量后跳转
  • 使用location匹配在跳转

二、Nginx Rewrite基本操作

2.1Rewrite命令

Rewrite格式

  • rewrite < regex > < replacement >[flag]
  • regex:正则表达式
  • replacement :跳转后的内容
  • flag:rewrite支持的flag标记

flag标记说明

标记说明
last相当于Apache的[L]标记,表示完成rewrite
break本条规则匹配完成即终止,不在匹配后面的任何规则
redirect返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url
permanent返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url
  • last 和 break 的区别
lastbreak
使用场景一般写在server和if中一般使用在location中
URL匹配不终止重写后的url匹配终止重写后的url匹配

2.2location分类和优先级

2.2.1location分类

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

2.2.2location优先级

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

2.2.3location优先级规则

  • 匹配某个具体文件
    (location=完整路径) > (location ~ 完整路径) > (location ~ *完整路径) >(location~完整路径) > (location完整路径) > (location /)
  • 用目录做匹配访问某个文件
    (location=目录) > (location ^ ~ 目录/) > (location ~ 目录 ) > (location~*目录) > (location目录) > (location /)

三、Rewrite使用场景实验

3.1搭建基础环境

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.51xit.top;   ### 更改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.51xit.top.access.log;  ###改访问日志地址

        location / {
            root   html;
            index  index.html index.htm;
        }
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# touch /var/log/nginx/www.51xit.top.access.log

可以先访问一下看是否正常运作

3.2测试

  • 基于域名的跳转
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.51xit.top;

        charset koi8-r;

        access_log  /var/log/nginx/www.51xit.top.access.log;

        location / {
            root   html;
            index  index.html index.htm;
            if ($host = 'www.51xit.top')     ###这边改一下本机域名
                {
                rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;  ###这边改下跳转后的域名
}
        }

测试:打开浏览器输入www.51xit.top它会跳转到www.51xit.top中去

(3)、基于客户端IP地址的跳转

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.51xit.top;

        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        access_log  /var/log/nginx/www.51xit.top.access.log;

        set $rewrite true;
        if($remote_addr = "20.0.0.6") {
        set $rewrite false;

        access_log  /var/log/nginx/www.51xit.top.access.log;

        set $rewrite true;

        if($remote_addr = "20.0.0.6"){
        set $rewrite false;
        }

        if ($rewrite = true){
        rewrite (.+) /wh.html;
        }

        location = /wh.html {
        root /usr/local/nginx/html/;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# cp index.html wh.html  
[root@localhost html]#  vi wh.html               ###里面稍微修改一下,可以改成维护中。
[root@localhost html]# systemctl restart nginx.service   ###重启nginx服务

测试:客户端浏览器输入www.51xit.top自动跳转到维护页面

  • 基于旧域名跳转到新域名后面加目录
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   server {
        listen       80;
        server_name  www.51xit.top;  ###改域名

        charset utf-8;                ###改字符集

        access_log  /var/log/nginx/www.51xit.top.access.log; ###改日志存储路径,去掉main

        location /bbs {
        rewrite (.+) http://www.52xit.top/new$1 permanent;
        }
[root@localhost html]# systemctl restart nginx.service   ###重启nginx服务

测试:客户端浏览器输入www.51xit.top/bbs自动跳转新域名www.52xit.top/bbs

  • 基于参数匹配的跳转
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        access_log  /var/log/nginx/www.51xit.top.access.log;

        if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.*) http://www.51xit.top permanent;
        }
[root@localhost html]# systemctl restart nginx.service   ###重启nginx服务

测试:客户端浏览器输入www.51xit.top/100-100-100.html 页面显示www.51xit.top
客户端浏览器输入www.51xit.top/100-200-100.html 页面显示www.51xit.top
客户端浏览器输入www.51xit.top/100-200-10dsa0.html 页面不能跳转,因为有英文字符了,不是数字

  • 基于目录下所有php结尾的文件跳转,访问www.51xit.top/upload/1.php跳转到首页
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        access_log  /var/log/nginx/www.51xit.top.access.log;

        location ~* /upload/.*\.php$ {
        rewrite (.+) http://www.51xit.top permanent;
        }

[root@localhost html]# systemctl restart nginx.service   ###重启nginx服务

测试:访问www.51xit.top/upload/1.php跳转到www.51xit.top

  • 基于最普通一条url请求的跳转,访问一个具体的页面跳转到首页
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        access_log  /var/log/nginx/www.51xit.top.access.log;

        location ~* ^/1/test.html {
        rewrite (.+) http://www.51xit.top permanent;
        }

[root@localhost html]# systemctl restart nginx.service   ###重启nginx服务

测试:访问www.51xit.top/1/test.html跳转到www.51xit.top首页上

  1. 基于报错代码来跳转的
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   server {
        listen       80;
        server_name  www.51xit.top;

        charset utf-8;

        access_log  /var/log/nginx/www.51xit.top.access.log;
	
	error_page 500 502 503 504 /index.html;
	location = /index.html {
		root html;
	}

[root@localhost html]# systemctl restart nginx.service   ###重启nginx服务
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值