zoukankan      html  css  js  c++  java
  • nginx利用Location,rewrite模块防盗链,禁止访问

    一:Location语法:
    location [=|~|~*|^~] /uri/ { … }
    注:
    1、~ 为区分大小写匹配
    2、~* 为不区分大小写匹配
    3、!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
    示例一:
    location / { }
    匹配任何查询,因为所有请求都以 / 开头。但是正则表达式规则将被优先和查询匹配。

    示例二:
    location =/ {}
    仅仅匹配/

    示例三:
    location ~* \.(gif|jpg|jpeg)$ {
    rewrite \.(gif|jpg)$ /logo.png;
    注:不区分大小写匹配任何以gif,jpg,jpeg结尾的文件

    二、ReWrite语法
    last – 基本上都用这个Flag。
    break – 中止Rewirte,不在继续匹配
    redirect – 返回临时重定向的HTTP状态302
    permanent – 返回永久重定向的HTTP状态301
    1、下面是可以用来判断的表达式:
    -f和!-f用来判断是否存在文件
    -d和!-d用来判断是否存在目录
    -e和!-e用来判断是否存在文件或目录
    -x和!-x用来判断文件是否可执行
    2、下面是可以用作判断的全局变量
    例:http://localhost:88/test1/test2/test.php
    $host:localhost
    $server_port:88
    $request_uri:http://localhost:88/test1/test2/test.php
    $document_uri:/test1/test2/test.php
    $document_root:D:\nginx/html
    $request_filename:D:\nginx/html/test1/test2/test.php
    三、Redirect语法
    server {
    listen 80;
    server_name start.igrow.cn;
    index index.html index.php;
    root html;
    if ($http_host !~ “^star\.igrow\.cnmce_markeramp;quot [点击图片可在新窗口打开] {
    rewrite ^(.*) http://star.igrow.cn$1 redirect;
    }
    }
    四、防盗链
    location ~* \.(gif|jpg|swf)$ {
    valid_referers none blocked start.igrow.cn sta.igrow.cn;
    if ($invalid_referer) {
    rewrite ^/ http://$host/logo.png;
    }
    }
    五、根据文件类型设置过期时间
    location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
    expires 1h;
    break;
    }
    }
    六、禁止访问某个目录
    location ~* \.(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
    }
  • 相关阅读:
    JDK安装,环境配置
    XSS攻击测试代码
    css3 input边框蓝光特效
    PDFobject插件使用,PDF在线查看插件
    网页中嵌入可以点击“运行代码”执行html/css/js代码
    SmohanTimeLine.js 酷炫的时间轴效果
    java进行文件上传,带进度条
    javascript读取xml文件
    C# 动态类型与动态编译简介
    Mac 命令行美化
  • 原文地址:https://www.cnblogs.com/buffer/p/2119242.html
Copyright © 2011-2022 走看看