Nginx 伪静态与 rewrite 规则详解

伪静态让动态 URL 看起来像静态页面,Nginx 里靠 rewrite 与 location 配合实现。

执行顺序

  1. server 块的 rewrite 先于 location 匹配
  2. location 内部按顺序匹配正则
  3. rewrite 的 last 重新匹配 location,break 停止处理——写错会 500 循环

常用示例

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

这是 Laravel/ThinkPHP/WordPress 通用的「全转发」写法。

调试

开 error_log 的 notice 级别能看到 rewrite 决策;改完务必 nginx -t 再 reload。

相关文章