proxy_intercept_errors

The `proxy_intercept_errors` directive is used to control whether NGINX intercepts errors from proxied servers. — NGINX HTTP Core

proxy_intercept_errors
httpserverlocation
语法proxy_intercept_errors on | off;
默认值off
上下文http, server, location
模块NGINX HTTP Core
参数flag

说明

`proxy_intercept_errors` 指令在 NGINX 中允许用户指定服务器是否应该处理来自被代理服务器的错误响应。当该指令被设置为 `on` 时,NGINX 会拦截表示错误的 HTTP 响应(例如 404、500 等),并根据配置的错误处理设置进行处理。相反,如果将该指令设置为 `off`,从被代理服务器收到的任何错误响应将直接传递给客户端,不会被 NGINX 修改或处理。这对于实现自定义错误页面或根据特定错误重定向客户端很有用。 该指令可以在 `http`、`server` 或 `location` 上下文中设置,使其在各种配置场景中都很灵活。需要注意的是,将此指令设置为 `on` 可能会导致与预期不同的行为,因为客户端将看不到被代理服务器的原始响应,除非你专门配置了错误处理。根据你的应用架构以及你希望如何向用户呈现错误,正确设置此指令可能非常重要。

配置示例

location /api {
    proxy_pass http://backend;
    proxy_intercept_errors on;
    error_page 404 /custom_404.html;
}

如果已将 `proxy_intercept_errors` 设置为 `on`,请确保已定义自定义错误页面,以避免提供默认错误页面。

启用时,请确保您应用程序的错误处理策略与 NGINX 处理错误的方式一致。