首页 > 解决方案 > 在 Nginx 中向 uWSGI 公开 Flask (connexion) 实例

问题描述

在运行我的 Docker 映像并请求 Swagger UI 时,我收到:502 Bad Gateway.

我正在尝试在 nginx 上使用 uWSGI 运行 Connexion(基于 Flask 的 Swagger UI 生成器)。我认为这是因为 uWSGI 没有正确获取我的 Flask 实例。但是,在我看来,我的容器实例配置正确。

如果你看这里https://github.com/Microsoft/python-sample-vscode-flask-tutorial,我的应用程序的设置和配置是相似的,它可以正常工作。

根据 Connexion 文档,我应该能够使用

app = connexion.App(__name__, specification_dir='swagger/')
application = app.app # expose global WSGI application object

你可以在这里找到我完整的应用程序代码:

https://bitbucket.org/lammy123/flask_nginx_docker_connexion/src/master/

Flask/Connexion 对象在 application/__init__.py

uwsgi.ini:

[uwsgi]
module = application.webapp
callable = app
uid = 1000
master = true
threads = 2
processes = 4

__init__.py:

import connexion

app = connexion.App(__name__, specification_dir='openapi/')
app.add_api('helloworld-api.yaml', arguments={'title': 'Hello World Example'})

webapp.py:

from . import app    # For application discovery by the 'flask' command. 
from . import views  # For import side-effects of setting up routes.
from . import app
import connexion

application = app.app

使用内置开发服务器运行代码可以工作。

预期行为是 Swagger UI 在以下位置可用:

http://localhost:5000/v1.0/ui/#/

从 Docker 容器运行时。

标签: dockernginxflaskuwsgiconnexion

解决方案


推荐阅读