nginx rewrite重写规则与防盗链配置方法

时间:2016-02-04 15:16:58来源:网络
导读:nginx rewrite重写规则与防盗链配置方法,rewrite规则格式中flag标记的几种形式,盗链时返回403错误,允许的域名直接跟在第二行的域名后面。
 
nginx rewrite重写规则与防盗链配置方法

nginx rewite 规则,官方文档:http://wiki.nginx.org/NginxHttpRewriteModule

nginx rewrite规则格式:rewrite regex replacement flag

flag标记有四种格式:

last – 相当于Apache中的L
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302,相当于Apache中的R
permanent – 返回永久重定向的HTTP状态301,相当于Apache中的R=301
 

可以放在server, location 和 if 模块中。

匹配判断:
~  为区分大小写匹配; !~为区分大小写不匹配
~* 为不区分大小写匹配;!~为不区分大小写不匹配

例如,设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

if ($http_user_agent ~ MSIE) {
  rewrite  ^(.*)$  /msie/$1  break;
}

附,常用nginx Rewrite 规则配置代码。

1、只使用一个网址,比如主力网址设为www.xfcodes.com。

if ($host != 'www.xfcodes.com' ) {
rewrite ^/(.*)$ http://www.xfcodes.com/$1 permanent;
}
 

访问xfcodes.com时,会自动跳转到www.xfcodes.com。

2、防盗链

location ~* .(gif|jpg|png|swf|flv)$ {
valid_referers none blocked xfcodes.com dgjs123.com;
if ($invalid_referer) {
return 403;
}
}
 

盗链时则返回403错误,允许的域名可以直接跟在第二行的域名后面。

3、WordPress的Rewrite

location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
 

目前,代码收藏上就是使用的这段代码。

4.bo-blog在nginx下nginx rewrite 规则

