【问题标题】:Flask FastCGI SetupFlask FastCGI 设置
【发布时间】:2014-03-09 07:14:50
【问题描述】:

我在使用 FastCGI (Uberspace) 将 Flask 应用程序部署到 Apache 服务器时遇到问题。 我的基本 hello world 应用程序正在运行。我为索引视图设置了一个变量。但是变量上的机会不会更新浏览器中的视图。使用 python geoflask.fcgi 运行该过程将显示更新版本(在终端中),但带有以下警告:

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 200 OK

我正在使用 virtualenv,我的文件如下所示:

我的 fcgi-bin/geoflask.fcgi:

#!/home/usr/.virtualenvs/flaskenv/bin/python2.7

RELATIVE_WEB_URL_PATH = '/geoflask'
import os
LOCAL_APPLICATION_PATH = os.path.expanduser('~') + '/html/geoflask'

import sys
sys.path.insert(0, LOCAL_APPLICATION_PATH)

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


class ScriptNamePatch(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = RELATIVE_WEB_URL_PATH
        return self.app(environ, start_response)

app = ScriptNamePatch(app)

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

我的 .htacces:

<IfModule mod_fcgid.c>
   AddHandler fcgid-script .fcgi
   <Files ~ (\.fcgi)>
       SetHandler fcgid-script
       Options +FollowSymLinks +ExecCGI
   </Files>
</IfModule>

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ /fcgi-bin/geoflask.fcgi/$1 [QSA,L]
</IfModule>

有什么提示或建议吗?我整天都在为此苦苦挣扎……

【问题讨论】:

  • 你在说什么变量?可以发app的相关代码吗?
  • 这只是一个简单的 hello world 示例,在 app.py 中具有以下路由:@app.route('/') def index(): return render_template('index.html', name='hans') 如果它在我的本地主机上运行,​​它运行良好,但在服务器上,变量不会更新更改。删除浏览器缓存并没有解决问题。
  • 所以你正在编辑服务器上的文件(更改name)然后重新加载浏览器?
  • 是的,我愿意。如果我直接用 python 运行我的 fcgi 脚本,则标准输出是正确的。浏览器输出不是。

标签: python flask fastcgi uberspace


【解决方案1】:

Apache 不会立即重新加载 FastCGI 服务器进程。查看the docs for mod_fastcgi 似乎mod_fastcgi 只支持重新加载after an idle period, after a certain number of requests, or after a certain period of time。这就是为什么您的应用程序似乎没有更新的原因,即使您从命令行运行它时也会更新。

要获得您想要的行为(每次更改都重新加载),您似乎需要将 FcgidMaxRequestsPerProcessFcgidCmdOptions MaxRequestsPerProcess 设置为 1(实质上是将您的 FastCGI 设置变成 CGI 设置)。这将在每次请求时重新加载应用程序,因此不应将其用于生产 - 但它会使开发更容易。

【讨论】:

    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2011-05-19
    • 2017-05-28
    • 2012-06-06
    • 1970-01-01
    相关资源
    最近更新 更多