nginx和fastcgi安裝就不多說了,由於在win下,直接下載win版的nginx,不用安裝,直接就可以運行。
令人暗爽的是nginx內置fastcgi模塊,不用自己下載編譯fastcgi,也就因為這種所以才選擇nginx。
當然在Lin下另當別說,因為在ubuntu安裝這類應用太爽了,apt-get install即可。
少YY多做事,言歸正傳吧。
另外,django使用fastcgi需要使用flup模塊。
準備妥當後,開始設置配置文件。先從django開始。
MEDIA_ROOT,一定要指定django正確的位置,盡可能使用django應用所在目錄絕對路徑吧。
TEMPLATE_DIRS,還是建議使用絕對路徑。
現在用兩個django應用,一個使用localhost訪問,另一個使用電腦名稱admin-mu訪問。
使用localhost訪問設置
MEDIA_ROOT = 'E:/soft/nginx-0.8.30/html/localhost/www/site1'
TEMPLATE_DIRS=('%s/templates' % MEDIA_ROOT,)
使用admin-mu訪問設置
MEDIA_ROOT = 'E:/soft/nginx-0.8.30/html/admin-mu/www/site1'
TEMPLATE_DIRS=('%s/templates' % MEDIA_ROOT,)
然後設置nginx的配置文件nginx.conf。
多站點設置只是要設置多個server{}定義段,下面用實例來說一下吧。
server {
listen 80;
server_name localhost;#指定使用的域名
location / {
root html/localhost; #指定網站的路徑,win下相對於nginx運行的位置
index index.html index.htm; #使用的默認入口文件
fastcgi_pass 127.0.0.1:8000;#這個要與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_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
server {
listen 80;
server_name admin-mu;
index index.html;
root html/admin-mu/www;
location / {
fastcgi_pass 127.0.0.1:8001;
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_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
設置完成後,就可以運行程序了。
cd E:/soft/nginx-0.8.30/html/admin-mu/www/site1
manage.py runfcgi method=threaded host=localhost port=8001
port指定的端口要與nginx.conf相應server定義段的fastcgi_pass指定的端口要一致。
cd E:/soft/nginx-0.8.30/html/localhost/www/site1
manage.py runfcgi method=threaded host=localhost port=8000
這樣fastcgi使用不同的端口加載不同的django應用。
接下來啟動nginx,win下默認使用conf下的nginx.conf配置文件,如果在其它位置可以使用 -c 指定位置/nginx.conf
如果不出錯,nginx就正常啟動了。現在可以試下分別用http://localhost和http://admin-mu來訪問不同的應用。
其實也是挺簡單的,只是開始的時候想使用一個nginx實例對應一個站點,也就是每一個站點使用獨立的nginx.conf。弄得灰塵滿天飛,現在一切都結束,塵埃也可以落地。
nginx+fastcgi 實現多django站點應用
最新推荐文章于 2020-04-21 13:45:01 发布