nginx rewrite重写规则与防盗链配置方法教程详解
导读:nginxrewrite重写规则与防盗链配置方法,rewrite规则格式中flag标记的几种形式,盗链时返回403错误,允许的域名直接跟在第二行的域名后面。
nginxrewrite重写规则与防盗链配置方法如下所示:
nginxrewite规则,官方文档:http://wiki.nginx.org/NginxHttpRewriteModule
nginxrewrite规则格式:rewriteregexreplacementflag
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/$1break; }
附,常用nginxRewrite规则配置代码。
1、只使用一个网址,比如主力网址设为www.xfcodes.com。
if($host!='www.xfcodes.com'){ rewrite^/(.*)$http://www.xfcodes.com/$1permanent; }
访问xfcodes.com时,会自动跳转到www.xfcodes.com。
2、防盗链
location~*.(gif|jpg|png|swf|flv)${ valid_referersnoneblockedxfcodes.comdgjs123.com; if($invalid_referer){ return403; } }
盗链时则返回403错误,允许的域名可以直接跟在第二行的域名后面。
3、WordPress的Rewrite
location/{ indexindex.htmlindex.php; if(-f$request_filename/index.html){ rewrite(.*)$1/index.htmlbreak; } if(-f$request_filename/index.php){ rewrite(.*)$1/index.php; } if(!-f$request_filename){ rewrite(.*)/index.php; } }
目前,代码收藏上就是使用的这段代码。
4.bo-blog在nginx下nginxrewrite规则
if(!-e$request_filename){ rewrite^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$/read.php?entryid=$1&page=$2&part=$3last; rewrite^/page/([0-9]+)/([0-9]+)/?$/index.php?mode=$1&page=$2last; rewrite^/starred/([0-9]+)/?([0-9]+)?/?$/star.php?mode=$1&page=$2last; rewrite^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$/index.php?go=category_$1&mode=$2&page=$3last; rewrite^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$/index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4last; rewrite^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$/index.php?go=showday_$1-$2-$3&mode=$4&page=$5last; rewrite^/user/([0-9]+)/?$/view.php?go=user_$1last; rewrite^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$/tag.php?tag=$1&mode=$2&page=$3last; rewrite^/component/id/([0-9]+)/?$/page.php?pageid=$1last; rewrite^/component/([^/]+)/?$/page.php?pagealias=$1last; #Forceredirectionforoldrules 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; #Filenamealias rewrite^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$/read.php?blogalias=$1&page=$2&part=$3last; }
以上所述是小编给大家介绍的nginxrewrite重写规则与防盗链配置方法教程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!