【问题标题】:Why up and running web service returns 500 Internal Server Error?为什么启动并运行 Web 服务返回 500 Internal Server Error?
【发布时间】:2022-02-06 20:56:19
【问题描述】:

我正在学习后端,并决定使用 FastAPI 编写一个简单的 Web 服务。这个想法是,前端向网络服务器发出请求,然后网络服务器与 Postgresql DB 通信以检索数据,然后网络服务器将该数据返回给前端。我使用 FastAPI 创建 Web 服务,使用 Nginx 将该服务公开到 Internet,使用 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 服务器作为单元服务 Systemd After=network.target

[服务]
用户=ubuntuserver
组=ubuntuserver
工作目录=/home/ubuntuserver/test
环境="PATH=/home/ubuntuserver/test/venv/bin"
ExecStart=/home/ubuntuserver/test/venv/bin/gunicorn --config /home/ubuntuserver/test/main.py main:app

[安装]
WantedBy=multi-user.target

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

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

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

systemctl status postgresql 
systemctl status gunicorn.service  
systemctl status nginx  

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

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

我错过了什么吗?

编辑
在使用 Thomas 的建议并运行以下命令后:

sudo journalctl -f -u nginx -u gunicorn  

我得到以下输出:

2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]: [2022-02-06 12:08:31 +0000] [3975] [ERROR] 错误处理请求 /
2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]:回溯(最后一次通话):
2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]:文件“/home/ubuntuserver/test/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py”,第 136 行,在句柄中
2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]: self.handle_request(listener, req, client, addr)
2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]:文件“/home/ubuntuserver/test/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py”,第 179 行,在 handle_request
2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]: respiter = self.wsgi(environ, resp.start_response)
2 月 6 日 12:08:31 ubuntuserver gunicorn[3975]: TypeError: _call_() 缺少 1 个必需的位置参数:'send'

【问题讨论】:

  • 日志中应该有更多的信息,例如运行sudo journalctl -f -u nginx -u gunicorn并刷新页面触发错误。

标签: python nginx gunicorn fastapi internal-server-error


【解决方案1】:

最后,解决了这个问题,所以 FastAPI 使用异步的 ASGI,而我运行我的 FastAPI 应用程序的 gunicorn 使用同步的 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 workers,需要先安装以下组件:

紫外线循环
http工具

【讨论】:

    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2023-03-30
    相关资源
    最近更新 更多