【问题标题】:uwsgi uwsgi flask app and nginx gives 404 not founduwsgi uwsgi flask app 和 nginx 给出 404 not found
【发布时间】:2022-01-09 20:23:57
【问题描述】:

我卡住了。我想在我的 vps 上部署 python dash 应用程序。我按照本教程开始使用烧瓶应用程序: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

所以我创建了服务 myproject。检查状态似乎没问题:

我的 nginx 配置:

server {

    listen       443 ssl http2;
    #listen [::]:443 ssl http2;
    server_name  www.mysite.com mysite.com;
    root         /var/www/wordpress;
    index        index.php;

    access_log   /var/log/nginx/wordpress.log;
    error_log    /var/log/nginx/wordpress_error.log  error;

    ssl_certificate_key /etc/nginx/ssl/www.XXX.key;
    ssl_certificate     /etc/nginx/ssl/www.XXX.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    add_header Strict-Transport-Security max-age=31536000;


    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }


    location /shiny/ {
       rewrite ^/shiny/(.*)$ /$1 break;
       proxy_pass http://127.0.0.1:3838;
       proxy_redirect http://127.0.0.1:3838 $scheme://$host/shiny/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $connection_upgrade;

       proxy_temp_file_write_size 64k;
       proxy_connect_timeout 10080s;
       proxy_send_timeout 10080;
       proxy_read_timeout 10080;
       proxy_buffer_size 64k;
       proxy_buffers 16 32k;
       proxy_busy_buffers_size 64k;
       proxy_redirect off;
       proxy_request_buffering off;
       proxy_buffering off;
    }
    location /rstudio/ {

       rewrite ^/rstudio/(.*)$ /$1 break;
       proxy_pass http://127.0.0.1:8787;
       proxy_redirect http://127.0.0.1:8787 $scheme://$host/rstudio/;
       #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       #proxy_set_header X-Real-IP $remote_addr;
       #proxy_set_header Host $http_host;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $connection_upgrade;
       proxy_http_version 1.1;
       #proxy_redirect off;
       proxy_buffering off;
       #proxy_set_header Upgrade $http_upgrade;
       #proxy_set_header Connection "upgrade";
       proxy_read_timeout 86400;
    }


   location /myproject/ {
        uwsgi_pass unix:/home/XXX/XXX/python_dash/myproject/myproject.sock;
        include uwsgi_params;
        }

}

但是当我访问 mysite.com/myproject/ 时,我得到:

但它与常规 nginx 不同(出现拼写错误):

所以有些事情正在发生。它在 myproject 服务状态上也可见。每次我尝试访问它时,它都会在底部显示一行:

[pid: 1696|app: 0|req: 1/1] 213.134.X.XX () {58 vars in 1334 bytes} [Fri Dec  3 13:22:08 2021] GET /myproject/ => generated 232 bytes in 40 msecs (HTTP/2.0 404) 2 headers in 87 bytes (2 switches on core 0)

我的 myproject.ini:

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = myproject.sock
chmod-socket = 666
vacuum = true

die-on-term = true

你能给我一个提示吗?我被卡住了,无法解决...

最好的问候 托马斯

【问题讨论】:

  • 我怀疑是你的烧瓶应用返回 404,你可以尝试运行:curl -vL --no-buffer -XGET --unix-socket /home/XXX/XXX/python_dash/myproject/myproject.sock http://localhost/myproject/
  • Taek,这是响应: 注意:不必要地使用 -X 或 --request,GET 已经被推断出来。 * 正在尝试 /home/lemur/Knight_Frank/python_dash/myprojec:0... * 连接到 localhost (myproject.sock) 端口 80 (#0) > GET /myproject/ HTTP/1.1 > 主机:localhost > User-Agent:curl /7.68.0 > 接受:/ > * 来自服务器的空回复 * 到主机 localhost 的连接 #0 保持原样 curl:(52)来自服务器的空回复
  • @Taek,有什么想法可以继续吗?
  • 没有线索,抱歉。也许尝试使用 proxy_pass over http 而不是套接字进行调试。

标签: nginx flask uwsgi


【解决方案1】:

终于找到了!

@app.route 必须与 nginx 配置中的位置匹配

所以在我的情况下:

location /myproject/

应用路径必须是:

@app.route("/myproject/")

记住尾随“/”,因为如果你不添加它,如果你在地址栏中输入尾随“/”,它会返回 not found(比如 https://website.com/myproject/ 不起作用,只有 https://website.com/myproject

python dash 也有类似的情况,但这次@app.route(或@server.route)不起作用。您需要在 dash.Dash(...) 中添加 routes_pathname_prefix="/myproject/"。

我不明白为什么它会这样,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 2019-11-08
    • 1970-01-01
    • 2018-08-17
    • 2015-02-09
    • 2021-05-24
    • 2018-01-25
    • 1970-01-01
    相关资源
    最近更新 更多