squeaky-pl / japronto

Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.
MIT License
8.61k stars 581 forks source link

request.body is deleted when the application is behind nginx proxy #62

Closed ardilgulez closed 7 years ago

ardilgulez commented 7 years ago

Hey,

I have the simplest possible application. The code is:

from japronto import Application

async def request_handler(request):
    text = """Body related properties:
      Mime type: {0.mime_type}
      Encoding: {0.encoding}
      Body: {0.body}
      Text: {0.text}
      Form parameters: {0.form}
      Files: {0.files}
    """.format(request)
    return request.Response(
        mime_type="application/json", text=text)

app = Application()
app.router.add_route('/', request_handler)
app.run(debug=True, port=8088)

When I send a POST request with body to localhost:8088, the body is correctly parsed. But when I send a POST request to localhost/application (which is an nginx proxy), what I get is:

Body related properties:
      Mime type: None
      Encoding: None
      Body: None
      Text: None
      Form parameters: None
      Files: None

What can I do about this?

Thanks in advance.

squeaky-pl commented 7 years ago

Can you double check your nginx configuration, also check the actual HTTP verb that nginx sends to Japronto. Maybe you are doing some redirection in NGINX that turns your POST to GET.

ardilgulez commented 7 years ago

I don't do http method conversion in my nginx. In fact, I'm using the same nginx configuration for a handful of other applications, all of which handle POST requests, I encountered this type error with no other one of them.

Anyway, I found what was causing this: When I redirect from nginx as:

location /application {
    proxy_pass http://127.0.0.1:8088
}

It fails to route to app.router.add_route('/', request_handler)

But when I switch my nginx routing to:

location /application {
    proxy_pass http://127.0.0.1:8088/application
}

and handle that in my japronto app as app.router.add_route('/application', request_handler), suddenly it's all fine.

squeaky-pl commented 7 years ago

if you want the original behaviour without changing routes you need to use terminating slashes explicitely:

location /application/ {
    proxy_pass http://127.0.0.1:8088/
}

Note the terminating / in location and proxy_pass