【Nginx】 location rewrite 调试神器

2021-03-19  本文已影响0人  64aee3e008b3

rewrite_log指令

由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南

语法: rewrite_log on | off;

默认值: rewrite_log off;

配置段: http, server, location, if

启用时将在error log中记录notice级别的重写日志。

error_log指令

语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];

默认值: error_log logs/error.log error;

配置段: main, http, server, location

配置错误日志。

调试nginx中rewrite跳转神器:

开启rewrite_log开关,并配置error_log,格式如下,注意最后是:notice级别

rewrite_log on;

error_log /home/work/logs/nginx/zy_test_nginx_error.log notice;

例如:nginx配置文件部分配置如下:

error_log /home/work/logs/nginx/zy_test_nginx_error.log notice;

    rewrite /1.html /3.html last;

    rewrite /b.html /2.html last;

    location /2.html

    {

        rewrite /3.html /a.html last;

        rewrite /2.html /1.html last;

        return 444;

    }

访问url:http://testnginx.fzsf.com/b.html

error_log中的日志如下:

2021/03/19 09:45:03 [notice] 11139#0: *2601235 "/1.html" does not match "/b.html", client: 172.19.36.61, server: testnginx.fzsf.com, request: "GET /b.html HTTP/1.1", host: "testnginx.fzsf.com"

2021/03/19 09:45:03 [notice] 11139#0: *2601235 "/b.html" matches "/b.html", client: 172.19.36.61, server: testnginx.fzsf.com, request: "GET /b.html HTTP/1.1", host: "testnginx.fzsf.com"

2021/03/19 09:45:03 [notice] 11139#0: *2601235 rewritten data: "/2.html", args: "", client: 172.19.36.61, server: testnginx.fzsf.com, request: "GET /b.html HTTP/1.1", host: "testnginx.fzsf.com"

2021/03/19 09:45:03 [notice] 11139#0: *2601235 "/3.html" does not match "/2.html", client: 172.19.36.61, server: testnginx.fzsf.com, request: "GET /b.html HTTP/1.1", host: "testnginx.fzsf.com"

2021/03/19 09:45:03 [notice] 11139#0: *2601235 "/2.html" matches "/2.html", client: 172.19.36.61, server: testnginx.fzsf.com, request: "GET /b.html HTTP/1.1", host: "testnginx.fzsf.com"

2021/03/19 09:45:03 [notice] 11139#0: *2601235 rewritten data: "/1.html", args: "", client: 172.19.36.61, server: testnginx.fzsf.com, request: "GET /b.html HTTP/1.1", host: "testnginx.fzsf.com"

从上述日志记录可见,b.html匹配规则:

rewrite /b.html /2.html last; 

重写至2.html,继续匹配location /2.html  中的 第二条rewrite规则:rewrite /2.html /1.html last;

重写至1.html,由于结尾是last,因此,并没有返回444,而是最终返回1.html中的信息

上一篇下一篇

猜你喜欢

热点阅读