请教一个 nginx404 错误跳转的问题

2016-06-28 09:57:17 +08:00
 qcloud
配置了一个 nginx404 错误跳转到自定义 404 的页面,我是配置跳转到了一个网站,但是配置好之后, 404 并不能跳转到配置的 404 网站,而是无限重定向。。。
http://yun.redhatcn.cn/201606289551465025547.png ( nginx 的 conf 配置)
http://yun.redhatcn.cn/201606289595567895227.png (无限重定向)
847 次点击
所在节点    问与答
4 条回复
lhbc
2016-06-28 10:46:14 +08:00
error_page 404 =301 http://example.com/notfound.html;

另外, 404 跳转到其他域名,你确定找不到页面就直接把用户赶走?
qcloud
2016-06-28 10:52:16 +08:00
@lhbc 不是,我的为了截图故意搞得, V2 是全球第二大 DDCC 论坛,哈哈哈
lhbc
2016-06-28 11:03:18 +08:00
可以用以下指令
error_page 404 http://example.com/forbidden.html;
error_page 404 =301 http://example.com/notfound.html;
=后面可以用 301, 302, 303, 307

这样做,会产生一次跳转,非常不友好


最优雅的做法是这样
location / {
error_page 404 = @fallback;
}

location @fallback {
proxy_pass http://backend;
}

或者
error_page 404 /404.html;

location /404.html {
internal;
}

参考:
http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page
qcloud
2016-06-28 11:05:34 +08:00
@lhbc 非常感谢,按照你得方法已经成功了,谢谢了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/288758

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX