【问题标题】:nginx fails to use proxy_pass when handling errornginx在处理错误时无法使用proxy_pass
【发布时间】:2019-01-07 03:23:45
【问题描述】:

我正在尝试配置 nginx 以提供来自 s3 存储桶的错误页面。

为此,我的配置如下所示:

location / {
    error_page 404 = @fallback;
}

location @fallback {
    rewrite ^ /my-s3-bucket/404.html;
    proxy_pass https://s3.ap-northeast-2.amazonaws.com;
}

我的期望是任何访问网站但未找到的内容都会被发送到@fallback 位置。然后我想用我的 404 页面的实际位置重写 URL 并发送到 s3 存储桶。我不想只是 302 重定向到 404 页面。

问题是 proxy_pass 指令没有被执行。相反,它只是在本地查找我重写的 URL。

在下面查看我的访问日志:

2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/sdfd" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/my-s3-bucket/404.html" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"

我向 www.dev.mywebsite.com.au/sdfd 提出请求,但未找到。 'sdfs' 被重写为 'my-s3-bucket/404.html' 但不是代理将其传递给https://s3.ap-northeast-2.amazonaws.com,而是在本地 /etc/nginx/html 目录中查找它。

我的nginx版本是1.15.2

【问题讨论】:

    标签: nginx


    【解决方案1】:

    如果您希望在同一 location 块内处理重写的 URI,请使用 rewrite...break。请参阅this document 了解更多信息。

    例如:

    location @fallback {
        rewrite ^ /error/404.html break;
        proxy_pass https://example.com;
    }
    

    【讨论】:

    • 谢谢! '休息;'是必需的。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    相关资源
    最近更新 更多