在配置Nginx的时候我们经常会用到rewrite指令将用户的请求重写到另一个地址,比如下面的配置
location / {
index index.html index.htm;
rewrite ^/doc/(.*) /document/$1 last;
rewrite ^/soft/(.*) /software/$1 last;
}
会将用户访问/doc/info时重写到/document/info;将用户访问/soft/info时,重写到/software/info;
那么我们如何通过日志能查看用户的请求URL匹配了哪一条rewrite规则呢?
第一步:打开rewrite_log
在http或server或location增加 rewrite_log on;
rewrite_log的日志级别为notice
http {
rewrite_log on;
}
第二步:修改error_log的日志级别为notice
error_log /var/log/nginx/error.log notice
验证
使用浏览器访问 /doc/info ,然后查看error.log日志文件的输出信息,会发现如下信息
"^/doc/(.*)" matches "/doc/info"
rewritten data: "/document/info",
第一行说明了:哪一条rewrite规则匹配了请求的URL
第二行说明了:将请求的URL重写后的地址是什么
本文暂时没有评论,来添加一个吧(●'◡'●)