Centos – gunicorn working vs uwsgi timeout

centosdjangogunicornpythonuwsgi

I've setup my Python / Django app on a vmware machine with CentOS, uwsgi and gunicorn along with all my apps dependencies.

After running my app with gunicorn with the following command:

gunicorn --workers=4 --bind=0.0.0.0:8081 wsgi:application

The application works like a charm and everything is going smoothly. However, i tried running it with uwsgi to compare performance (requests / second) on both. So i ran the following command:

sudo uwsgi --chdir=/var/www/pyapp/ --module=wsgi:application --env DJANGO_SETTINGS_MODULE=settings --socket=127.0.0.1:8081 --processes=5  --harakiri=20  --max-requests=5000  --vacuum --master --pidfile=/tmp/pyapp-master.pid

The first problem i encountered running this is the following error:

invalid request block size: 21573 (max 4096)...skip

After adding the -b 25000 to make the buffer larger than the maxmimum, i started encountering:

timeout. skip request.

I couldn't suspect that my app had something wrong with it as it ran with gunicorn without such problems.

Can someone help pointing out what i am doing here?

Thanks

Best Answer

Adding --protocol=http solved the problem. By default uwsgi works with wsgi protocol which doesn't accept http requests.

In addition, instead of using the reverse proxy, you have to use uwsgi mode

upstream myapp {
    server 127.0.0.1:8081;
}
server {
    listen 80;
    server_name apps.myapp.com apps-backend.myapp.com;

    root /www/python/apps/myapp/;

    access_log /var/log/nginx/apps.myapp.com.access.log;
    error_log /var/log/nginx/apps.myapp.com.error.log;

    # https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production
    location /static/ {
        alias /www/python/apps/myapp/static/;
        expires 30d;
    }

    location /media/ {
        alias /www/python/apps/myapp/media/;
        expires 30d;
    }

    location / {
        uwsgi_pass myapp;
        include uwsgi_params;
#        proxy_pass_header Server;
#        proxy_set_header Host $http_host;
#        proxy_redirect off;
#        proxy_set_header X-Real-IP $remote_addr;
#        proxy_set_header X-Scheme $scheme;
#        proxy_connect_timeout 50;
#        proxy_read_timeout 50;
#        proxy_pass http://localhost:8081/;
    }

    # what to serve if upstream is not available or crashes
    #error_page 500 502 503 504 /media/50x.html;
}