Flask 简明教程

Flask – FastCGI

FastCGI 是适用于 nginix、lighttpd 和 Cherokee 等网络服务器的 Flask 应用程序的另一个部署选项。

FastCGI is another deployment option for Flask application on web servers like nginix, lighttpd, and Cherokee.

Configuring FastCGI

首先,您需要创建 FastCGI 服务器文件。让我们称之为 yourapplication.fcgi

First, you need to create the FastCGI server file. Let us call it yourapplication.fcgi.

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

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

nginx 和更早版本的 lighttpd 需要一个套接字来明确地传递与 FastCGI 服务器进行通信。要做到这一点,您需要将套接字的路径传递给 WSGIServer

nginx and older versions of lighttpd need a socket to be explicitly passed to communicate with the FastCGI server. For that to work, you need to pass the path to the socket to the WSGIServer.

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

Configuring Apache

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

For a basic Apache deployment, your .fcgi file will appear in your application URL e.g. example.com/yourapplication.fcgi/hello/. There are few ways to configure your application so that yourapplication.fcgi does not appear in the URL.

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

Configuring lighttpd

lighttpd 的基本配置如下所示 -

Basic configuration of lighttpd looks like this −

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

Remember to enable the FastCGI, alias and rewrite modules. This configuration binds the application to /yourapplication.