如何调试nginx的rewrite规则

如何调试nginx的rewrite规则?

设置 rewrite_log on;

并且将error_log 的报错等级改成 notice;

官网对 rewrite_log 的解释:

Syntax: rewrite_log on | off;
Default:
rewrite_log off;
Context: http, server, location, if
Enables or disables logging of ngx_http_rewrite_module module directives processing results into the error_log at the notice level.

可以看到,这个指令只能在 http server location if 配置指令中使用,日志等级是 notice 写入到 error_log 配置的文件中。

示例

url: /q?someparams

2021/04/22 18:21:28 [notice] 18131#0: *102982 "^/+m/question(.*)?qid=\d+(.*)?$" does not match ...
... 中间省略一些日志
2021/04/22 18:21:28 [notice] 18131#0: *102982 "^/+question/(\w+.*?)$" does not match ...
2021/04/22 18:21:28 [notice] 18131#0: *102982 "^/+q\?(.*)?tn=nsatom_qb_main(.*)?$" matches ...
2021/04/22 18:21:28 [notice] 18131#0: *102982 "^/+q" matches ...
2021/04/22 18:21:28 [notice] 18131#0: *102982 rewritten data: "/q/q/q/q/q"

从上面的日志中可以看出,当触发了重写规则时,会有关键字 matches 否则会有 does not match

最终重写完成之后,访问的地址会有标记:rewritten data

这里就是 /q/q/q/q/q

参考

best way to debug nginx rewrite rules in config file?

nginx官方文档:Module ngx_http_rewrite_module

posted @   吴丹阳-V  阅读(387)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2020-04-23 使用 MWeb + Typora 写作并发布到博客园【过时】
点击右上角即可分享
微信分享提示