新浪SAE下 riwrite重写规则详解

AppConfig模块上线,Rewrite和压缩页面功能可以用了

  • 03/19. 2010

AppConfig能做哪些事情?

AppConfig模块负责提供用户自定义web服务器配置的能力,目前可以自行配置的功能包括

  • 目录默认页面

  • 自定义错误页面

  • 压缩

  • 页面重定向

  • 页面过期

  • 设置响应Header的Content-Type

在哪里设置AppConfig选项?

在每个版本的目录下边,有一个叫做config.yaml的文件,只需要在里边追加handel段(绿色部分)即可,AppConfig采用基于yaml的自定义语法.
这里是来自sinat.sinaapp.com的一个例子:

name: sinat version: 1 handle:  - rewrite: if(!is_dir() && !is_file()) goto "index.php?%{QUERY_STRING}" #这里开头有两个空格

1

2

3

4

name: sinat

version: 1

handle:

  - rewrite: if(!is_dir() && !is_file()) goto "index.php?%{QUERY_STRING}" #这里开头有两个空格

特别需要注意的是yaml里边的前置空格不能用TAB代替,否则会提示语法错误.

语法说明

AppConfig的语法分两种,一种是简单的参数罗列方式,一种是灵活的表达式语法,不同的功能会用到不同的类型的语法.

参数方式

目录默认页面

- directoryindex: file_list
file_list 中各个文件名以空格分隔,directoryindex 在 yaml 文件中仅有一项

例子:
- directoryindex: aaa.php bbb.html

自定义错误页面

- errordoc: httpcode error_file
httpcode 是诸如 404,302 之类的http响应码,error_file 是服务器以 httpcode 响应请求时响应的文件。errordoc 在 yaml 中可以配置多项。
- errordoc: 404 /path/404.html
- errordoc: 403 /path/403.html

表达式语法

其他功能需要用到表达式语法.其形式为:

if (expression) do_something

expression 有如下形式

  • in_header["header_name"] op string_or_digit

  • out_header["header_name"] op string_or_digit

  • path op string

  • query_string op string

  • is_file()

  • is_dir()

关于以上形式说明如下:

  • in_header 是请求 header,out_header 是响应 header,header_name 是 header 的名字

  • op 是操作符,有 ~(正则匹配) !~(正则不匹配) ==(相等,用于字符串和数字) !=(不相等,用于字符串和数字) > >= < <=(比较操作符仅用于整形数字)

  • string 是形如 “xxxx” 的字符串

  • string_or_digit 表示 string 或者 digit,根据 op 的种类,后面跟 string 或者 digit

  • path 是系统宏,表示用户请求的 url 去掉主机部分和查询串后剩下的部分

  • query_string 是系统宏,表示查询串

  • is_file() 和 is_dir 是系统函数,判断 path 是文件还是目录,!is_file(),!is_dir() 分别是其否定形式。

表达式语法用于以下功能.

压缩

- compress: if (single_express) compress
在 compress 中 single_express 表示单一的表达式,不能用 && 做复合,in_header,out_header,path 都可以出现在 single_express 中

例如:
- compress: if(out_header["Conteng-Length"] >= 500) compress
- compress: if(in_header["Referer"] == “gphone”) compress
- compress: if(path ~ “/big/”) compress

URL重写

- rewrite: if (complex_express) goto target_url

在 rewrite 中,complex_express 可以用 && 连接,组成复合表达式。除 out_header (没办法根据响应 header 做重定向) 外都可以出现在 rewrite  的 if 中,并且 path 只能出现一个(如果有多个,只有最后一个生效,其它被忽略),当省略 path 时,表示任意请求。
target_url 表示重定向的目标url,在target_url 可以以 $N 的形式表示 path 中匹配到的内容,%N 的形式表示最后一个query_string 中匹配到的内容,因为query_string 可以在 if 中出现多次,以%{QUERY_STRING} 表示查询串。

