Nginx+fastcgi部署Django项目
一: 下载安装Nginx/Flup
主站
下载
http://nginx.org/en/download.html
解压,运行nginx.exe,查看http://localhost/,没问题就出现:
Welcome to nginx! |
注意:nginx也可以在命令窗口启动(可以看到nginx.conf文件中是否出错)
安装Flup,到官网上http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz下载,解压后发现是python写的类库,把它安装到python里,安装方法和其他的python类库一样做法,在doc命令行里执行:
pythonsetup.py install #注意要求python写到path里,才能有python命令
二:配置nginx.conf
1:配置文件在conf/nginx.conf
2:#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 64;
}
http {
include mime.types;
default_type application/octet-stream;
include D:/Documents/Desktop/nginx-1.2.8/vhosts/*.conf;
#log_format main '$remote_addr - $remote_user [$time_local] $request '
# '"$status"$body_bytes_sent "$http_referer" '
# '"$http_user_agent""$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 10;
#gzip on;
server {
listen 80;
server_name localhost; #写上project对应的域名我们在本地上测试,所以是localhost;
#root /cygdrive/D/html;
#root F:/projects/www/mysite;
# 使用哪行,调试一下
root E:\soft\eclipse\eclipse-jee-indigo-SR2-win32\workspace\gstrz;#django项目路径
index base.html base.htm;
charset utf-8;
#access_log logs/host.access.log main;
location ^~/static/ {
#alias E:/soft/eclipse/eclipse-jee-indigo-SR2-win32/workspace/gstrz/static/;#项目的静态资源
# 使用哪行,调试一下
#alias /cygdrive/F/Python25/Lib/site-packages/django/contrib/admin/media/;
}
# 静态资源
location ~*^.+/.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{ root E:/soft/eclipse/eclipse-jee-indigo-SR2-win32/workspace/gstrz/static/; #项目的静态资源
expires 30d;
break;
}
location / {
# 指定 fastcgi 的主机和端口
fastcgi_pass 127.0.0.1:8051;我们通过fastgci(flup)在8051端口上跑django项目
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_paramREQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
#error_page 404 /404.html
# redirectserver error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
# proxy the PHPscripts to Apache listening on 127.0.0.1:80
#
#location ~/.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHPscripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~/.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny accessto .htaccess files, if Apache's document root
# concurs withnginx's one
#
#location ~//.ht {
# deny all;
#}
}
三:启动django项目:
在命令出口里进入django项目的根目录,启动:
manage.py runfcgimethod=threaded host=127.0.0.1 port=8051
也可以编写指令,方面启动:
在项目的根目录下新建一个vbs文件run:
set ws=wscript.createobject("wscript.shell")
ws.run "manage.py runfcgi method=threadedhost=127.0.0.1 port=8051",0
以后每次启动,双击run文件即可。
5. nginx服务器上配置多个网站的方法
1、为我们的站点创建配置文件
在nginx的配置文件conf目录下创建一个专门存放VirtualHost的目录,命名为vhosts_conf,可以把虚拟目录的配置全部放在这里。
具体参考: http://www.jb51.net/article/27533.htm
注意: django—setting.py文件中debug设为false,nginx会报500错误
改正: 将setting.py文件中的ALLOWED_HOSTS = []设置为ALLOWED_HOSTS = ['*']
参考资料:
1:http://blog.csdn.net/sunguangran/article/details/6760383
2:http://2goo.info/blog/panjj/other/2010/11/07/137
3:http://wenku.baidu.com/view/45aaab14a21614791711289f.html