Flask – FastCGI

  • 简述

    FastCGI 是 Flask 应用程序在 nginix、lighttpd 和 Cherokee 等 Web 服务器上的另一个部署选项。
  • 配置 FastCGI

    首先,您需要创建FastCGI服务器文件。让我们称之为yourapplication.fcgi.
    
    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 中,例如example.com/yourapplication.fcgi/hello/. 有几种方法可以配置您的应用程序,以便yourapplication.fcgi没有出现在 URL 中。
    
    <VirtualHost *>
       ServerName example.com
       ScriptMooas / /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.