【问题标题】:Running Dash on a public IP : werkzeug.exceptions.NotFound: 404 Not Found在公共 IP 上运行 Dash:werkzeug.exceptions.NotFound: 404 Not Found
【发布时间】:2022-11-10 12:29:55
【问题描述】:

我正在尝试在可使用公共 IP(在 NAT 后面)访问的 VM 上运行 Dash 应用程序。

这是基本的 Dash 应用程序:

from dash import Dash, html
import dash_bootstrap_components as dbc
import dash_daq as daq
from dash import Input, Output, State, dcc
from flask import Flask, url_for
import os
import numpy as np

server = Flask(__name__)
server.config.update(
    SERVER_NAME=os.getenv("SERVER_NAME", "localhost:8080")
)
app = Dash(__name__,server=server, external_stylesheets=[dbc.themes.BOOTSTRAP, \
dbc.icons.BOOTSTRAP])

app.layout = html.Div(children=[    html.H1(children='Test'),    html.Div(    '\
''        Built with Dash: A web application framework for Python.    ''')])

if __name__ == '__main__':
    app.run_server(debug=True,host='0.0.0.0',port=8080)

如果我使用python test.py 运行应用程序,当我浏览 http://my_public_IP:8080 时,它会返回:werkzeug.exceptions.NotFound: 404 Not Found

如果我使用gunicorn --workers 1 --bind 0.0.0.0:8080 test:server 运行应用程序,它会返回经典: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

以下日志由 gunicorn 生成:

[2022-09-20 09:25:50 +0000] [62184] [INFO] Starting gunicorn 20.1.0
[2022-09-20 09:25:50 +0000] [62184] [INFO] Listening at: http://0.0.0.0:8080 (62184)
[2022-09-20 09:25:50 +0000] [62184] [INFO] Using worker: sync
[2022-09-20 09:25:50 +0000] [62186] [INFO] Booting worker with pid: 62186
/home/azure-vm-user/.local/lib/python3.8/site-packages/flask/app.py:2218: UserWarning: Current server name 'my_public_IP:8080' doesn't match configured server name 'localhost:8080'
  return self.url_map.bind_to_environ(

谢谢你的帮助。我可能在这里遗漏了一些简单的东西,但在 SO 中找不到任何等效的东西,除了这篇没有解决我的问题的帖子: Not able to deploy dash application on gunicorn

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python flask gunicorn


【解决方案1】:

如果你完全省略了SERVER_NAME 配置变量的设置会发生什么?

Flask/Werkzeug 会检查您在应用程序配置中设置的 SERVER_NAME 是否与作为 HTTP 请求的一部分发送到应用程序的 HTTP 标头的 Host 部分匹配。如果不匹配,您会收到您在日志中观察到的 UserWarning 消息,并且路由器的 subdomain URL 部分在内部设置为“<invalid>”,这会导致所有路由有意返回 404 错误。

请参阅werkzeug.routing.Map.bind_to_environ() 的以下代码摘录:

            if subdomain is None and not self.host_matching:
            cur_server_name = wsgi_server_name.split(".")
            real_server_name = server_name.split(".")
            offset = -len(real_server_name)

            if cur_server_name[offset:] != real_server_name:
                # This can happen even with valid configs if the server was
                # accessed directly by IP address under some situations.
                # Instead of raising an exception like in Werkzeug 0.7 or
                # earlier we go by an invalid subdomain which will result
                # in a 404 error on matching.
                warnings.warn(
                    f"Current server name {wsgi_server_name!r} doesn't match configured"
                    f" server name {server_name!r}",
                    stacklevel=2,
                )
                subdomain = "<invalid>"
            else:
                subdomain = ".".join(filter(None, cur_server_name[:offset]))

在您的情况下,您正在绑定应用程序服务器以使用 0.0.0.0 指令监听主机上的所有可用接口 app.run_server(debug=True,host='0.0.0.0',port=8080) 如果其中一个接口与地址 my_public_IP 匹配,则您的应用程序应该可以在没有任何 SERVER_NAME 设置的情况下访问,并且路由器应该能够匹配定义的路由。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2017-11-25
    • 2015-06-27
    • 2012-09-04
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多