English 中文(简体)
Flask - FastCGI
  • 时间:2024-09-17

Flask – FastCGI


Previous Page Next Page  

FastCGI is another deployment option for Flask apppcation on web servers pke nginix, pghttpd, and Cherokee.

Configuring FastCGI

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

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

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

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

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

Configuring Apache

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

<VirtualHost *>
   ServerName example.com
   ScriptApas / /path/to/yourapppcation.fcgi/
</VirtualHost>

Configuring pghttpd

Basic configuration of pghttpd looks pke this −

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

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

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

Remember to enable the FastCGI, apas and rewrite modules. This configuration binds the apppcation to /yourapppcation.

Advertisements