在运行nginx的实际服务器上使用waiter提供dash应用程序时,我发现了404个错误

pb3skfrl  于 2023-01-20  发布在  Nginx
关注(0)|答案(1)|浏览(191)

我试图在一个实际的服务器上提供一个dash应用程序-本地运行工作正常-这是运行nginx,有一个ssl证书和代理传递传入的流量到我在waitress.serve中指定的端口。
我可以运行一个试用的flask应用程序,但是尝试服务一个dash应用程序除了Loading...之外不会返回任何东西。
设置如下:
/etc/nginx/sites-enabled/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    ## Redirects all traffic to the HTTPS host
    root /nowhere; ## root doesn't have to be a valid path since we are redirecting
    rewrite ^ https://$host$request_uri? permanent;
}
server{
    # SSL configuration
    #
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    ## Strong SSL Security
    ssl on;
    ssl_certificate /etc/letsencrypt/live/FQDN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/FQDN/privkey.pem;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_verify_client off;
    ssl_protocols  TLSv1.2 TLSv1.3;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_prefer_server_ciphers   on;
    add_header Strict-Transport-Security max-age=31536000;
    add_header X-Content-Type-Options nosniff;
    
    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name FQDN;

    location / {

        proxy_pass http://IP:8050;
        proxy_set_header X-Real-IP $remote_addr;

        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}

在服务器上的主目录中-我没有以root身份登录-我有(测试)应用程序的代码。
尝试为以下测试应用提供服务时,运行完美:

from flask import Flask
from waitress import serve

app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'> A very simple flask server !</h1>"

if __name__ == "__main__":
    serve(app, host='0.0.0.0', port=8050, url_scheme='https')

但是,尝试提供以下MWE测试应用程序失败:

from flask import Flask
from waitress import serve
from dash import Dash
import dash_core_components as dcc
import dash_html_components as html

server = Flask(__name__)
app = Dash(
    __name__,
    server=server,
    url_base_pathname='/'
)
                          
app.layout = html.Div(
    html.H1(
        "A very simple dash app on a flask server !",
        style={'color': 'blue'},
    ),

)

@server.route("/")
def my_dash_app():
    return app.index()

if __name__ == "__main__":
    serve(server, host='0.0.0.0', port=8050, url_scheme='https')

我使用的是带有ssh插件的VSCode,所以我有一个自然的ssh隧道,通过它我可以访问http://localhost:8050/上的后一个应用程序。
在检查view-source:https://FQDN/上失败服务器的源代码时,我发现以下内容:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Dash</title>
        <link rel="icon" type="image/x-icon" href="/_favicon.ico?v=2.6.1">
        
    </head>
    <body>
        
<div id="react-entry-point">
    <div class="_dash-loading">
        Loading...
    </div>
</div>

        <footer>
            <script id="_dash-config" type="application/json">{"url_base_pathname":"/","requests_pathname_prefix":"/","ui":false,"props_check":false,"show_undo_redo":false,"suppress_callback_exceptions":false,"update_title":"Updating...","children_props":{"dash_core_components":{"Checklist":["options[].label"],"Clipboard":[],"ConfirmDialog":[],"ConfirmDialogProvider":[],"DatePickerRange":[],"DatePickerSingle":[],"Download":[],"Dropdown":["options[].label"],"Graph":[],"Input":[],"Interval":[],"Link":[],"Loading":[],"Location":[],"LogoutButton":[],"Markdown":[],"RadioItems":["options[].label"],"RangeSlider":[],"Slider":[],"Store":[],"Tab":[],"Tabs":[],"Textarea":[],"Tooltip":[],"Upload":[]},"dash_html_components":{"A":[],"Abbr":[],"Acronym":[],"Address":[],"Area":[],"Article":[],"Aside":[],"Audio":[],"B":[],"Base":[],"Basefont":[],"Bdi":[],"Bdo":[],"Big":[],"Blink":[],"Blockquote":[],"Br":[],"Button":[],"Canvas":[],"Caption":[],"Center":[],"Cite":[],"Code":[],"Col":[],"Colgroup":[],"Content":[],"Data":[],"Datalist":[],"Dd":[],"Del":[],"Details":[],"Dfn":[],"Dialog":[],"Div":[],"Dl":[],"Dt":[],"Em":[],"Embed":[],"Fieldset":[],"Figcaption":[],"Figure":[],"Font":[],"Footer":[],"Form":[],"Frame":[],"Frameset":[],"H1":[],"H2":[],"H3":[],"H4":[],"H5":[],"H6":[],"Header":[],"Hgroup":[],"Hr":[],"I":[],"Iframe":[],"Img":[],"Ins":[],"Kbd":[],"Keygen":[],"Label":[],"Legend":[],"Li":[],"Link":[],"Main":[],"MapEl":[],"Mark":[],"Marquee":[],"Meta":[],"Meter":[],"Nav":[],"Nobr":[],"Noscript":[],"ObjectEl":[],"Ol":[],"Optgroup":[],"Option":[],"Output":[],"P":[],"Param":[],"Picture":[],"Plaintext":[],"Pre":[],"Progress":[],"Q":[],"Rb":[],"Rp":[],"Rt":[],"Rtc":[],"Ruby":[],"S":[],"Samp":[],"Script":[],"Section":[],"Select":[],"Shadow":[],"Slot":[],"Small":[],"Source":[],"Spacer":[],"Span":[],"Strike":[],"Strong":[],"Sub":[],"Summary":[],"Sup":[],"Table":[],"Tbody":[],"Td":[],"Template":[],"Textarea":[],"Tfoot":[],"Th":[],"Thead":[],"Time":[],"Title":[],"Tr":[],"Track":[],"U":[],"Ul":[],"Var":[],"Video":[],"Wbr":[],"Xmp":[]},"dash_table":{"DataTable":[]}}}</script>
            <script src="/_dash-component-suites/dash/deps/polyfill@7.v2_6_1m1663249656.12.1.min.js"></script>
<script src="/_dash-component-suites/dash/deps/react@16.v2_6_1m1663249656.14.0.min.js"></script>
<script src="/_dash-component-suites/dash/deps/react-dom@16.v2_6_1m1663249656.14.0.min.js"></script>
<script src="/_dash-component-suites/dash/deps/prop-types@15.v2_6_1m1663249656.8.1.min.js"></script>
<script src="/_dash-component-suites/dash/dash-renderer/build/dash_renderer.v2_6_1m1663249656.min.js"></script>
<script src="/_dash-component-suites/dash/dcc/dash_core_components.v2_6_1m1663249656.js"></script>
<script src="/_dash-component-suites/dash/dcc/dash_core_components-shared.v2_6_1m1663249656.js"></script>
<script src="/_dash-component-suites/dash/html/dash_html_components.v2_0_5m1663249656.min.js"></script>
<script src="/_dash-component-suites/dash/dash_table/bundle.v5_1_5m1663249656.js"></script>
            <script id="_dash-renderer" type="application/javascript">var renderer = new DashRenderer();</script>
        </footer>
    </body>
</html>

已经无法加载图标-检查view-source:https://FQDN/_favicon.ico?v=2.6.1

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

也就是说,路径似乎出了问题,但我对w.r.t. nginx/flask/dash了解不够,欢迎任何提示。
多谢了!

kmb7vmvb

kmb7vmvb1#

终于找到答案了,好像是我的nginx配置有问题,下面是一个可以用的:

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;

    ssl_certificate /path/to/ssl/certificate.crt;
    ssl_certificate_key /path/to/ssl/private.key;

    location / {
        proxy_pass http://localhost:8050;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

相关问题