使用fastcgi部署django应用

1.fastcgi和cgi的区别

1)CGI (Common Gateway Interface): html

用来做为 Web Server 同 Python, PHP 等的通讯手段。而在静态网页的时代, 只须要 Web Server 便可。python

2)CGI 的缺点: nginx

1. 每一个请求都会产生一个对应的 gateway application 进程。从一个请求到另外一个请求, 内存和上下文信息会丢失。没法 cache 来提升效率。web

2. 大负载能力不足。对系统而言,启动一个进程很是耗资源。大量的并发请求(每一个请求启动一个进程)会瞬间搞跨服务器。django

3. 将 web server, gateway application 部署到不一样服务器上的愿望难以实现。服务器

 

3)FastCGI (Fast Common Gateway Interface): 并发

同 CGI 同样都是一个协议, 而不是一个具体实现。app

4)FastCGI 相对于 CGI 的改进: spa

反复重用一个进程来处理多个请求, 而不是每一个请求产生一个进程。线程

2.安装和使用

1)安装python-flup

easy_install python-flup

2)使用fastcgi方式启动django应用 

python manage.py runfcgi method=prefork host=127.0.0.1 port=9000 pidfile=/var/run/django.pid

runfcgi 的重点参数介绍:

method:

prefork or threaded (default prefork)

prefork 内存占用量大, threaded 内存须要量小。

prefork 在超大负载是仍然能够很好的工做, threaded 在大负载时经常没法响应。

prefork 至关于使用 进程处理每一个请求, 而 threaded 是每一个子进程再产生必定量的线程来处理请求。

 

pidfile:

把进程ID写入到指定的文件中。

 

3)Nginx的配置:

 

location / { 
            root html; 
            index index.html index.htm; 
            fastcgi_pass 127.0.0.1:9000; 
            fastcgi_param PATH_INFO $fastcgi_script_name; 
            fastcgi_param REMOTE_ADDR $remote_addr;
        }  
location /static{
        alias /Application/ownforum/media;
    }


修改完以后须要 reload nginx:

4)重启nginx

/usr/local/nginx/sbin/nginx -s reload
相关文章
相关标签/搜索