运行 Flask with Gunicorn On Raspberry Pi

Running Flask with Gunicorn On Raspberry Pie

我正尝试 运行 我的 Flask 应用程序在我的 Raspberry pi 上使用 gunicorn。我已经将我的路由器设置为端口转发 localhost:5000。当我通过 python manage.py runserver 运行 我的烧瓶应用程序时,这很有效。我可以在任何设备上使用我的浏览器并输入 http://**.**.***.***:5000/,它会加载我的 Flask 应用程序。但是,当我尝试通过 gunicorn 运行 应用程序时,我收到错误连接页面。我 运行 gunicorn 和 flask documentation 说的一模一样。如果我检查 gunicorn 的日志,我可以看到 html 正在呈现。更重要的是,当我 运行 本地带有 gunicorn 的应用程序 (gunicorn -w 2 -b localhost:5000 my_app:app) 时,它工作得很好。我在线优化,我的路由器设置如下...

protocol -> all
port -> 5000
forward port to -> same as incoming port
host -> raspberrypi 
locate device by -> ipaddress

就像我说的,当我使用 python 的内置 wsgi 服务器时,这些设置在我的 pi 上工作得很好。当我在本地 运行 Gunicorn 时它工作得很好,当我在浏览器中键入 localhost:5000 时我可以看到我的应用程序,就在我在我的 pi 上设置它并尝试访问页面时外部 IP,如果我不使用 gunicorn,则外部 IP 工作正常。我想不通。有什么想法吗?

您需要让 Gunicorn 监听 0.0.0.0(所有网络接口)。这意味着它将侦听外部可访问的 IP 地址。

this post on ServerFault 中有更多关于 localhost0.0.0.0 的区别的信息。