nginx rewrite Issue,为什么它只替换一个匹配的字符串?

nginx rewrite Issue, why does it just replaces one matched string?

我想通过 Nginx 的重写指令替换我的 url。比如客户端请求http://127.0.0.1/user/user_id/, and I want to let Nginx rewrite the url to http://127.0.0.1/person/person_id/.

我的Nginx配置是这样的: rewrite (.*)user(.*) person;

但我发现 Nginx 将 url 更改为 .../user/person_id/

有人可以告诉我如何通过重写指令将 user 更改为 person 吗?

好吧,你说的这个案例可以很简单地解决:

rewrite ^/user/user_id/(.*)$ /person/person_id/ ;

假设userperson的第一个实例是常量,并且第二项后面总是有一个斜线,你可以尝试:

rewrite ^/user/user_([^/]+)/(.*)$ /person/person_/ ;