为什么up and 运行 web service returns 500 Internal Server Error?

Why up and running web service returns 500 Internal Server Error?

我正在学习后端并决定使用 FastAPI 编写一个简单的 Web 服务。这个想法是,前端向网络服务器发出请求,然后网络服务器与 Postgresql DB 通信以检索数据,然后网络服务器 returns 将该数据发送给前端。我正在使用 FastAPI 创建 Web 服务,使用 Nginx 将该服务公开到互联网,使用 Postgresql 存储数据。
这是该服务的代码:

from fastapi import FastAPI, HTTPException
import psycopg2
import time

app = FastAPI()

while True:
    try:
        # Connect to your postgres DB
        conn = psycopg2.connect(host='hostIP', database='postgres',user='postgres',password='passwd')

        # Open a cursor to perform database operations
        cursor = conn.cursor()
        print('DB connection was successfull!')
        break
    except Exception as error:
        print('Connection to DB failed')
        print("Error: ", error)
        time.sleep(2)

@app.get("/")
async def root():
    cursor.execute("""SELECT * FROM characters""")
    flags = cursor.fetchall()
    return {'data': flags}

hostIP = 是安装在 VirtualBox VM 上的我的 ubuntu 服务器的 IP 地址(出于安全目的,我没有在此处显示实际 IP)。所以我的网络服务和数据库都部署在这个虚拟机上。
然后我创建了一个 gunicorn.service 到 运行 fastAPI 服务:

[Unit] Description=Gunicorn Web Server as Unit Service Systemd After=network.target

[Service]
User=ubuntuserver
Group=ubuntuserver
WorkingDirectory=/home/ubuntuserver/test
Environment="PATH=/home/ubuntuserver/test/venv/bin"
ExecStart=/home/ubuntuserver/test/venv/bin/gunicorn --config /home/ubuntuserver/test/main.py main:app

[Install]
WantedBy=multi-user.target

对于 nginx,我配置 default.conf 文件如下:

server {
  listen 80;
  server_name hostIP;
  location / {
    proxy_pass http://localhost:8000;
  }
}

所以我希望在另一台计算机的浏览器上输入 hostIP:80 并获取数据库信息或至少一些成功消息等,但我却得到 内部服务器错误
在服务器端,我 运行 下面的命令来检查服务:

systemctl status postgresql 
systemctl status gunicorn.service  
systemctl status nginx  

而且它们似乎都正常激活。

下面我检查了服务使用的所有端口:

我是不是漏掉了什么?

编辑
在使用 Thomas 和 运行ning 的建议后,命令如下:

sudo journalctl -f -u nginx -u gunicorn  

我得到以下输出:

Feb 06 12:08:31 ubuntuserver gunicorn[3975]: [2022-02-06 12:08:31 +0000] [3975] [ERROR] Error handling request /
Feb 06 12:08:31 ubuntuserver gunicorn[3975]: Traceback (most recent call last):
Feb 06 12:08:31 ubuntuserver gunicorn[3975]: File "/home/ubuntuserver/test/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle
Feb 06 12:08:31 ubuntuserver gunicorn[3975]: self.handle_request(listener, req, client, addr)
Feb 06 12:08:31 ubuntuserver gunicorn[3975]:File "/home/ubuntuserver/test/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
Feb 06 12:08:31 ubuntuserver gunicorn[3975]: respiter = self.wsgi(environ, resp.start_response)
Feb 06 12:08:31 ubuntuserver gunicorn[3975]: TypeError: _call_() missing 1 required positional argument: 'send'

终于解决了这个问题,所以FastAPI使用了同步的ASGI which is asynchronous, and gunicorn, where I run my FastAPI application, uses WSGI。当执行 gunicorn 命令时,你应该添加 uvicorn 的工人。就我而言,我编辑了 gunicorn.service 文件:

[Unit] Description=Gunicorn Web Server as Unit Service Systemd After=network.target

[Service]
User=ubuntuserver
Group=ubuntuserver
WorkingDirectory=/home/ubuntuserver/test
Environment="PATH=/home/ubuntuserver/test/venv/bin"
ExecStart=/home/ubuntuserver/test/venv/bin/gunicorn --config /home/ubuntuserver/test/main.py main:app -w 4 -k uvicorn.workers.UvicornWorker

[Install]
WantedBy=multi-user.target

注意要使用uvicorn worker,需要先安装以下组件:

uvloop
httptools