Internal Rewrite in Nginx not work, and redirected

Hi I want a rewrite rule, but i don't want to redirect it, and requesting one URL but load different content from another URL. So, I think last and break flag in rewrite directive is a good solution. But, it's always redirected, I search the problem and they state that if the new URL not from nginx instance it will redirect instead. Here several lines of my config:

...
rewrite ^(/\w+/$)(.+\.php) /$2 last;
proxy_pass upstream_name:80;
...

What i ask is how i can use rewrite but not redirected?

Update: For example, i want to make a new like /link/asd, but there is no /link/asd in my web application. I want my /link/asd link load resource from /asd link. then, i think the rewrite last flag is a good solution. If my understanding is false, please correct me and explain it. Thank you


If you want to rewrite exactly /link/example to /example, where example is anything, you can use this rewrite:

rewrite ^/link/(.+)$ /$1 last;

This will also match /link/example/something/else, as the .+ matches any characters before the end of the string ($). If you want to restrict which characters are allowed in the capture group, for example, exclude / from the capture group, one can use [^/]+ notation for the regex.