intercepting upstream error with nginx

删除回忆录丶 提交于 2019-12-02 09:23:44

问题


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:


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;
}


来源:https://stackoverflow.com/questions/29344414/intercepting-upstream-error-with-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!