Pēteris Caune
Pēteris Caune

Reputation: 45212

CORS on DotCloud, 411 Length required

tl;dr: how do I work around "411 Length required" errors from nginx running on DotCloud?

I've got an API with CORS support deployed as Python service on DotCloud platform. When my javascript client tries to access it, browser starts with OPTIONS request, but gets back 411.

It appears nginx on DotCloud doesn't like HTTP requests with empty body. I've seen suggestions to add "Content-Length: 0" header, or try to use chunkin module, but I cannot do either:

Any ideas how to work around this problem?

Update:

Putting the following in nginx.conf solves my immediate problem. Similarly to chunkin, it traps 411 errors and returns canned response if request method is OPTIONS. Came across it 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;
}

It's not ideal as I'd like to handle these responses in Python code not nginx configuration. And I expect problems down the road with DELETE and HEAD requests--these don't have request body either.

Upvotes: 1

Views: 958

Answers (1)

3on
3on

Reputation: 6339

Another way to go around this issue is to not use nginx and use gunicorn instead.
Here is an example of a dotcloud.yml for such a configuration:

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

Upvotes: 0

Related Questions