Nginx – What performance degradation to expect with Nginx over raw Gunicorn+Gevent

gunicornnginxperformance

I'm trying to get a very high performing webserver setup for handling long-polling, websockets etc. I have a VM running (Rackspace) with 1GB RAM / 4 cores. I've setup a very simple gunicorn 'hello world' application with (async) gevent workers. In front of gunicorn, I put Nginx with a simple proxy to Gunicorn. Using ab, Gunicorn spits out 7700 requests/sec, where Nginx only does a 5000 request/sec. Is such a performance degradation expected?

Hello world:

#!/usr/bin/env python
def application(environ, start_response):
    start_response("200 OK", [("Content-type", "text/plain")])
    return [ "Hello World!" ]

Gunicorn:

gunicorn -w8 -k gevent --keep-alive 60 application:application

Nginx (stripped):

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
}    
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    upstream app_server {
        server 127.0.0.1:8000 fail_timeout=0;
    }    
    server {
        listen 8080 default;
        keepalive_timeout 5;
        root /home/app/app/static;
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass   http://app_server;
        }
    }
}

Benchmark: (results: nginx TCP, nginx UNIX, gunicorn)

ab -c 32 -n 12000 -k http://localhost:[8000|8080]/

Running gunicorn over a unix socket gives somewhat higher throughput (5500 r/s), but it still does't match raw gunicorn's performance.

Best Answer

You can reduce the connection timeout to 0 seconds for gunicorn by using:

http://docs.gunicorn.org/en/latest/settings.html#timeout

You can also use keep_alive for the proxy_pass backend by using this directive: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

be aware, that the keep alive directive is not available in older nginx versions

Related Topic