V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
qcloud
V2EX  ›  问与答

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

  •  
  •   qcloud · 2016-06-28 09:57:17 +08:00 · 826 次点击
    这是一个创建于 2930 天前的主题,其中的信息可能已经有所发展或是发生改变。
    配置了一个 nginx404 错误跳转到自定义 404 的页面,我是配置跳转到了一个网站,但是配置好之后, 404 并不能跳转到配置的 404 网站,而是无限重定向。。。
    http://yun.redhatcn.cn/201606289551465025547.png ( nginx 的 conf 配置)
    http://yun.redhatcn.cn/201606289595567895227.png (无限重定向)
    4 条回复    2016-06-28 11:05:34 +08:00
    lhbc
        1
    lhbc  
       2016-06-28 10:46:14 +08:00   ❤️ 1
    error_page 404 =301 http://example.com/notfound.html;

    另外, 404 跳转到其他域名,你确定找不到页面就直接把用户赶走?
    qcloud
        2
    qcloud  
    OP
       2016-06-28 10:52:16 +08:00
    @lhbc 不是,我的为了截图故意搞得, V2 是全球第二大 DDCC 论坛,哈哈哈
    lhbc
        3
    lhbc  
       2016-06-28 11:03:18 +08:00   ❤️ 2
    可以用以下指令
    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
        4
    qcloud  
    OP
       2016-06-28 11:05:34 +08:00
    @lhbc 非常感谢,按照你得方法已经成功了,谢谢了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2314 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:24 · PVG 20:24 · LAX 05:24 · JFK 08:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.