nginx之rewrite、if、基于浏览器实现分离案例及防盗链

rewrite

名词解释
URI:Uniform Resource Indentifier,统一资源标识符。用于定义全局范围内(包括但不仅限于互联网)去标记唯一的、定位一种资源访问路径的方式,或者命名方式,被称作统一资源标识符。这里的统一指的是路径格式上的统一。
URL:Uniform Resource Location,统一资源定位符,是URI的一个子集,用于描述在互联网上互联网资源的统一表示格式(protocol://host:port/path/to/file)
URL基本语法
语法:rewrite regex replacement flag;,如:
此时是访问本地

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;

此处的$1用于引用(.*.jpg)匹配到的内容,又如:
此时是访问网上的链接

rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;

如上例所示,replacement可以是某个路径,也可以是某个URL
常见的flag

last:基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个
一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理
而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程。
break:中止Rewrite,不再继续匹配
一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求,
且不再会被当前location内的任何rewrite规则所检查。
redirect:以临时重定向的HTTP状态302返回新的URL。
permanent:以永久重定向的HTTP状态301返回新的URL。
rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)。
nginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[ ]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
I匹配 I 之前或之后的试题
()分组,组成一组用于匹配的实体,通常会有 I 来协助

本地访问

例子
[root@localhost ~]# cd /usr/local/nginx/html/
50x.html  index.html
[root@localhost html]# mkdir images
[root@localhost html]# ls
50x.html  images  IMG_1998(20200720-092033).JPG  index.html
[root@localhost html]# mv IMG_1998\(20200720-092033\).JPG images/1.jpeg
[root@localhost html]# ls
50x.html  images  index.html

        
        #access_log  logs/host.access.log  main;
        
         location / {
            root   html;
            index  index.html index.htm;
        }   
        location /images {             //添加内容
            alias html/images;           //添加内容
        }

页面测试
在这里插入图片描述
修改地址继续访问

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  images  index.html
[root@localhost html]# mv images imgs
[root@localhost html]# ls
50x.html  imgs  index.html

页面访问此时访问失败
在这里插入图片描述
此时需要用到URL重写

修改图片名
[root@localhost html]# cd imgs/
[root@localhost imgs]# ls
1.jpeg
[root@localhost imgs]# mv 1.jpeg 1.jpg
[root@localhost imgs]# ls
1.jpg

修改文件
[root@localhost nginx]#  vim conf/nginx.conf
location /images {
            rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;     重写表示访问images的会自动跳转到访问imgs
        }

        location /imgs {
            root html;
        }
[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx -s reload

页面访问(发现此时还是没有修改地址,就可以自动跳转到新位置)
在这里插入图片描述
URL重写就是把原来的位置http://192.168.175.100/images/1.jpg跳转到新位置即http://192.168.175.100/imgs/1.jpg
修改位置

[root@localhost html]# mv imgs /opt/
[root@localhost html]# ls
50x.html  index.html
此时页面访问失败

解决方法
[root@localhost nginx]#  vim conf/nginx.conf
 }
        location /images {
            root /opt;                   //添加此行
            rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
        }

#        location /imgs {
#            root html;       
#        }            
[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx -s reload

页面访问测试(相同位置只是位置发生了变化,只需要在重新的上方加个位置即可)
在这里插入图片描述

网上访问

修改图片位置让它跳转到百度

[root@localhost nginx]#  vim conf/nginx.conf
添加图片链接地址
location /images {
            root /opt;
            rewrite ^/images/(.*\.jpg)$ https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1141259048,554497535&fm=26&gp=0.jpg break;
        }
[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx -s reload

页面访问(发现已经跳转到网上的图片链接)
在这里插入图片描述
last应用

[root@localhost nginx]#  vim conf/nginx.conf
表示只要是访问图片的都自动跳转到百度
 }
        location /images {
            root /opt;
            rewrite ^/images/(.*\.jpg)$ /imgs/$1 last;
        }

        location /imgs {
            rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com last;  
        }
[root@localhost nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx -s reload

页面测试(此时last表示到baidu时不再被后面的处理,baidu为最终的访问位置)
在这里插入图片描述

if

语法:if (condition) {...}

应用场景:server段、location段

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

[root@localhost ~]# cd /usr/local/nginx/html/
创建目录并添加内容
[root@localhost html]# mkdir Gecko chrome firefox
[root@localhost html]# ls
50x.html  chrome  firefox  Gecko  index.html
[root@localhost html]# cd chrome/
[root@localhost chrome]# echo 'chrome test page' >index.html
[root@localhost chrome]# cd ..
[root@localhost html]# cd firefox/
[root@localhost firefox]# echo 'firefox test page' >index.html
[root@localhost firefox]# cd ..
[root@localhost html]# cd Gecko/
[root@localhost Gecko]# echo 'Gecko test page' >index.html
[root@localhost html]# tree
.
├── 50x.html
├── chrome
│   └── index.html
├── firefox
│   └── index.html
├── Gecko
│   └── index.html
└── index.html

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
添加以下浏览器内容
location / {             
            if ($http_user_agent ~ Firefox) {
                 rewrite ^(.*)$ /firefox/$1 break;
            }
            if ($http_user_agent ~ Chrome) {
                 rewrite ^(.*)$ /chrome/$1 break;
            }
            if ($http_user_agent ~ Gecko) {
                 rewrite ^(.*)$ /Gecko/$1 break;
            }
            root   html;
            index  index.html index.htm;
        }                                                       
[root@localhost html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]# nginx -s reload

添加浏览器位置
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location /firefox {
              root html;
              index index.html;
          }
          location /chrome {
              root html;
              index index.html;
          }
          location /Gecko {
              root html;
              index index.html;
          }
[root@localhost html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]# nginx -s reload

页面访问(已成功实现浏览器分离)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
防盗链(即把访问自己指向别的网站)

location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.idfsoft.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.idfsoft.com/403.html;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值