Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a POST request to an external URL from a django + gunicorn + nginx setup

I am sending a post request from a method inside a web application running on django+nginx+gunicorn. I have no issues receiving 200 response from the same code when executed on django's own server (using runserver).

try:
    response = requests.post(post_url, data=some_data)
    if response.status_code == OK and response.content == '':
        logger.info("Request successful")
    else:
        logger.info("Request failed with response({}): {}".format(response.status_code, response.content))
        return response.status_code == OK and response.content == ''
except requests.RequestException as e:
    logger.info("Request failed with exception: {}".format(e.message))
    return False

I checked the server logs at post_url, it is indeed returning 200 response with this data. However, when I run the app behind gunicorn and nginx, I am not able to receive the response, (however the request is being sent). The code gets stuck at the first line after the try block, and gunicorn worker times out (after 30 seconds).

This is the apache server log at the post_url:

[14/Sep/2016:13:19:20 +0000] "POST POST_URL_PATH HTTP/1.0" 200 295 "-" "python-requests/2.9.1"

UPDATE:

I forgot to mention, this request takes less than a second to execute, so it is not a timeout issue. Something is wrong with the configuration? I have the standard nginx+gunicorn setup, where gunicorn is set as the proxy_pass in nginx. I am guessing since I am behind a nginx proxy, should I be doing something different while sending a post request from the application?

like image 539
SeeknInspYre Avatar asked Sep 14 '16 13:09

SeeknInspYre


1 Answers

In my gunicorn settings, setting workers=2 solved this issue.

When I was sending a request to the external URL, the external application would send a request back. This new request would occupy the one and only worker in the application. The original request that I sent out is workerless, and so it get's stuck. With 2 workers, I am able to simultaneously send out a request and receive another request.

like image 156
SeeknInspYre Avatar answered Sep 20 '22 07:09

SeeknInspYre