【发布时间】:2018-06-18 16:38:19
【问题描述】:
我在将小型 Flask 应用程序部署到 Azure 时遇到了困难。该应用程序在本地和 Heroku 上运行良好,但在 Azure 上返回内部服务器错误。这是日志:
logs.txt
StdErr:
2018-06-15 22:14:38.239395: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
File "D:\home\python353x86\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\home\python353x86\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "D:\home\python353x86\wfastcgi.py", line 603, in get_wsgi_handler
handler = getattr(handler, name)
AttributeError: module 'app' has no attribute 'wsgi_app'
2018-06-15 22:14:38.255034: Running on_exit tasks
2018-06-15 22:14:38.270645: wfastcgi.py 3.0.0 closed
这似乎是 WSGI 配置的问题。这是我的第一次 Azure 部署,以前我从来没有过多考虑过 WSGI。根据教程,我在 Heroku 上使用 Green Unicorn,在 requirements.txt 和 Procfile 中引用了它:
过程文件
web: gunicorn app:app
requirements.txt
Flask==0.12.2
Flask-Cors==3.0.3
gunicorn==19.7.1
但是 Azure 不使用 Procfile,我不清楚 gunicorn 是否可以在 Azure 环境中工作。
我的问题是:
- 绿色独角兽可行吗?如果是,我该如何配置?
- 如果不是,我还有哪些其他选择?根据this 之类的帖子,我尝试在
app声明下方添加wsgi_app = app.wsgi_app行,但这会返回一个错误,导致我陷入另一个兔子洞:TypeError: wsgi_app() missing 2 required positional arguments: 'environ' and 'start_response'
这是后端的其余部分:
app.py
# Dependencies ------------------------
from flask import Flask
from flask_cors import CORS, cross_origin
from flask import render_template
from flask import request
import sqlite3
import json
import os
# Config ------------------------------
app = Flask(__name__)
CORS(app)
# Routes ------------------------------
@app.route("/")
def home():
# Do stuff
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="WSGI_HANDLER" value="app.wsgi_app()" />
<add key="PYTHONPATH" value="D:\home\site\wwwroot" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule"
scriptProcessor="D:\home\python353x86\python.exe| D:\home\python353x86\wfastcgi.py"
resourceType="Unspecified" requireAccess="Script" />
</handlers>
<rewrite>
<rules>
<rule name="Configure Python" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
在这一点上,我只是盲目地复制/粘贴可能的解决方案,所以我非常感谢任何帮助。谢谢。
【问题讨论】:
标签: python azure flask gunicorn wsgi