认识
文档:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
The ngx_http_rewrite_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.
性质
rewrite regex replacement [flag];
文档:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
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.
if
Module ngx_http_rewrite_module/if
If Is Evil
# 判断用户使用IE浏览器
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
# 判断referer是否合法
# 参考valid_referers指令
if ($invalid_referer) {
return 403;
}