Nginx 根据上游响应代码返回 444

Mar*_*ark 5 nginx

我有 nginx 设置可以使用代理传递传递给上游。上游被写入以在某些请求上返回 502 http 响应,而不是返回 502 和所有标头,我希望 nginx 识别它并返回 444,因此没有返回任何内容。这可能吗?

我还尝试在任何 50x 错误时返回 444,但它也不起作用。

location / {
    return 444;
}


location ^~ /service/v1/ {
    proxy_pass http://127.0.0.1:3333;
    proxy_next_upstream error timeout http_502;
    error_page  500 502 503 504  /50x.html;
}

location = /50x.html {
    return 444;
}

error_page  404              /404.html;
location = /404.html {
    return 444;
}
Run Code Online (Sandbox Code Playgroud)

Pot*_*thu 2

对的,这是可能的。根据error_page的文档,可以这样做......

\n\n

error_page \xc2\xa0500 502 503 504 =444\xc2\xa0/50x.html;

\n\n

以同样的方式...

\n\n

error_page 404 =444 /404.html;

\n\n

因此,您的整个示例配置将变成......

\n\n
location / {\n return 444;\n}\n\nlocation ^~ /service/v1/ {\n proxy_pass http://127.0.0.1:3333;\n proxy_next_upstream 错误超时 http_502;\n error_page 500 502 503 504 =444 /50x.html;\n}\n\nerror_page 404 =444 /404.html;\n
\n