Flask – FastCGI



FastCGI 是 Flask 應用程式在諸如 nginx、lighttpd 和 Cherokee 之類的網路伺服器上的另一種部署選項。

配置 FastCGI

首先,您需要建立 FastCGI 伺服器檔案。容我們稱之為 yourapplication.fcgi

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

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

nginxlighttpd 的舊版本需要明確傳遞通訊套接字才能與 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
   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

廣告