【发布时间】:2012-03-29 14:53:00
【问题描述】:
我已经设置好我的 Apache 服务器,它正在通过 mod_wsgi 处理 Flask 响应。我已经通过别名注册了 WSGI 脚本:
[httpd.conf]
WSGIScriptAlias /service "/mnt/www/wsgi-scripts/service.wsgi"
我已经在上面的路径添加了对应的WSGI文件:
[/mnt/www/wsgi-scripts/service.wsgi]
import sys
sys.path.insert(0, "/mnt/www/wsgi-scripts")
from service import application
我有一个提供服务模块的简单测试 Flask Python 脚本:
[/mnt/www/wsgi-scripts/service.py]
from flask import Flask
app = Flask(__name__)
@app.route('/')
def application(environ, start_response):
status = '200 OK'
output = "Hello World!"
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
@app.route('/upload')
def upload(environ, start_response):
output = "Uploading"
status = '200 OK'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
if __name__ == '__main__':
app.run()
当我转到我的网站 URL [主机名]/服务时,它按预期工作,我得到“Hello World!”背部。问题是我不知道如何让其他路线像上面示例中的“上传”一样工作。这在独立的 Flask 中运行良好,但在 mod_wsgi 下我很难过。我唯一能想到的是在 httpd.conf 中为我想要的每个端点注册一个单独的 WSGI 脚本别名,但这会带走 Flask 的花哨路由支持。有没有办法让这个工作?
【问题讨论】:
-
你试过浏览到
/service/upload吗?您可能会感到惊喜。 -
当我点击 /service/upload 时,请求仍会发送到“应用程序”功能。事实上,我可以在应用程序函数之前删除路由语句,它仍然有效。这就像应用程序总是被 mod_wsgi 用作应用程序的入口点。感觉我需要在“应用程序”中做一些事情来启动 Flask 的路由逻辑。
标签: python apache mod-wsgi flask