将 Python/Flask 应用程序从 Heroku 迁移到 Azure 时出现 WSGI 错误

WSGI error migrating Python/Flask app from Heroku to Azure

我在将小型 Flask 应用程序部署到 Azure 时遇到了困难。该应用程序在本地和 Heroku 上运行良好,但 returns 在 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.txtProcfile:

中被引用

Procfile

web: gunicorn app:app

requirements.txt

Flask==0.12.2
Flask-Cors==3.0.3
gunicorn==19.7.1

但是 Azure 不使用 Procfile,我不清楚 gunicorn 是否可以在 Azure 环境中工作。

我的问题是:

  1. Green Unicorn 可行吗?如果可以,我该如何配置?
  2. 如果没有,我的其他选择是什么?对于像 这样的帖子,我尝试在 app 声明下方添加一个 wsgi_app = app.wsgi_app 行,但是 returns 一个错误让我陷入了另一个兔子洞: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>

此时我只是盲目 copy/pasting 可能的解决方案,所以我真的很感激任何帮助。谢谢。

尝试定义 wsgi_app:

app = Flask(__name__)

# define for IIS module registration.
wsgi_app = app.wsgi_app

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