【问题标题】:intercepting upstream error with nginx使用 nginx 拦截上游错误
【发布时间】:2015-06-03 08:41:59
【问题描述】:

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

是否有 nginx 参数来“拦截”类似于proxy_intercept_errors 的上游错误?

根据this answer

我的 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(或任何其他错误代码,显然)它会直接传递给客户。

This answer 建议使用proxy_next_upstream,但这意味着使用proxy_pass 而不是uwsgi_pass。我需要使用uwsgi_pass,因为我需要将某些参数传递给 python 应用程序。

【问题讨论】:

    标签: nginx uwsgi http-error


    【解决方案1】:

    事实证明,答案比我想象的要简单 - 使用 uwsgi_intercept_errors 指令。

    首先,从上游应用程序(在我的例子中是 python)返回 444

    其次,配置nginx如下:

    location / {
        uwsgi_pass                myapp;
        include                   uwsgi_params;
        [...]
    
        uwsgi_intercept_errors  on;
        error_page 444 @drop;
    }
    
    location @drop {
        return 444;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      相关资源
      最近更新 更多