大网数据广告活动
服务器运维问答

Internal Rewrite in Nginx not work, and redirected

· 52CE 我爱测

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, …

问题原文

原帖链接 · Ilham Sulaksono · CC BY-SA 3.0

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

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。