用nginx拦截上游错误 [英] intercepting upstream error with nginx

查看:87
本文介绍了用nginx拦截上游错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用nginx和uwsgi(python)运行.我的目的是断开连接(如此处所述),例如当python应用决定这样做时.

是否存在nginx参数来拦截"类似于proxy_intercept_errors的上游错误?

根据此答案

我的nginx配置:

location / {
    uwsgi_pass                myapp;
    include                   uwsgi_params;

    uwsgi_buffering           on;
    uwsgi_buffer_size         8k;

    uwsgi_connect_timeout     800;

    uwsgi_read_timeout        800;
    uwsgi_send_timeout        800;

    proxy_intercept_errors  on;
    error_page 420 =444 @foo;
}

location @foo {
    return 444;
}

我尝试了可能想到的所有可能组合,包括/不带proxy_intercept_errorserror_page,但是无论我如何配置nginx,当我返回420 Enhance your calm(或任何其他错误代码,显然)时,它都会直接传递给客户.

此答案建议使用proxy_next_upstream,但这意味着使用proxy_pass而不是uwsgi_pass.我需要使用uwsgi_pass,因为我需要将某些参数传递给python应用.

解决方案

事实证明答案比我想象的要简单-使用here) e.g. when the python app decides to do so.

Is there a nginx parameter to "intercept" upstream errors similar to proxy_intercept_errors?

According to this answer,

My nginx config:

location / {
    uwsgi_pass                myapp;
    include                   uwsgi_params;

    uwsgi_buffering           on;
    uwsgi_buffer_size         8k;

    uwsgi_connect_timeout     800;

    uwsgi_read_timeout        800;
    uwsgi_send_timeout        800;

    proxy_intercept_errors  on;
    error_page 420 =444 @foo;
}

location @foo {
    return 444;
}

I tried all possible combinations I could think of with/without proxy_intercept_errors and error_page but no matter how I configure nginx, when I return 420 Enhance your calm (or any other error code, apparently) it gets passed directly to the client.

This answer suggests using proxy_next_upstream but that implies using proxy_pass rather than uwsgi_pass. I need to use uwsgi_pass because I need certain parameters passed to the python app.

解决方案

It turns out the answer is simpler than I thought - use the uwsgi_intercept_errors directive.

Firstly, from the upstream app (in my case python) return 444.

Secondly, configure nginx as follows:

location / {
    uwsgi_pass                myapp;
    include                   uwsgi_params;
    [...]

    uwsgi_intercept_errors  on;
    error_page 444 @drop;
}

location @drop {
    return 444;
}

这篇关于用nginx拦截上游错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