Python+Django+Nginx配置

python django nginx configure

Posted by Xiekers on July 21, 2015
Python和Django的安装就不写了,网上太多了,Django的部分推荐看http://www.djangoproject.com/ 已经很详细了。
下面记录一下怎样为Django配置Nginx:
需要用到flup,直接python setup.py install安装即可。http://trac.saddi.com/flup
然后
#python manage.py runfcgi socket=/tmp/python/python.sock maxrequests=1
当然也可以
#python manage.py runfcgi method=prefork host=127.0.0.1 port=9001
配置nginx.conf
server {
        listen       80;
        server_name  p.wraith.com;
        root /data0/htdocs/python;
        access_log  /data1/logs/python.access.log;
        error_log  /data1/logs/python.error.log;
 
        #配置django admin需要的文件
        location ~* ^/media/(css|img|js)/.*$ {                root /usr/local/lib/python2.6/site-packages/django/contrib/admin;                expires 30d;                break;        }
 
        location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {                access_log off;                break;        }
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {                expires 30d;                break;        }
        location / {                fastcgi_pass unix:/tmp/python/python.sock;
                #如果上面是采用TCP的形式则
                #fastcgi_pass 127.0.0.1:9001;
                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";
        }
}
如果出现502错误的话,看一下python.sock的权限