• NGINX重写功能和防盗链


    NGINX重写功能和防盗链

    重写功能

    Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatible regular expression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为访问,另外还可以在一定程度上提高网站的安全性。

    官方文档: https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

    rewrite指令

    通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理

    https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite  #官方文档
    
    • 1
    rewrite regex replacement [flag];
    重写		正则   替换			标志
    
    正则表达式格式
    . #匹配除换行符以外的任意字符
    \w #匹配字母或数字或下划线或汉字
    \s #匹配任意的空白符
    \d #匹配数字     
    \b #匹配单词的开始或结束
    ^ #匹配字付串的开始
    $ #匹配字符串的结束
    * #匹配重复零次或更多次
    + #匹配重复一次或更多次
    ? #匹配重复零次或一次
    (n) #匹配重复n次
    {n,} #匹配重复n次或更多次
    {n,m} #匹配重复n到m次
    *? #匹配重复任意次,但尽可能少重复
    +? #匹配重复1次或更多次,但尽可能少重复
    ?? #匹配重复0次或1次,但尽可能少重复
    {n,m}? #匹配重复n到m次,但尽可能少重复
    {n,}? #匹配重复n次以上,但尽可能少重复
    \W  #匹配任意不是字母,数字,下划线,汉字的字符
    \S #匹配任意不是空白符的字符
    \D #匹配任意非数字的字符
    \B #匹配不是单词开头或结束的位置
    [^x] #匹配除了x以外的任意字符
    [^kgc] #匹配除了kgc 这几个字母以外的任意字符
    
    flag说明
    redirect;302
    #临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
    
    permanent;301       www.bj.com     www.beijing.com
    #重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301
    
    break;       www.bj.com
    #重写完成后,停止对当前URL在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用
    #适用于一个URL一次重写 
    
    last;
    #重写完成后,停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
    #适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户301
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    server {
       listen 80;
       server_name www.test.com;
       root /data/nginx/html/;
       location / {
       rewrite ^/bj/(.*)    /beijing/$1   permanent; #访问bj跳转到beijieng页面
     }
    }
    
    [root@localhost conf.d]# cd /data/nginx/html
    [root@localhost html]# mkdir bj ;echo This is bj > bj/index.html
    [root@localhost html]# mkdir beijing;echo This is Beijing > beijing/index.html
    [root@localhost html]# ls
    beijing  bj  test.html
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    location / {
       root /data/nginx/html/pc;
       index index.html;
       rewrite / http://www.kgc.com permanent; #访问后跳转到这个网站
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    if指令

    用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断

    https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if #官方文档
    格式
    if (条件匹配) { 
     action
    }
    
    = #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
    !=  #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
    ~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
    !~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
    
    ~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
    !~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    location /main {
         index index.html;
         default_type text/html;
         if ( $scheme = http ){
           echo "if-----> $scheme";
         }
         if ( $scheme = https ){
          echo "if ----> $scheme";
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    return指令

    return用于完成对请求的处理,并直接向客户端返回响应状态码

    www.kgc.com/test/
    404
    return code; #返回给客户端指定的HTTP状态码
    return code [text]; #返回给客户端的状态码及响应报文的实体内容,可以调用变量,其中text如果有空格,需要用单或双引号
    return code url; #返回给客户端的URL地址    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    
    server { 
        listen 80;
        server_name www.kgc.com;
        root /data/nginx/pc/;
    	location /{
            root /data/nginx/pc/;
    
    }
      location /test {      #访问test 直接返回403
    	return 403;           
      }
    }
    
    
    server { 
        listen 80;
        server_name www.kgc.com;
        root /data/nginx/pc/;
    	location /{
            root /data/nginx/pc/;
    }
    location /test {                #访问test 直接返回403
    	return 666 "hello";         #可以改成666自定义,hello是描述 文字可以  图形浏览器不可以    
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    set指令

    指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key,另外set定义格式为set $key value,value可以是text, variables和两者的组合。

    location /main {
       root /data/nginx/html/pc;
       index index.html;
       default_type text/html;
        set $name kgc;
        echo $name;
        set $my_port $server_port;
        # $server_port 服务端口号
        echo $my_port;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    break指令

    用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的 ngx_http_rewrite_module 模块中指令就不再执行

    注意: 如果break指令在location块中后续指令还会继续执行,只是不执行 ngx_http_rewrite_module 模块的指令,其它指令还会执行

    if ($slow) {
       limit_rate 10k;
       break;
    }
    location /main {
       root /data/nginx/html/pc;
       index index.html;
       default_type text/html;
        set $name kgc;
        echo $name;
       break;  #location块中break后面指令还会执行
        set $my_port $server_port;
        echo $my_port;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    防盗链

    防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链

    基本语法
    valid_referers none | blocked | server_names | string ...;
    
    none:表示接受没有Referer字段的HTTP请求访问。
    blocked:表示允许http://或https//以外的请求访问。
    server_names:资源的白名单,这里可以指定允许访问的域名。
    string:可自定义字符串,支配通配符、正则表达式写法
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    实现盗链
    192.168.65.102
    yum install nginx
    cd /usr/share/nginx/html
    vim  test.html
    <html>
    <body>
        <h1>this is AG</h1>
    <img src="http://192.168.65.104/a.jpg"/>#有图片的服务器名称
    </body>
    </html>
    systemctl start nginx  
    输入http://192.168.65.102/test.html 可以看到另一台页面的图片
    
    实现防盗链
    location ~* \.(jpg|gif|swf)$ {            
             valid_referers none blocked *.test.com test.com; #如果不是test.com 的访问将返回403页面
             if ( $invalid_referer ) {
               return 403;
               }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

  • 相关阅读:
    2022年湖北劳务资质如何办理?劳务资质不分等级
    2023C语言暑假作业day6
    异步FIFO设计的仿真与综合技术(4)
    【牛客网面试必刷TOP101】二叉树篇(二)
    BLEMotion-Kit 开发环境搭建&评估板程序下载
    汽车tbox车联网系统终端
    BM6 判断链表中是否有环——Java Set集合&&hashSet哈希表应用
    线程协作(生产者消费者模式)
    linux文件相关命令
    vue3使用elementPlus进行table合并处理
  • 原文地址:https://blog.csdn.net/Meng2453508284/article/details/133275755