Using nginx+fastcgi to serve django request

Nginx is a fast and efficient HTTP and reverse proxy server. It’s suitable for building heavy traffic website. Python is a popular language because of it’s simpleness and fast develop speed. Django is no doubt the most used web development framework developed in python. Today I will write down how to make django work with nginx and fastcgi.

My development environment is based on ubuntu server. It’s very convinient to make it done under ubuntu. At first, install softwares we need.

# sudo apt-get install python-django python-flup spawn-fcgi nginx

Then add the following sentences in the nginx config file:

location / {
    # host and port to fastcgi server
    fastcgi_pass 127.0.0.1:8801;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors off;
}

At last, use the command to start fastcgi process:

# python manage.py runfcgi method=threaded host=127.0.0.1 port=8801

Open your browser and open http://localhost, if you see the django test page, then congratulations! You succeed.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>