序
本文主要解析一下ngx_http_rewrite_module中的rewrite相關(guān)配置。
directives
| 名稱 | 默認(rèn)配置 | 作用域 | 官方說明 | 中文解讀 | 模塊 |
|---|---|---|---|---|---|
| break | 無 | server, location, if | Stops processing the current set of ngx_http_rewrite_module directives. | 中斷當(dāng)前的重寫 | ngx_http_rewrite_module |
| if | 無 | server, location | The specified condition is evaluated. If true, this module directives specified inside the braces are executed, and the request is assigned the configuration inside the if directive. Configurations inside the if directives are inherited from the previous configuration level. | if表達(dá)式成立則執(zhí)行,if代碼塊里頭的配置繼承外部的配置; false和0都表示不成立; string比較,相等用=,不相等用!= ; 文件存在用-f,不存在用! -f ; 目錄存在用-d,不存在用 ! -d ; 鏈接存在用-e,不存在用! -e ; 文件可執(zhí)行用-x,不可執(zhí)行用! -x | ngx_http_rewrite_module |
| return | 無,語法 return code [text]; return code URL; return URL; | server, location, if | Stops processing and returns the specified code to a client. The non-standard code 444 closes a connection without sending a response header. | 停止處理并返回,返回444的話則會(huì)關(guān)閉連接,連header都不會(huì)發(fā)送 | ngx_http_rewrite_module |
| rewrite | 無,語法 rewrite regex replacement [flag]; | server, location, if | If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configuration file. It is possible to terminate further processing of the directives using flags. If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client. | last停止匹配,使用重寫后的url進(jìn)行新的匹配; break停止當(dāng)前的url重寫; redirect進(jìn)行302跳轉(zhuǎn); permanent進(jìn)行301跳轉(zhuǎn) | ngx_http_rewrite_module |
| rewrite_log | rewrite_log off; | http, server, location, if | Enables or disables logging of ngx_http_rewrite_module module directives processing results into the error_log at the notice level. | 設(shè)置是否在error_log以notice級(jí)別開啟rewrite的log | ngx_http_rewrite_module |
| set | 無,語法 set $variable value; | server, location, if | Sets a value for the specified variable. The value can contain text, variables, and their combination. | 設(shè)置變量 | ngx_http_rewrite_module |
| uninitialized_variable_warn | uninitialized_variable_warn on; | http, server, location, if | Controls whether warnings about uninitialized variables are logged. | 設(shè)置是否打印使用了未初始化變量的log | ngx_http_rewrite_module |
實(shí)例
http {
# a special log format referencing variables we'll define later
log_format imagelog '[$time_local] ' $image_file '
'$image_type '
' $body_bytes_sent '
' $status;
# we want to enable rewrite-rule debugging to see if our rule does what we intend
rewrite_log on;
server {
root /home/www;
location / {
# we specify which logfile should receive the rewriteruledebug messages
error_log logs/rewrite.log notice;
# our rewrite rule, utilizing captures and positional variables
# note the quotes around the regular expression theseare required because we used {} within the expression itself
rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
# note that we didn't use the 'last' parameter above; if we had, the variables below would not be set because NGINX would have ended rewrite module processing here we set the variables that are used in the custom log format 'imagelog'
set $image_file $3;
set $image_type $4;
}
}
}