在給你們講述這個問題以前,先給你們看一段nginx配置. 咱們用到了 set-misc-nginx-modulehtml
nginxlocation /test/ { default_type text/html; set_md5 $hash "secret"$remote_addr; echo $hash; }
這樣輸出來的內容,多是下面這樣的nginx
202cb962ac59075b964b07152d234b70
但若是咱們要截取某幾位字符怎麼辦呢?
首先你們想到的確定是使用模塊來實現, 但只能這樣嗎? 有沒有更方便的方式呢?git
有的.
咱們能夠巧妙地使用if + 正則表達式來實現這個小需求:github
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