Serving a Request from Gunicorn

Serving a request from gunicorn

looking at the quickstart guide, you probably should have run

(tutorial) $ ../bin/gunicorn -w 4 myapp:app

which should have produced a line that looks a bit like:

Listening at: http://127.0.0.1:8000

Among others. see if you can access your site at that address.

Also Note that 127.0.0.1 is the loopback address; accessible only from that host itself. To get gunicorn to bind to a different option, pass it --bind 0.0.0.0:80, as Jan-Philip suggests.

Since you mention rackspace, its possible that you may need to adjust the firewall settings to allow incoming connections to the desired ports.

I can't serve gunicorn requests from nginx

Here's a sample NGINX config which I know to work as a reverse proxy with Gunicorn.

#Config Contents
server {
listen 80;
server_name site.your.domain;
# Or use the following if you do not have a domain
#server_name 123.123.123.123;

location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
}
}

The Gunicorn Run string looks like

gunicorn --bind localhost:5000 flask_app:app

As a sanity check, you can check which processes are listening to which ports using

lsof -i -P -n | grep LISTEN

Make sure your server allows traffic on port 80 from at least your IP address.

Django-App with Nginx and Gunicorn - Requests get lost?

Thanks to the ideas from @SDRJ, I could solve this issue on my own.

Original server specs

  • VM with 2 CPUs
  • Nginx as reverse proxy
  • Gunicorn running with 3 workers and 1 thread per worker

Load test

Although a small amount of requests had a average runtime of 0.05 seconds, a load test reveiled that the server isn't able to process more than 200 requests at a time.
This resulted in Nginx signaling "Bad Request 502" for the further requests, an indication that Nginx can't reach Gunicorn anymore.

Optimised specs

Changing the configuration of gunicorn to:

  • 3 workers with 4 threads

This resulted in much better performance. The server was able to easily handle more than 4000 requests at a time.

Conclusion

Is it possible, that requests get lost when Nginx/Gunicorn is not able to handle the amount of requests?

Yes, it is. "Lost" might be an unfavorable description, but it can happen that some requests do not get processed.



Related Topics



Leave a reply



Submit