使用fastcgi部署django应用

1.fastcgi和cgi的区别

1)CGI (Common Gateway Interface):

用来作为 Web Server 同 Python, PHP 等的通信手段。而在静态网页的时代, 只需要 Web Server 即可。

2)CGI 的缺点:

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

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

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

3)FastCGI (Fast Common Gateway Interface):

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

4)FastCGI 相对于 CGI 的改进:

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

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


原文地址:https://www.cnblogs.com/chenjianhong/p/4144736.html