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

404 rewrite or redirect nginx path

· 52CE 我爱测

Im having problems with redirect on nginx I have this original URL: https://www.example.com/media/wysiwyg/logo.png And i'm using the Cloudflare Image, i can use this to load by Cloudflare Image: https://www.example.com/cdn-cgi/im…

问题原文

原帖链接 · user2925795 · CC BY-SA 4.0

Im having problems with redirect on nginx

I have this original URL:

https://www.example.com/media/wysiwyg/logo.png

And i'm using the Cloudflare Image, i can use this to load by Cloudflare Image:

https://www.example.com/cdn-cgi/image/onerror=redirect/media/wysiwyg/logo3.png 

But i need this dynamic, like:

Everything in this folder: "media/wysiwyg/" will redirected to:

https://www.example.com/cdn-cgi/image/format=auto,onerror=redirect/media/wysiwyg/logo.png

logo.png would be dynamic, BUT MUST BE AN IMAGE (JPEG, GIF, PNG).

I tried this, but without success:

location ~ ^/media/wysiwyg/(.*)$ {
    rewrite    ^/media/wysiwyg/(.*)$ /cdn-cgi/image/format=auto,onerror=redirect/media/wysiwyg/$1 break;
}

I need, if i access this:

https://www.example.com/media/wysiwyg/logo.png

I need redirect to this (without change url):

https://www.example.com/cdn-cgi/image/format=auto,onerror=redirect/media/wysiwyg/logo.png

What's wrong?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

Change break to last.

Because the new URL will need to be processed by a different location, you need to have nginx restart processing for the new rewritten URL, which is only done when you use last in the rewrite rule.

See the docs for the complete explanation of how rewrite works.

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

资料来源与适用范围

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

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

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