【问题标题】:CORS on DotCloud, 411 Length requiredDotCloud 上的 CORS,需要 411 长度
【发布时间】:2012-11-06 13:19:41
【问题描述】:

tl;dr:如何解决在 DotCloud 上运行的 nginx 出现“411 Length required”错误?

我在 DotCloud 平台上部署了一个支持 CORS 的 API 作为 Python 服务。当我的 javascript 客户端尝试访问它时,浏览器以 OPTIONS 请求开始,但返回 411。

DotCloud 上的 nginx 似乎不喜欢带有空正文的 HTTP 请求。我已经看到添加“Content-Length: 0”标头的建议,或者尝试使用chunkin 模块,但我不能这样做:

  • 我不控制浏览器为其 OPTIONS 请求添加的 HTTP 请求标头
  • 我认为我不能在 DotCloud 上安装 3rd 方 nginx 模块。即使我可以,这也可能无济于事,因为请求中没有“Transfer-encoding: chunked”标头

任何想法如何解决这个问题?

更新:

将以下内容放入nginx.conf 解决了我的直接问题。与 chunkin 类似,如果请求方法为 OPTIONS,它会捕获 411 错误并返回预设响应。遇到了in this repo

error_page 411 = @cors;
location @cors {
  if ($request_method = OPTIONS) {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'Content-Type, Authorization, ...';
    add_header Access-Control-Max-Age '1800';
    add_header Content-Length 0;
    add_header Content-Type text/plain;
    return 200;
  }
  return 411;
}

这并不理想,因为我想在 Python 代码而不是 nginx 配置中处理这些响应。而且我预计 DELETE 和 HEAD 请求会出现问题——这些请求也没有请求正文。

【问题讨论】:

  • 我认为你应该尝试一下chunkin。它只是处理 411 错误,这正是您所需要的。
  • 不幸的是,dotcloud 上没有 chunkin...
  • dotCloud 工程师在这里——我们正在考虑一种潜在的解决方法,允许在上游级别将分块请求转换为非分块请求。不能给出预计到达时间,但我们肯定在考虑;所以最终问题应该消失,您将能够摆脱额外的 Nginx 配置! (或者,我们可能会在所有与 Nginx 相关的服务中启用 chunkin...)

标签: nginx chunked-encoding dotcloud http-status-code-411


【解决方案1】:

解决此问题的另一种方法是不使用 nginx 而是使用 gunicorn。
以下是此类配置的 dotcloud.yml 示例:

www:
    type: python-worker
    config:
        python_version: v2.7
    processes:
        api: gunicorn -b 0.0.0.0:$PORT_WWW -w 8 wsgi:app
    ports:
        www: http

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多