intercepting upstream error with nginx

2019-02-28 00:12发布

My app runs with nginx and uwsgi (python). My aim is to drop a connection (as explained 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.

1条回答
你好瞎i
2楼-- · 2019-02-28 01:07

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;
}
查看更多
登录 后发表回答