gunicorn + nginx: Server via socket or proxy? gunicorn + nginx: Server via socket or proxy? nginx nginx

gunicorn + nginx: Server via socket or proxy?


Besides the small TCP/IP overhead, there's not much of a difference. Each listen() socket gets a connection queue, and accept() just pops a connection from that queue. In gunicorn each worker just pops a new connection from that queue as its able so that won't change. The difference is performance (sockets being a bit faster) and portability (port:IP is more flexible). Unix domain sockets will give you a bit better performance, while a socket connected to localhost gives you a bit better portability if you move the server app to a different OS, you can do so by just changing the IP address from localhost to a different hostname.


Here are the results of my test TCP Proxy via Unix socket:

Setup:nginx + gunicorn + django running on 4 m4.xlarge nodes on AWS.Setup of each node is uniform (from the same image).

1 million of requests are made over about 30 minute window:

4 m4.xlarge instances running TCP Proxy vs. Unix socket

One instance is at 100% CPU because of unrelated job running on one of the servers. 3 others are 70% CPU each represent real application load.

TCP vs. socket is virtually identical

Timing for making 1000000 requests

is 27 minutes for TCP proxy

and 31 minutes for the unix socket.

In this particular setup no unix socket performance advantage.


Socket traffic will be an easy choice if both your webserver and app server(wsgi) exist on the same machine. However you will need network ports over network connections as sockets cannot work over network so..

  1. If webserver and appserver lie on same machine - GO SOCKET
  2. If webserver and appserver are on network - GO PORTS