if (!-e $request_filename) {
  rewrite ^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?entryid=$1&page=$2&part=$3 last;
  rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last;
  rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last;
  rewrite ^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=category_$1&mode=$2&page=$3 last;
  rewrite ^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
  rewrite ^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;
  rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;
  rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last;
  rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;
  rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;

#Force redirection for old rules
  rewrite ^/read\.php/([0-9]+)\.htm$ http://$host/post/$1/ permanent;
  rewrite ^/post/([0-9]+)\.htm$ http://$host/post/$1/ permanent;
  rewrite ^/post/([0-9]+)\_([0-9]+)\.htm$ http://$host/post/$1/$2/ permanent;
  rewrite ^/post/([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/post/$1/$2/$3/ permanent;
  rewrite ^/index\_([0-9]+)\_([0-9]+)\.htm$ http://$host/page/$1/$2/ permanent;
  rewrite ^/star\_([0-9]+)\_([0-9]+)\.htm$ http://$host/starred/$1/$2/ permanent;
  rewrite ^/category\_([0-9]+)\.htm$ http://$host/category/$1/ permanent;
  rewrite ^/category\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/category/$1/$2/$3/ permanent;
  rewrite ^/archive\_([0-9]+)\_([0-9]+)\.htm$ http://$host/archiver/$1/$2/ permanent;
  rewrite ^/archive\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/archiver/$1/$2/$3/$4/ permanent;
  rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/ permanent;
  rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/$4/$5/ permanent;

#Filename alias
  rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last;
}

nginx rewrite重写与防盗链配置的更多相关文章

  1. Nginx Rewrite相关功能-防盗链

    Nginx Rewrite相关功能-防盗链 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  2. Nginx缓存功能、防盗链、URL重写

    nginx做为反向代理时,能够将来自upstream的响应缓存至本地,并在后续的客户端请求同样内容时直接从本地构造响应报文. nginx的缓存数据结构: 共享内存:存储键和缓存对象元数据 磁盘空间:存 ...

  3. nginx实现防盗链配置方法介绍

    有些朋友觉得防盗链就是防止图片,其实有很多东西要进行防盗链了,下面我来介绍在nginx中实现防盗链配置方法有对图片防盗链与下载资源等. 防盗链配置 假设网站域名是 www.php100.com. 编辑 ...

  4. Nginx防盗链配置案例配置,Nginx的知识分享

    防盗链的含义:网站内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接在自己的有广告有利益的页面上向最终用户提供此内容. 常常是一些名不见经传的小网站来盗取一些有实力的大网站的地址 ...

  5. nginx防盗链配置

    Ps:防盗链的意义就是保证自己的版权,不免网站的流量流失,为他人做嫁衣.下面是网上看到的三种方法: 修改 /usr/local/nginx/conf/nginx.conf 这个配置文件.找到locat ...

  6. nginx的优化和防盗链

    nginx的优化和防盗链 目录 nginx的优化和防盗链 一.nginx的优化 1. 隐藏版本号 (1)隐藏版本号的原因 (2)查看版本号的方法 (3)隐藏方法一:修改配置文件 (4)隐藏方法二:修改 ...

  7. Nginx的优化与防盗链

    Nginx的优化与防盗链 1.隐藏版本号 2.修改用户与组 3.缓存时间 4.日志切割 5.连接超时 6.更改进程数 7.配置网页压缩 8.配置防盗链 9.fpm参数优化 1.隐藏版本号: 可以使用 ...

  8. Nginx详解十三:Nginx场景实践篇之防盗链

    防盗链: 目的:防止资源被盗用 防盗链设置思路 首要方式:区别哪些请求是非正常的用户请求 基于http_refer防盗链配置模块(判断refer(上一步的链接)信息是否为允许访问的网站) 配置语法:v ...

  9. Apache 防盗链配置

    盗链 网站内有许多的图片地址,或一些我们可以用到的资源,在这种情况下,我可以通过赋值其他图片链接地址,到我自己的平台上,这样相当于盗取了一张图片的链接,那么盗链会有什么危害呢? 当我们盗取一张图片链接 ...

随机推荐

  1. mobilebone.js使用笔记

    mobilebone.js主要用来是网页呈现单页效果,添加类似native app的页面切换效果.原理是:当打开a链接里的页面时,不再以传统的新页面打开,而是以ajax-html的方式,将新页面的内容 ...

  2. paip.最好的脚本语言node js 环境搭建连接mysql

    paip.最好的脚本语言node js 环境搭建连接mysql #====下载node...走十一个exe..容易的.. 1 #0----Hello world   .js 2 #---------模 ...

  3. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  4. android开源项目和框架

    特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...

  5. spring源码 — 一、IoC容器初始化

    IoC容器初始化 注意:本次的spring源码是基于3.1.1.release版本 容器:具有获取Bean功能--这是最基本功能,也是BeanFactory接口定义的主要行为,在添加了对于资源的支持之 ...

  6. C++类的底层机理

    我们首先从一个问题来阐明类的底层机理: 假如有一个类A,里面有一个成员函数get(),例如: class A { public:     void get(); } A a; 那么a.get()表示什 ...

  7. Andorid--java0

    java code: public class Hello{     public static void main(String argv[]) {        System.out.printl ...

  8. HTML5手机APP开发入(4)

    HTML5手机APP开发入(4) 课程内容 完成一个自定义的Component用来展现通讯录用户的明细信息如下图 http://bootsnipp.com/snippets/featured/prof ...

  9. zz Windows 10安装教程:硬盘安装Win10 系统步骤(适合32位和64位)

    Windows 10安装教程:硬盘安装Win10 系统步骤(适合32位和64位) Posted on 2015年01月28日 by 虾虾 22 Comments   最新的Windows 10 MSD ...

  10. 鸡和蛋的OO设计

    一个题目:用类图表示出鸡和蛋的关系. 第一版: 第二版: 一个鸡可以下N个蛋,一个蛋可以浮出0或者1个鸡. 问题是公鸡不会下单,第三版: