URL 重写上的 nginx 正则表达式引擎不支持波浪括号?

nginx regex engine on URL rewrite doesn't support squiggly brackets?

如果我尝试使用波浪括号,我的 nginx 配置似乎无法通过语法检查,即使 this answer implies that nginx uses PECR, and this fiddle 显示波浪括号在 PECR 下工作(尽管它确实说 PECR PHP括号内的...)。

所以,如果我把它放在我的服务器 > 位置块下:

location / {
    rewrite ^/([0-9]{4})$ /page.php;
}

我从 nginx 的语法检查器中得到以下错误:

nginx: [emerg] directive "rewrite" is not terminated by ";" in /etc/nginx/sites-enabled/default:37

nginx: configuration file /etc/nginx/nginx.conf test failed

如果我只去掉波浪形的部分,留下它:

location / {
    rewrite ^/([0-9])$ /page.php;
}

那么一切就OK了:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

那么,这里到底出了什么问题,如何让我的 URL-rewriter 接受 URL 重写规则中的 4 位数字?

来自 manual page for rewrite:

If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.

};字符在Nginx配置文件中有特殊含义,所以在正则表达式中使用需要用引号括起来保护。

同样适用于出现在 locationifmap 指令(以及我可能忘记的任何其他指令)中的正则表达式。

例如:

rewrite "^/[0-9]{4}$" /page.php last;