提问者:小点点

504连接错误Flask Nginx uWSGI Ubuntu


大家好,我希望我能得到一些关于这件事的指导。我有一个在 ubuntu 服务器上设置的烧瓶应用程序。它使用 ssh 创建一个通往 Centos 7 服务器的隧道,该服务器拥有它的 mysql 数据库。在 Ubuntu 服务器上使用 python 运行此应用程序后,我能够完美地登录到我的应用程序并从域 ip 查看数据库中的数据。现在,在尝试在nginx和uWSGI上运行该应用程序时,我实际上可以从我的域名进入登录页面。但是在输入我的凭据并尝试登录时,页面加载了大约一分钟,并且我收到 504 连接超时错误

我会收到这个吗?因为我的应用程序在处理我的数据时试图连接到另一台服务器。我不确定,还没有任何帮助。这是我的文件。

服务器块

server {
    listen 80;
    server_name itinareport.tk www.itinareport.tk;

    location / {
        uwsgi_read_timeout 600;
        include uwsgi_params;
        uwsgi_pass unix:/home/pinchrep2/itinarep/itinarep.sock;
    }
}

ini文件

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = itinarep.sock
chmod-socket = 660
vacuum = true

die-on-term=true

wsgi.py

from main import  app


if __name__ == "__main__":
    app.run()

服务文件

[Unit]
Description=uWSGI instance to serve itinarep
After=network.target

[Service]
User=pinchrep2
Group=www-data
WorkingDirectory=/home/pinchrep2/itinarep
Environment="PATH=/home/pinchrep2/itinarep/it_venv/bin"
ExecStart=/home/pinchrep2/itinarep/it_venv/bin/uwsgi --ini itinarep.ini

[Install]
WantedBy=multi-user.target

这是我从主 py 文件 main.py ssh 的地方

sshforward = SSHTunnelForwarder(
    ("public ip", 22),
   ssh_username = 'user',
   ssh_password = 'pass',
   remote_bind_address = ('127.0.0.1', 3306)
)
sshforward.start()
local_port = sshforward.local_bind_port

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://root@localhost:{local_port}/asteriskcdrdb"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

再次,我只需要部署这个。请指向正确的配置方向。我可以进入应用程序,但一登录就收到这个问题。


共1个答案

匿名用户

当您的数据库连接 url 引用“localhost”时,它实际上是通过 unix 套接字连接的。

您可以使用包含unix套接字的local_bind_address连接,添加<code>?unix_socket=/path/to/mysql.sock到SQLALCHEMY_DATABASE_URI,就像这个答案。

似乎连接到远程unix套接字正在等待实现此上游问题。