Windows 下以 nginx + fastcgi 运行 Django 或 web.py
gamexg
|
1#
gamexg 发表于 2008-10-10 19:38
Windows 下以 nginx + fastcgi 运行 Django 或 web.py
原始地址Windows 下以 nginx + fastcgi 运行 Django 或 web.py
从 http://www.saddi.com/software/flup/dist/ 下载 flup 并执行 setup.py install 安装。 从 http://www.kevinworthington.co.....ers/nginx/ 下载nginx Windows版本的安装程序。 运行并安装。 打开配置文件 nginx/conf/nginx.conf 修改为: 1. #user nobody; 2. worker_processes 1; 3. 4. #error_log logs/error.log; 5. #error_log logs/error.log notice; 6. #error_log logs/error.log info; 7. 8. #pid logs/nginx.pid; 9. 10. 11. events { 12. worker_connections 64; 13. } 14. 15. 16. http { 17. include mime.types; 18. default_type application/octet-stream; 19. 20. #log_format main '$remote_addr - $remote_user [$time_local] $request ' 21. # '"$status" $body_bytes_sent "$http_referer" ' 22. # '"$http_user_agent" "$http_x_forwarded_for"'; 23. 24. #access_log logs/access.log main; 25. 26. sendfile on; 27. #tcp_nopush on; 28. 29. #keepalive_timeout 0; 30. keepalive_timeout 65; 31. 32. #gzip on; 33. 34. server { 35. listen 80; 36. server_name localhost; 37. 38. root /cygdrive/D/html; 39. index index.html index.htm; 40. 41. charset utf-8; 42. 43. #access_log logs/host.access.log main; 44. 45. # 静态资源 46. 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)$ 47. { 48. expires 30d; 49. break; 50. } 51. 52. location / { 53. # 指定 fastcgi 的主机和端口 54. fastcgi_pass 127.0.0.1:8051; 55. fastcgi_param PATH_INFO $fastcgi_script_name; 56. fastcgi_param REQUEST_METHOD $request_method; 57. fastcgi_param QUERY_STRING $query_string; 58. fastcgi_param CONTENT_TYPE $content_type; 59. fastcgi_param CONTENT_LENGTH $content_length; 60. fastcgi_param SERVER_PROTOCOL $server_protocol; 61. fastcgi_param SERVER_PORT $server_port; 62. fastcgi_param SERVER_NAME $server_name; 63. fastcgi_pass_header Authorization; 64. fastcgi_intercept_errors off; 65. } 66. 67. 68. #error_page 404 /404.html; 69. 70. # redirect server error pages to the static page /50x.html 71. # 72. error_page 500 502 503 504 /50x.html; 73. location = /50x.html { 74. root html; 75. } 76. 77. # proxy the PHP scripts to Apache listening on 127.0.0.1:80 78. # 79. #location ~ \.php$ { 80. # proxy_pass http://127.0.0.1; 81. #} 82. 83. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 84. # 85. #location ~ \.php$ { 86. # root html; 87. # fastcgi_pass 127.0.0.1:9000; 88. # fastcgi_index index.php; 89. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 90. # include fastcgi_params; 91. #} 92. 93. # deny access to .htaccess files, if Apache's document root 94. # concurs with nginx's one 95. # 96. #location ~ /\.ht { 97. # deny all; 98. #} 99. } 100. 101. 102. # another virtual host using mix of IP-, name-, and port-based configuration 103. # 104. #server { 105. # listen 8000; 106. # listen somename:8080; 107. # server_name somename alias another.alias; 108. 109. # location / { 110. # root html; 111. # index index.html index.htm; 112. # } 113. #} 114. 115. 116. # HTTPS server 117. # 118. #server { 119. # listen 443; 120. # server_name localhost; 121. 122. # ssl on; 123. # ssl_certificate cert.pem; 124. # ssl_certificate_key cert.key; 125. 126. # ssl_session_timeout 5m; 127. 128. # ssl_protocols SSLv2 SSLv3 TLSv1; 129. # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 130. # ssl_prefer_server_ciphers on; 131. 132. # location / { 133. # root html; 134. # index index.html index.htm; 135. # } 136. #} 137. 138. } 现在重启启动 nginx 后在Django项目下执 .\manage.py runfcgi method=threaded host=127.0.0.1 port=8051 就可以了。 这里没有设置 media 目录,现在是以扩展名来分辨是不是转发到Django的程序处理。 想要只允许 nginx 处理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)$ 换成 location ~ ^/media/ 就可以了。 主目录为d:\html,定义是: root /cygdrive/D/html; 如果运行 web.py 的项目 1. #!/usr/bin/env python 2. import web 3. urls = ( 4. '/.*', 'hello' 5. ) 6. class hello: 7. def GET(self): 8. print 'aaa' 9. if __name__ == "__main__": web.run(urls, globals()) 只需要把执行 .\manage.py runfcgi method=threaded host=127.0.0.1 port=8051 换成执行 a.py 8051 fastcgi 就可以了。 附一个参数列表(发现一般是使用include 包含配置文件fastcgi_params): fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with –enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; |