ãªãwsgiãšnginxã§ããïŒ
ããã«ã¯ããã€ãã®çç±ããããŸãã ãŸãã解決çã¯é«éã§ãã¡ã¢ãªãã»ãšãã©æ¶è²»ãããæç¶å¯èœã§ãªããã°ãªããŸããã Nginxã¯ãã®ããã«äœæãããŸããã ããã«ã ããã©ãŒãã³ã¹ ãã¹ãã«ãããšãnginx + wsgiãã³ãã«ã¯ãã¡ã¢ãªæ¶è²»éãå°ãªããé«è² è·äžã§ã®é«ããã©ãŒã«ããã¬ã©ã³ã¹ãæäŸããŸãã 第äºã«ãã·ã¹ãã ãåçŽã§ããã»ã©ãä¿¡é Œæ§ãé«ãããšãç¥ãããŠããŸãã wsgiãšfastcgiãæ¯èŒãããšãåŸè ã«ã€ããŠã¯ãã¢ããªã±ãŒã·ã§ã³èªäœãå®è¡ãããå¥åã®ãµãŒããŒã§ãããšèšããŸãã 誰ãããã®ãµãŒããŒãã¯ã©ãã·ã¥ããªãããšã確èªããå¿ èŠããããŸãã äžæ¹ãwsgiã¯Cã®ã¢ããªã±ãŒã·ã§ã³ïŒnginxïŒããã®pythonã¹ã¯ãªããã®åŒã³åºãã§ãããããã€ã³ã¿ãŒãã§ã€ã¹ãæ§æãããããšã³ãã£ãã£ã®æ°ãååã«ããŠãWebãµãŒããŒãšã®ã¿éä¿¡ããŸãã
ããã«ãçè«çã«ã¯åã«wsgiããšã³ããŠãŒã¶ãŒã®æ¹ãé«éã§ãããšããä»®å®ããããŸãããçè«çã«ã¯50人ã®ãŠãŒã¶ãŒãåæã«ãµãŒããŒã«æ¥ç¶ããåæ§ã«éãããŒãžãèŠæ±ãããšããŸãã fastcgiã®å Žåãnginxã¯ããã«fastcgiãµãŒããŒã«50ãªã¯ãšã¹ããè¡ããå¿çãåŸ ã¡ãŸãã çŽç²ã«çè«çã«ã¯ãåæã«å°çããåããªã¯ãšã¹ããå®è¡äžã®ã¢ããªã±ãŒã·ã§ã³ã倧éã«çæããããã»ããµæéãåçã«ç«¶ãããã¹ãŠãäžåºŠã«å®è¡ãããŸãïŒå®è¡äžã®fastcgiã¢ããªã±ãŒã·ã§ã³ã®æ°ã«å¶éããªãå ŽåïŒã Nginxã¯wsgièªäœã«å±ãããªã¯ãšã¹ããåŠçããåæãªã¯ãšã¹ãã®æ倧æ°ã¯ã¯ãŒã«ãŒã®æ°ã«çããããããããã¯ãã¥ãŒã«å ¥ãããããŠãŒã¶ãŒã¯ã»ãšãã©ããã«æåã®åçãåãåããŸãã ããããåŸè ã¯fastcgiã®å Žåãšåãææã«æ¥ãŸããè¡ãå ã¯ãããŸããã
çè«ããå®è·µãž
æãéå±ãªéšåã¯ãnginxãåéããŸãã mod_wsgiãæ¥ç¶ããä»ã®æ¹æ³ããªããããã¢ã»ã³ãã«ããå¿ èŠããããŸãããã®ãããææ°ã®å®å®ããŒãžã§ã³ïŒçŸåšã¯0.7.60 ïŒãIgor Sysoevã®ãµã€ãããåŒãåºããŠãã©ããã«å±éããŸãã
mod_wsgiã®å ¬åŒããŒãžã¯ãç§ãç解ããŠãããšããã ããã«ãããŸã ã ãããŠãããã¯äž»ãªå€±æãç§ãã¡ã«éããããå Žæã§ã-ãããžã§ã¯ãã¯1幎以äžæŽæ°ãããŠããŸããã nginxã®çŸåšã®ããŒãžã§ã³ãšã®äºææ§ã®ããã«ãœãŒã¹ãå°ãä¿®æ£ããå¿ èŠããããŸãããäœããäž»ãªããšã¯ãã¹ãŠãéå§ããå¿ èŠããããšããããšã§ãã ã¢ãžã¥ãŒã«ã®ææ°ããŒãžã§ã³ãããŠã³ããŒãããIndixã®è¿ãã®ã©ããã«å±éããŸãã
æ£ç¢ºã«ç·šéããå¿ èŠããããã®ïŒ
1ïŒmod_wsgiã䜿çšããã¢ãŒã«ã€ãã®patchãã©ã«ããŒã«ã¯ãconfigãšngx_http_wsgi_module.cã«ããããåœãŠããããnginx-0.6.x.patchããããŸããçµããã¯ç¡èŠã§ããŸãããå¿ èŠãããŸããã
2ïŒmod_wsgiã®æåŸã®æŽæ°ããã£ããšããEnginixã®çŸåšã®ããŒãžã§ã³ã¯ãŸã ã§ã¯ãªãã£ãã®ã§ãå ¬åŒãããã§ã¯ååã§ã¯ãããŸããã ä»ã®ã³ã³ãã€ã«ãšã©ãŒãæ€çŽ¢ããçµæã ãã®ãµã€ãã«ãã©ãçããŸãã ã æ¥æ¬èªã¯ããããŸããããå¿ é ã§ã¯ãããŸããã§ãããããŒãžã«å¿ èŠãªãããããããŸãã 確ãã«ããã®ãããã®è¡çªå·ã¯ãœãŒã¹ã®è¡çªå·ãšäžèŽããªãã£ããããæåã§ç·šéããå¿ èŠããããŸããã
src / ngx_http_wsgi_module.cãã¡ã€ã«ã§ããã¡ã€ã«ã®å é ã«è¿ãæ§é ã宣èšããŸãã
static ngx_path_init_t ngx_http_wsgi_temp_path = {
ngx_string(NGX_HTTP_WSGI_TEMP_PATH), { 1, 2, 0 }
};
åãå Žæã§ãngx_garbage_collector_temp_handleré¢æ°ã®æåã®åŒã³åºãã¯NULLã«çœ®ãæããããŸãã
é¢æ°åŒã³åºã
ngx_conf_merge_path_value(conf->temp_path,
prev->temp_path,
NGX_HTTP_WSGI_TEMP_PATH, 1, 2, 0,
ngx_garbage_collector_temp_handler, cf);
ã«å€æŽ
ngx_conf_merge_path_value(cf, &conf->temp_path,
prev->temp_path,
&ngx_http_wsgi_temp_path);
ä»åŸã¯ãmod_wsgiãœãŒã¹ã«æ»ã£ãŠä¿®æ£ããå¿ èŠããããšèšããŸãããååãšããŠããã§ã«ã¢ã»ã³ããªã®æºåãã§ããŠããŸãã
EnginixãœãŒã¹ããããã£ã¬ã¯ããªã«ç§»åããŠå®è¡ããŸã
./configure --add-module = / path / to / mod_wsgi /
ããã«å°ããªãã©ã°ããããŸããã å®éã®ãšãããhttp_cacheã¢ãžã¥ãŒã«ã¯indinixã§æäŸããå§ããŠãããopenSSLãèŠæ±ãå§ããŸããã ç§ã¯é·ãéopenssl-devããã±ãŒãžãæ€çŽ¢ãããã®ã¢ãžã¥ãŒã«ãªãã§ãã«ããããã£ãã®ã§ãããããã±ãŒãžãèŠã€ãããåã«ssl-devãšåŒã°ããŸããã ãšããã§ããšã©ãŒã®èª¬æã«ãšã©ãŒããããŸããhttp_cacheãç¡å¹ã«ãããã©ã¡ãŒã¿ãŒã¯ã--without-http_cacheãã§ã¯ãªããã-without-http-cacheãã§ãã
ããã§ãmakeïŒmake installãè¡ããŸããã
Nginxèšå®
nginx.confã§æåã«ä¿®æ£ããå¿ èŠãããã®ã¯ãã¯ãŒã«ãŒã®æ°ã§ãã éåžžãæé©ãªã¯ãŒã«ãŒæ°ã¯ãã·ã³ã®ã³ã¢æ°ã«çãããšèããããšãããã§ãããã mod_wsgiã䜿çšãããšãç¶æ³ã¯å°ãå€ãããŸãã å®éãã¢ããªã±ãŒã·ã§ã³ã¯ãããã®åãã¯ãŒã«ãŒã®ã³ã³ããã¹ãã§åäœãããããåæã«å®è¡ãããããã»ã¹ã®æ°ã¯ã¯ãŒã«ãŒã®æ°ã«çãããªããŸãã çæ³çãªæ¡ä»¶äžã§ã¯ãã³ã¢ã®æ°ã§èŠãã¯ãŒã«ãŒã®æ°ãããã»ããµãŒã«æ£ç¢ºã«100ïŒ ãããŒãããŸãã ããããæ¡ä»¶ã¯å®å šã§ã¯ãªããã¢ããªã±ãŒã·ã§ã³ã®å®è¡ã¯æã äžæãããŸãã ããšãã°ãããŒã¿ããŒã¹ãµãŒããŒãå¥ã®ãã·ã³äžã«ããå Žåããã£ã¹ã¯æäœã®å®è¡æãŸãã¯ããŒã¿ããŒã¹ã®ã¯ãšãªæã ãããã£ãŠãæé©ãªã¯ãŒã«ãŒæ°ã¯ã³ã¢æ°ã®2åã«çããããã«æããŸãã
次ã«ããµãŒããŒå ã®å Žæãæ§æããŸãã
mod_wsgiã®äŸã§ææ¡ãããŠããããã«è¡ãå ŽåïŒ
location / {
wsgi_pass /path/to/nginx/django.py;
}
次ã«ãåããµãŒããŒã§ã®éçåŠçã«ãããªããèšãããšãã§ããŸãã äžè¬çãªè§£æ±ºçã¯ãäžè¬çãªãã¡ã€ã«ã¿ã€ãã«èŠåæ§ãèšå®ãããããããã¡ã€ã«ãšããŠæäŸããæ®ããããã¯ãšã³ãã«ãã³ãããããšã§ãã ããããç§ã¯ãã®ãªãã·ã§ã³ã奜ãã§ã¯ãããŸããã æåã«ããµãŒããŒäžã«ãªãç»åãèŠæ±ãããšãæšæºã®Enginixã¡ãã»ãŒãžã衚瀺ãããŸãã次ã«ãèªåçæãããpdfãã¡ã€ã«ãæäŸããå¿ èŠãããå Žåã¯ã©ããªããŸããïŒ åœŒã¯ãŸããpdfã®æ¡åŒµåãæã£ãŠããããšããå§ãããŸãã ãããã£ãŠãç§ã¯ç§ã®ãªãã·ã§ã³ãææ¡ããŸãïŒãã¹ãŠã®ãã¹ãæåã«ãã£ã¹ã¯äžã§æ€çŽ¢ããããã®ãããªãã¡ã€ã«ãèŠã€ãããªãå Žåã¯ãããŒãžã®åŸãããããã¯ãšã³ãã«ç§»åããŸãïŒãããŠäœãèŠã€ãããªãå Žåã¯ããµãŒããŒã§ã¯ãªãã¢ããªã±ãŒã·ã§ã³ããããŒãž404ããããŸãïŒã
location / {
root /path/to/www/;
error_page 404 = @backend;
log_not_found off;
}
location = / {
#
wsgi_pass /path/to/nginx/django.py;
include wsgi_params;
}
location @backend {
wsgi_pass /path/to/nginx/django.py;
include wsgi_params;
}
ãŸããäœæããå¿ èŠããã2ã€ã®ãã¡ã€ã«ã䜿çšããŸãã
ãããã wsgi_paramsãååŸããŸã ã
wsgi_var REQUEST_METHOD $request_method;
wsgi_var QUERY_STRING $query_string;
wsgi_var CONTENT_TYPE $content_type;
wsgi_var CONTENT_LENGTH $content_length;
wsgi_var SERVER_NAME $server_name;
wsgi_var SERVER_PORT $server_port;
wsgi_var SERVER_PROTOCOL $server_protocol;
wsgi_var REQUEST_URI $request_uri;
wsgi_var DOCUMENT_URI $document_uri;
wsgi_var DOCUMENT_ROOT $document_root;
wsgi_var SERVER_SOFTWARE $nginx_version;
wsgi_var REMOTE_ADDR $remote_addr;
wsgi_var REMOTE_PORT $remote_port;
wsgi_var SERVER_ADDR $server_addr;
wsgi_var REMOTE_USER $remote_user;
ãããŠãå ¬åŒã®django.pyããã¥ã¡ã³ãããïŒ
import os, sys
# ,
sys.path.append('/path/to/')
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
DJANGO_SETTINGS_MODULEããã¡ã€ã«åã§ã¯ãªããã¢ãžã¥ãŒã«åã瀺ãããšã«æ³šæããŠãã ããã ç¹å®ã®äŸã§ã¯ãæ§æãã¡ã€ã«ã®å®éã®å Žæã¯ã/path/to/project/settings.pyãã§ããå¿ èŠããããŸã
ããã°ãã³ããŒ
äœãèŠéããŠããªãå Žåã¯ããã¹ãŠãEnginixãèµ·åããæºåãã§ããŠããã¯ãã§ãã ãã©ãŠã¶ãèµ·åããhttpïŒ// localhost /ïŒãŸãã¯ããã«ãããã®ïŒã«ç§»åãããããžã§ã¯ããæ°ããå ŽåïŒç§ã®ãã®ã®å ŽåïŒããŸãã¯ã¡ã€ã³ããŒãžïŒæ¢ã«èšè¿°ãããŠããå ŽåïŒã®dzhangaã®ãŠã§ã«ã«ã ããŒãžã衚瀺ããŸãã æ£çŽãªãšãããæ倧ã®å°é£ã¯èåŸã«ãããšãã§ã«æã£ãŠããŸããããããã«ã¯ãããŸããã§ããã httpïŒ// localhost / 1ïŒãŸãã¯ã«ãŒã以å€ã®URLïŒãå ¥åãããšããã«ã500çªç®ã®EnginixãååŸããŸããã ããã¯ã500çªç®ã®dzhangiãããæªãããšã¯æããã§ãã è² ã®2çªç®ã®åŒæ°ïŒæååã®é·ãïŒãPyString_FromStringAndSizeé¢æ°ã«æž¡ãããããšã瀺ããšã³ããªããã°ã«è¡šç€ºãããŸããã
ãœãŒã¹ãéã¢ã»ã³ãã«ããåŸãç§ã¯ã¢ãžã¥ãŒã«ãpath_infoè¡ã®å é ããå Žæã®ååãURLãååŸãããã³ãã©ãŒã«åé€ããããšããŠãããšããçµè«ã«è³ããŸããã ã€ãŸã ãã®å Žåããã¹ãŠã®ããŒãžããã±ãŒã·ã§ã³ããã¯ãšã³ãã«åé¡ãããã¢ãžã¥ãŒã«ãpath_infoããæžç®ããããšããã®ã¯æ£ç¢ºã«ã¯8æåã§ãããåçŽã«ããã»ã©å€ãã¯ãããŸããã URLã«éåžžã«å€ãã®æåãå«ãŸããå Žåãæåãããã¹ãåãæšãŠãããŸããã httrã®ä»£ããã«ïŒ// localhost / 123456789åä¿¡httrïŒ// localhost89
ã³ã¡ã³ãã«ã¯TODOã®ãclcf-> nameãr-> uriã«ããããšã確èªããå¿ èŠããããŸãããã€ãŸã ãURLæååã«ãã®å Žæã®ååããããã©ããã確èªããå¿ èŠããããŸãã
ååãšããŠããããè¡ãããçç±ãç解ããŠããŸããããšãã°ã
location /django {
wsgi_pass âŠ
}
httpïŒ// localhost / django / foo / bar /ãšãã圢åŒã®URLã¯ãjungaããhttpïŒ// localhost / foo / bar /ã«ãããŒããã£ã¹ããããŸãã ããããèè ããªãurlãäžè¬çãªæ£èŠè¡šçŸããã³ãªã³ã¯ã§ããå¯èœæ§ãããããšãèæ ®ããŠããªãã®ãã¯æããã§ã¯ãããŸããïŒãã®å Žåã®ããã«ïŒã ç¹ã«å¿ èŠãªå Žåã¯ãincludesã䜿çšããŠurls.pyãæžãæããããšã«ãããdjangoã§å®è¡ã§ããããããã®ãããªãã°ã®ããæ©èœã¯å¿ èŠãªããšå€æããŸããã
äžè¬ã«ã ngx_wsgi_run.cã® 584è¡ç®ãä¿®æ£ããå¿ èŠããããŸãã
if (clcf->name.len == 1 && clcf->name.data[0] == '/') {
if (1 || (clcf->name.len == 1 && clcf->name.data[0] == '/')) {
åäœäžã®Enginixãæ¶æ» ãããåŸãå床makeïŒmake installãå®è¡ããŸãã
ãµãŒããŒã®åèµ·å
Pythonã¯phpã§ã¯ãããŸããã ããŠã³ããŒããããã¢ããªã±ãŒã·ã§ã³ã¯ããœãŒã¹ããåé¢ããããã®å€æŽã«ã€ããŠã¯äœãç¥ããŸããã ãããã£ãŠãã³ãŒããæŽæ°ãããšãã¯ãæã Enginixãåèµ·åããå¿ èŠããããŸãã 幞ããEnginixã¯éåžžã«åªãããµãŒããŒã§ãããã¹ãŠã®ã¯ã©ã€ã¢ã³ããåæããŠãããã匷å¶çµäºããŠå床å®è¡ããã¹ã¯ãªãããèšè¿°ããå¿ èŠã¯ãããŸããã ããã¯ããµãŒãã¹ãæäŸããŠããã¯ã©ã€ã¢ã³ããå³åº§ã«åæããããšãªããã¯ãŒã«ãŒããœããã«åèµ·åããæ©èœãæäŸããŸãã ããã¯ãHUPã·ã°ãã«ããã¹ãããã»ã¹ã«éä¿¡ããããšã§å®è¡ã§ããŸãã ãããã£ãŠãç§ãã¡ãããå¿ èŠãããã®ã¯ãåçŽãªã¹ã¯ãªãããæžããŠãããã«ååãä»ããäŸãã°restart-serverãæžãããšã§ãïŒ
sudo kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
ãã®ã¹ã¯ãªãããå®è¡ãããã³ã«ãã¢ããªã±ãŒã·ã§ã³ãåèµ·åããŸãã ããã«ã1æéã«1åãªã©ããã®ã¹ã¯ãªãããå ã«æããããšã匷ããå§ãããŸãã ããã¯ããã©ãŒãã³ã¹ã«å€§ããªåœ±é¿ãäžããŸããããã¹ã¯ãªãããæµãå§ããã°ã¡ã¢ãªãç¯çŽã§ããŸãã
è»èã§é£ã¶
æ®å¿µãªãããçµæã®ãµãŒããŒã«ã¯ã¢ãã¬ã¹è ±ããããŸãã 10人ãäœããã®çš®é¡ã®ãã¬ãŒãããŒãžãåæã«ããŒãããããšã決ããå ŽåïŒpdfçæãŸãã¯ã¢ãã¿ãŒã®ãã³ããªã©ïŒãããªãã10人ã®åŽåè ããããªãå Žåãä»ã®èŠæ±ã¯ãããŸããïŒããšãã°ãgifç»åãªã©ã®æã軜ããã®ãåŠçãããŸãïŒå°ãªããšã1人ã®åŽåè ã¯ç¡æã§ã¯ãããŸããã æ®å¿µãªãããä»ã®éä¿¡æ¹æ³ããã¹ãããŸããã§ãããfastcgiãŸãã¯http_passã䜿çšããå Žåãšåãå¹æãåŸãããå¯èœæ§ããããŸãããå¥ã®TCPæ¥ç¶ã䜿çšãããŠããããããã®ããã«åäœããªãããã§ãã ããããç§ãèšã£ãããã«ãEnginixã¯çŽ æŽããããµãŒããŒã ããã§ãã 圌ã¯ãªã¯ãšã¹ããéããã®ãšè»œããã®ã«åããŸãããã¥ãŒã«éããªã¯ãšã¹ããããããããå Žåã§ããå°ãªããšã1人ã®ã¯ãŒã«ãŒã解æŸãããšãæåã«ãã¹ãŠã®è»œããªã¯ãšã¹ããåãåºãããã®ããšã§éããªã¯ãšã¹ããåãå ¥ããŸãã èšãæããã°ã軜ããªã¯ãšã¹ãã¯ãåãåã£ãé çªã«é¢ä¿ãªããåžžã«éããªã¯ãšã¹ããããåªå 床ãé«ããªããŸãã