在給大家講述這個問題之前,先給大家看一段nginx配置. 我們用到了 set-misc-nginx-module
location /test/ {
default_type text/html;
set_md5 $hash "secret"$remote_addr;
echo $hash;
}
這樣輸出來的內(nèi)容,可能是下面這樣的
202cb962ac59075b964b07152d234b70
但如果我們要截取某幾位字符怎么辦呢?
首先大家想到的肯定是使用模塊來實現(xiàn), 但只能這樣嗎? 有沒有更方便的方式呢?
有的.
我們可以巧妙地使用if + 正則表達式來實現(xiàn)這個小需求:
location /test/ {
default_type text/html;
set_md5 $hash "secret"$remote_addr;
if ( $hash ~ ^[\w][\w][\w][\w][\w][\w][\w][\w]([\w][\w][\w][\w][\w][\w][\w][\w]) ) {
set $hash $1;
}
echo $hash;
}
訪問/test/輸出的就是:
ac59075b