Flask 教程

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/flask-fastcgi.html

Flask FastCGI


FastCGI 是Web 服务器(如 nginix,lighttpd 和Cherokee)上 Flask 应用程序的另一个部署选项。

配置 FastCGI

首先,需要创建 FastCGI 服务器文件,例如它的名称为:yourapplication.fcgiC

from flup.server.fcgi import WSGIServer
from yourapplication import app

if __name__ == '__main__':
    WSGIServer(app).run()

nginx 和较早版本的 lighttpd 需要明确传递一个套接字来与 FastCGI 服务器进行通信。需要将路径传递给 WSGIServer 的套接字。

WSGIServer(application, bindAddress = '/path/to/fcgi.sock').run()

配置 Apache

对于基本的 Apache 部署,.fcgi 文件将出现在您的应用程序 URL 中,例如http://example.com/yourapplication.fcgi/hello/。有以下几种方法来配置应用程序,以便yourapplication.fcgi不会出现在 URL 中。

<VirtualHost *>
   ServerName example.com
   ScriptAlias / /path/to/yourapplication.fcgi/
</VirtualHost>

配置 lighttpd

lighttpd 的基本配置看起来像这样 -

fastcgi.server = ("/yourapplication.fcgi" => ((
   "socket" => "/tmp/yourapplication-fcgi.sock",
   "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",
   "check-local" => "disable",
   "max-procs" => 1
)))

alias.url = (
   "/static/" => "/path/to/your/static"
)

url.rewrite-once = (
   "^(/static($|/.*))$" => "$1",
   "^(/.*)$" => "/yourapplication.fcgi$1"
)

请记住启用 FastCGI,别名和重写模块。该配置将应用程序绑定到/yourapplication