例如:
- rewrite: if(query_string ~ “^(so)$” && path ~ “zhaochou$”) goto “/url/%1″
- rewrite: if(is_dir( ) && path ~ “urldir/(.*)”) goto “/url/$1″
- rewrite: if( !is_file() && !is_dir()) goto “index.php?%{QUERY_STRING}”

指定过期时间和头信息

- expire: if (single_express) time seconds
- mime: if (single_express) type content-type
在 expire 和 mime 中 single_express 表示单一的表达式,不能用 && 复合,in_header,path 都可以出现在 single_express 中,并且 op 只能是 ~ 或者 ==,即只支持正则匹配和字符串比较
seconds 是秒数,content-type 是表示文档类型的字符串。

例如:
- expire: if(in_header["referer"] ~ “sina”) time 10
- mime: if(path ~ “\.pdf2$”) type “application/pdf”

更多的例子

为方便大家使用,zhiyong同学已经为大家把常用的语法写了示范.

目录默认页面

当访问url没有指定文件时,返回aaa.php,如果其不存在,则返回bbb.html

- directoryindex: aaa.php bbb.html

自定义错误页面

遇到 404 错误,返回 /path/404.html 文件。遇到 403 错误,返回 /path/404.html 文件

- errordoc: 404 /path/404.html
- errordoc: 403 /path/403.html

压缩

当页面内容大于 500 byte 时压缩
- compress: if(out_header["Conteng-Length"] >= 500) compress

当请求 header Content-Type 中包含 text 时压缩
- compress: if(out_header["Content-Type"] ~ “text”) compress

当响应 header Referer 等于 gphone 时压缩
- compress: if(in_header["Referer"] == “gphone”) compress

当请求的 url 包含“/big/” 时压缩
- compress: if(path ~ “/big/”) compress

注:对所有的压缩,请求 header Accept-Encoding 包含 gzip,deflate 是题中之意。

页面重定向

当 url 匹配 urldir/(.*) ,并且 输入 header referer 等于 sina 时,跳转至页面 /usr/$1,$1 表示刚刚匹配的 urldir/(.*) 中的 (.*) 部分。
- rewrite: if (path ~ “urldir/(.*)” && in_header["referer"] == “sina”) goto “/url/$1″

当 url 匹配 urldir/(.*),并且请求的是一个目录时,跳转至 /url/$1
- rewrite: if(is_dir( ) && path ~ “urldir/(.*)”) goto “/url/$1″

当 url 匹配 path,并且请求的不是一个文件时,跳转至 /url/query.php
- rewrite: if(! is_file() && path ~ “path”) goto “/url/query.php”

当查询串等于so,并且 url 以 zhaochou 结尾时,跳转至 /url/%1,%1 表示 query_string 匹配到的部分。
- rewrite: if(query_string ~ “^(so)$” && path ~ “zhaochou$”) goto “/url/%1″

当查询串不包含sohu,并且 url 以 zhaochou 结尾时,跳转至 /url/query.php?%{QUERY_STRING},%{QUERY_STRING} 表示查询串。
- rewrite: if(query_string !~ “sohu” && path ~ “zhaochou$”) goto “/url/query.php?${QUERY_STRING}”

如果 url 既不是文件,也不是目录,跳转至 index.php?%{QUERY_STRING}
- rewrite: if( !is_file() && !is_dir()) goto “index.php?%{QUERY_STRING}”

设置响应头的mime类型

如果 url 请求文件的扩展名是 pdf2,设置 Content-Type 为 application/pdf
- mime: if(path ~ “\.pdf2$”) type “application/pdf”

只要请求 header referer 包含字符串 sina,就设置 Content-Type 为 text/plain
- mime: if(in_header["referer"] ~ “sina”) type “text/plain”

页面过期

如果请求 header Referer 包含 字符串sina,设置过期时间10s
- expire: if(in_header["referer"] ~ “sina”) time 10

如果 url 以 lib\.js 结尾,设置过期时间100s
- expire: if(path ~ “lib\.js$”) time 100


转载于:https://my.oschina.net/u/932455/blog/395147

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值