[英]Why up and running web service returns 500 Internal Server Error?
我正在學習后端,並決定使用 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 服務:
[單位] 描述=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=多用戶.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 個必需的位置參數:“發送”
最后,解決了這個問題,所以 FastAPI 使用異步的ASGI<\/a> ,而我運行我的 FastAPI 應用程序的 gunicorn 使用同步的WSGI<\/a> 。 執行 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.