如何在 Heroku 上设置 Nginx 以处理从端口 80 到端口 5000 的流量?
How do I set up Nginx on Heroku to serve traffic from port 80 to port 5000?
更新:显然,端口没有问题。我能够重新绑定端口并在端口 80 上提供我的应用程序,但我没有解决问题。问题是我的应用程序实际上没有在 Heroku 上正确部署。这是通过 Heroku 构建日志显示的内容:
原始问题:我在尝试在 Heroku 上设置 Flask 应用程序时遇到问题。我想在端口 5000 上安装 Flask 服务器 运行,并设置一个 Nginx 代理来服务从端口 80 到端口 5000 的流量,这样我仍然可以从 Heroku 访问网页(默认情况下服务端口 80 上的流量) ).
目前我已将 heroku-buildpack-runit 和 heroku-buildpack-nginx 添加到我的 Heroku 配置中,但我不确定如何正确启动 Nginx 以执行我想要的操作。我的 Procfile 看起来像:
web: bin/start-nginx gunicorn app:app
这不是我希望它做的事情。事实上,我不确定它是否真的在做任何事情。
注意,gunicorn 是我用来启动我的 Flask 应用程序的工具。
有什么想法吗?
在 Heroku 上,您无法选择使用的端口。您必须使用 Heroku 通过 PORT
环境变量分配给您的端口。 运行 Flask 也不需要 Nginx。只需使用 gunicorn
或其他 WSGI 网络服务器。
来自 the documentation(强调已添加):
Web servers
Outside of Heroku, web apps are sometimes executed inside a web server container. For example, PHP apps might run as a module inside Apache HTTPD, or Java apps might run inside Tomcat.
On Heroku, apps are completely self-contained and do not rely on runtime injection of a webserver into the execution environment to create a web-facing service. Each web process simply binds to a port, and listens for requests coming in on that port. The port to bind to is assigned by Heroku as the PORT
environment variable.
This is typically implemented by using dependency declaration to add a webserver library to the app, such as Tornado for Python, Unicorn for Ruby, or Jetty for Java and other JVM-based languages. This happens entirely in user space, that is, within the app’s code. The contract with Heroku is for the process to bind to a port to serve requests. Heroku’s routers are then responsible for directing HTTP requests to the process on the right port.
使用环境变量(如果存在)的一种简单方法是执行以下操作:
import os
# Set up your Flask app here
port = os.getenv('PORT', default='5000')
app.run('0.0.0.0', port=port)
看起来你已经 using gunicorn
,这太棒了。您的 Procfile
应包含如下内容:
web: gunicorn my_project.wsgi
其中 my_project.wsgi
是您的 WSGI 文件的名称。
更新:显然,端口没有问题。我能够重新绑定端口并在端口 80 上提供我的应用程序,但我没有解决问题。问题是我的应用程序实际上没有在 Heroku 上正确部署。这是通过 Heroku 构建日志显示的内容:
原始问题:我在尝试在 Heroku 上设置 Flask 应用程序时遇到问题。我想在端口 5000 上安装 Flask 服务器 运行,并设置一个 Nginx 代理来服务从端口 80 到端口 5000 的流量,这样我仍然可以从 Heroku 访问网页(默认情况下服务端口 80 上的流量) ).
目前我已将 heroku-buildpack-runit 和 heroku-buildpack-nginx 添加到我的 Heroku 配置中,但我不确定如何正确启动 Nginx 以执行我想要的操作。我的 Procfile 看起来像:
web: bin/start-nginx gunicorn app:app
这不是我希望它做的事情。事实上,我不确定它是否真的在做任何事情。
注意,gunicorn 是我用来启动我的 Flask 应用程序的工具。
有什么想法吗?
在 Heroku 上,您无法选择使用的端口。您必须使用 Heroku 通过 PORT
环境变量分配给您的端口。 运行 Flask 也不需要 Nginx。只需使用 gunicorn
或其他 WSGI 网络服务器。
来自 the documentation(强调已添加):
Web servers
Outside of Heroku, web apps are sometimes executed inside a web server container. For example, PHP apps might run as a module inside Apache HTTPD, or Java apps might run inside Tomcat.
On Heroku, apps are completely self-contained and do not rely on runtime injection of a webserver into the execution environment to create a web-facing service. Each web process simply binds to a port, and listens for requests coming in on that port. The port to bind to is assigned by Heroku as the
PORT
environment variable.This is typically implemented by using dependency declaration to add a webserver library to the app, such as Tornado for Python, Unicorn for Ruby, or Jetty for Java and other JVM-based languages. This happens entirely in user space, that is, within the app’s code. The contract with Heroku is for the process to bind to a port to serve requests. Heroku’s routers are then responsible for directing HTTP requests to the process on the right port.
使用环境变量(如果存在)的一种简单方法是执行以下操作:
import os
# Set up your Flask app here
port = os.getenv('PORT', default='5000')
app.run('0.0.0.0', port=port)
看起来你已经 using gunicorn
,这太棒了。您的 Procfile
应包含如下内容:
web: gunicorn my_project.wsgi
其中 my_project.wsgi
是您的 WSGI 文件的名称。