之前写了一个简易的运维管理系统,奈何一直都是用的开发者模式启动django,现想用ngxin代理,参照<<The Django Book>>,上面提供了Apache+mod_python(mod_wsgi|FastCGI)等方式,而我选择了Nginx+FastCGI的方式(机器上本来就有nginx了,并且我平时用nginx也比较多).
Django通过FastCGI启动的方式有如下几种:
在tcp端口上运行一个线程服务器:
./manage.py runfcgi method=threaded host=127.0.0.1 port=3033
在Unix socket 上运行prefork 服务器:
./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid
启动,但不作为后台进程(在调试时比较方便):
./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock
Django的目录结构如下:
[[email protected]_56_231_centos tencent]# tree tencent/ tencent/ ├── db.sqlite3 ├── log │ └── all.log ├── manage.py ├── scripts │ ├── changetime.sh │ ├── test.sh │ └── testtime.sh ├── static │ ├── css │ │ ├── bootstrap.css .... │ │ └── bootstrap-responsive.min.css │ ├── img │ │ ├── glyphicons-halflings.png │ │ └── glyphicons-halflings-white.png │ ├── jquery │ │ └── jquery-1.8.3.min.js │ └── js │ ├── bootstrap-datetimepicker.js .... ├── tencent │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── tencentapp ├── admin.py ├── __init__.py ├── models.py ├── template │ ├── bb.html .... │ └── ee.html ├── tests.py └── views.py
static是放置静态文件的目录
Nginx的配置文件如下:
server {
listen 9007;
server_name default;
index index.html index.htm;
location / {
fastcgi_pass 127.0.0.1:9008; #这是是Django启动时的套接字地址,启动方式可参照上文
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_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
location ~ ^/static/ {
root /data/python/tencent/tencent; #这是Django静态文件的目录
expires 24h;
}
}