nginx URI重写与if的使用

  • 1.URI重写
  • 1.1.URI跟URL介绍
  • 1.2 rewrite使用方法
  • 1.2 网站目录不在默认html下
  • 1.3 rewrite 将URL重写
  • 1.4 flag
  • 1.4.1 last用法
  • 1.4.2 last与break的区别
  • 2.if
  • 2.1 基于浏览器实现分离案例
  • 2.2 防盗链案例

1.URI重写

1.1.URI跟URL介绍

URI:统一标识符,拿www.abc.com/aw/wd/举例,那么rui就是/aw/wd/这部分数据(也有可能是图片,html网页,如果是伪静态的话,那就得看配置是什么了

 URL:统一定位符,还是拿www.abc.com/aw/wd/举例,那么整个www.abc.com/aw/wd/就是url

 rewrite:重写是由ngx_http_rewrite_module模块提供,主要是实现URI改写功能,并且此模块是Nginx默认安装的模块。通过rewrite,用户可以实现url重定向,根据nginx规则来匹配内容跳转到replacement,结尾以flag标记。

   

1.2 rewrite使用方法

1.创建一个网页目录,里面有两张图
[root@zyy ~]# cd /usr/local/nginx/html/
[root@zyy zyy]# pwd
/usr/local/nginx/html/zyy

[root@zyy zyy]# ls
1.jpg  2.jpg  index.html





[root@zyy ~]# vim /usr/local/nginx/conf/nginx.conf

        location / {
           rewrite ^/test/(.*\.jpg)$ /zyy/$1 last;    ##前面为重写后uri,后面为真实的uri
        }

        location /zyy {           ##指定真实网页位置
          root     html;
        }




解释
##rewrite ^/test/(.*\.jpg)$ /zyy/$1 last;
将/zyy/$1($1为用户请求的资源名称)的uri重写为 ^/test/(.*\.jpg)$
所以,只要用户输入http://192.168.30.244/test/1.jpg  就可以访问到/zyy/1.jpg的资源(前提是zyy目录下有)





当然我们其实也没必要写两个location,在当前location指定/zyy的位置
location / {
           root     html;
           rewrite ^/test/(.*\.jpg)$ /zyy/$1 break;
       }

http://192.168.30.244/test/1.jpg

nginx 改写Host头 nginx修改uri_nginx 改写Host头

————————————————————————————————————————————————————

http://192.168.30.244/test2.jpg

nginx 改写Host头 nginx修改uri_chrome_02

   

1.2 网站目录不在默认html下
还有一种情况我们要重写URI的网站目录不在默认的html下


        location / {
           rewrite ^/test/(.*\.jpg)$ /zyy/$1 last;
        }

        location /zyy {
           root    /tmp;          ##只需要在这个重新定义默认目录就可以了
        }






写成这样也可以
      location / {
           root     /tmp;
           rewrite ^/test/(.*\.jpg)$ /zyy/$1 break;
       }

 一些小问题

为什么写成这样,不可行?


        location / {
           rewrite ^/test/(.*\.jpg)$ /zyy/$1 break;
        }

        location /zyy {
           root    /tmp;          ##只需要在这个重新定义默认目录就可以了
        }

查看error日志

nginx 改写Host头 nginx修改uri_chrome_03

nginx去访问/usr/local/nginx/html/zyy/2.jpg 证明下面那个location根本没有生效,所以,我们需要将break改为last,或者在当前location定义网页目录 

http://192.168.30.244/test/1.jpg

nginx 改写Host头 nginx修改uri_nginx 改写Host头_04

1.3 rewrite 将URL重写
location / {
        root  html;
           rewrite ^/test/(.*\.jpg)$ https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=journey&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined©right=undefined&cs=2264615217,3721323389&os=2563698849,602219563&simid=3338191960,239324301&pn=8&rn=1&di=42130&ln=1632&fr=&fmq=1597142538782_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=http%3A%2F%2Fimg.9ku.com%2Fgeshoutuji%2Fsingertuji%2F6%2F6043%2F6043_9.jpg&rpstart=0&rpnum=0&adpicid=0&force=undefined  break;
       }



rewrite ^/test/(.*\.jpg)$   ##后面跟url  break;

http://192.168.30.244/test/1.jpg

nginx 改写Host头 nginx修改uri_nginx_05

————————————————————————————————————————————————————

nginx 改写Host头 nginx修改uri_nginx 改写Host头_06

   

1.4 flag

flag

作用

last

停止处理后续rewrite指令集,跳出location作用域,并开始搜索与更改后的URI相匹配的location,URL地址不变

break

停止处理后续rewrite指令集,不会跳出location作用域,不再进行重新查找,终止匹配,URL地址不变

redirect

返回302临时重定向,浏览器地址栏会显示跳转后的URL地址,爬虫不会更新URL

permanent

返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫会更新URL

    rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)  

nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

标识符

意义

^

必须以^后的实体开头

$

必须以$前的实体结尾

.

匹配任意字符

[]

匹配指定字符集内的任意字符

[^]

匹配任何不包括在指定字符集内的任意字符串

|

之前或之后的实体

()

分组,组成一组用于匹配的实体,通常会有

   

1.4.1 last用法
[root@zyy ~]# vim /usr/local/nginx/conf/nginx.conf


       location / {
           rewrite ^/test/(.*\.jpg)$ /111/$1 last;
       }

        location /111 {
           rewrite ^/111/(.*\.jpg)$ /zyy/$1 last;
        }

        location /zyy {
           root html;
        }









网站目录不在默认html下
location / {
           root /tmp;
           rewrite ^/test/(.*\.jpg)$ /111/$1 last;
       }

 location /111 {
           rewrite ^/111/(.*\.jpg)$ /zyy/$1 last;
        }

nginx 改写Host头 nginx修改uri_chrome_07

   

1.4.2 last与break的区别
location /break {
           rewrite ^/break/(.*)$ /zyy/$1 break;
       }

        location /last {
           rewrite ^/last/(.*)$ /zyy/$1 last;
           echo 'last';
       }

        location /zyy {
           echo 'test zyy';
       }

nginx 改写Host头 nginx修改uri_html_08

分析:

break:break并不会重新发起一个请求,只是跳过当前的rewrite阶段,并执行本请求location后续的执行阶段…

last:停止当前这个请求,并根据rewrite匹配的规则重新发起一个请求。新请求又从第一阶段开始执行…

   

2.if

  语法:if (condition) {…}

 应用场景:

  • server段
  • location段  

常见的condition

  • 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
  • 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
  • 正则表达式的模式匹配操作
  • ~:区分大小写的模式匹配检查
  • ~*:不区分大小写的模式匹配检查
  • !~和!~*:对上面两种测试取反  
  • 测试指定路径为文件的可能性(-f,!-f)
  • 测试指定路径为目录的可能性(-d,!-d)
  • 测试文件的存在性(-e,!-e)
  • 检查文件是否有执行权限(-x,!-x)

   

2.1 基于浏览器实现分离案例

创建firefox和chrome网站目录

[root@zyy html]# mkdir firefox
[root@zyy html]# vim firefox/index.html
firefox




[root@zyy html]# mkdir chrome
[root@zyy html]# vim chrome/index.html
chrome



       location / {
            if ($http_user_agent ~ Firefox) {        ## $http_user_agent用来区分用户设备(这里用来判断浏览器类型)
              rewrite ^(.*)$ /firefox/$1 break;         ##如果是Firefox,访问时的uri为/firefox/$1
            }

            if ($http_user_agent ~ Chrome) {
              rewrite ^(.*)$ /chrome/$1 break;     ##如果是chrome,访问时的uri为/chrome/$1
            }
        }

       location  /firefox {             ##指定firefox目录所在位置
            root    html;
            index  index.html;
        }

       location /chrome {                ##指定chrome目录所在位置
            root html;
            index   index.html;
        }

在Chrome上测试 http://192.168.30.244

nginx 改写Host头 nginx修改uri_chrome_09

在Firefox上测试 http://192.168.30.244

nginx 改写Host头 nginx修改uri_nginx_10

   

2.2 防盗链案例

location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.idfsoft.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.idfsoft.com/403.html;
  }
}