Nginx+FastCGI+Django要求静的(css,js,imgなど)
以前は簡単なメンテナンス管理システムを書いていましたが、どうしても開発者モードでdjangoを起動していました.今はngxinエージェントを使いたいと思っています.<>を参照して、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ソケットでpreforkサーバを実行するには、次の手順に従います.
./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid
起動しますが、バックグラウンドプロセスとして使用しません(デバッグ時に便利):
./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock
Djangoのディレクトリ構造は次のとおりです.
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;
}
}
DjangoがFastCGIで起動する方法は以下の通りです.
tcpポートでスレッドサーバを実行します.
./manage.py runfcgi method=threaded host=127.0.0.1 port=3033
Unixソケットでpreforkサーバを実行するには、次の手順に従います.
./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid
起動しますが、バックグラウンドプロセスとして使用しません(デバッグ時に便利):
./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock
Djangoのディレクトリ構造は次のとおりです.
[root@VM_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;
}
}