I was thinking of deploying Nginx with mod_wsgi. However I read this blog:


In here the author of mod_wsgi for nginx says that the very few worker threads can be blocked for a relatively long time, waiting on your script to return, which will slow down the server.

How true is this? Should I just stick to fastcgi or is there something better?


Because nginx is an event driven system, it is in effect single threaded at lowest level. In other words, not much different to prefork MPM when using Apache. This means that once a request is being handled in the WSGI application running under nginx/mod_wsgi, no parallel tasks can be carried out.

In prefork MPM of Apache this isn't too serious an issue because the Apache process will not accept a connection unless it is able to handle it immediately and so any other requests will just get handled by another process. This isn't the case in nginx/mod_wsgi however as the use of an event driven system means it can greedily accept many requests at a time even though it technically can only handle one at a time. Those requests will then get processed one at a time and so latter requests which were already accepted by the process will be delayed.

Further explanation of this problem can be found in:



I recommend to use fastCGI. Last update of wsgi was in 2008 or earlier)

Sample Django.conf for fcgi:

# Django project
server {
    listen  80;
    server_name www.server.com;

    location / {
        fastcgi_pass unix:/home/projectname/server.sock;
#       fastcgi_pass;
        include conf/fastcgi.conf;      
        access_log  /logs/nginx_django.log  main;

    location ^~ /admin/ {
        fastcgi_pass unix:/home/projectname/server.sock;
        include  conf/fastcgi.conf;     
            deny all;
        access_log   off;
            auth_basic "Gimme the key!";
            auth_basic_user_file /etc/nginx_passwd;

    location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
        root   /home/projectname/media;
            limit_rate 2000K;
        access_log  /logs/nginx_django_media.log  download;
        access_log   off;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
        root   /home/projectname/static;
        access_log   off;
        expires      30d;

server {
        listen  80;
        server_name server.com;
        rewrite  ^(.*)$  http://www.server.com$1;
    access_log  /logs/nginx_django.log  main;


    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors off;

    fastcgi_param PATH_INFO         $fastcgi_script_name;
    fastcgi_param REQUEST_METHOD    $request_method;
    fastcgi_param QUERY_STRING      $query_string;
    fastcgi_param CONTENT_TYPE      $content_type;
    fastcgi_param CONTENT_LENGTH    $content_length;
    fastcgi_param SERVER_PORT       $server_port;
    fastcgi_param SERVER_PROTOCOL   $server_protocol;
    fastcgi_param SERVER_NAME       $server_name;

    fastcgi_param REQUEST_URI       $request_uri;
    fastcgi_param DOCUMENT_URI      $document_uri;
    fastcgi_param DOCUMENT_ROOT         $document_root;
    fastcgi_param SERVER_ADDR           $server_addr;
    fastcgi_param REMOTE_USER       $remote_user;
    fastcgi_param REMOTE_ADDR       $remote_addr;
    fastcgi_param REMOTE_PORT       $remote_port;       
    fastcgi_param SERVER_SOFTWARE   "nginx";
    fastcgi_param GATEWAY_INTERFACE     "CGI/1.1";

    fastcgi_param UID_SET       $uid_set;
    fastcgi_param UID_GOT       $uid_got;    

#    fastcgi_param SCRIPT_NAME      $fastcgi_script_name;

At last:

su www -c "./manage.py runfcgi method={threaded | prefork} {socket=/home/projectname/server.sock | host= port=8000} pidfile=/home/projectname/server.pid"

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.