【问题标题】:Serving API via Flask / Gunicorn / Nginx: Connection refused通过 Flask / Gunicorn / Nginx 提供 API:连接被拒绝
【发布时间】:2019-10-26 09:58:17
【问题描述】:

我无法让 gunicorn 和 Nginx 协同工作并允许我通过烧瓶提供简单的 API:

在本地,运行 gunicorn 并从服务器获取响应工作正常:

gunicorn wsgi:app (start server)
[2019-06-11 23:12:48 +0000] [14615] [INFO] Starting gunicorn 19.9.0
[2019-06-11 23:12:48 +0000] [14615] [INFO] Listening at: http://127.0.0.1:8000 (14615)
[2019-06-11 23:12:48 +0000] [14615] [INFO] Using worker: sync
[2019-06-11 23:12:48 +0000] [14619] [INFO] Booting worker with pid: 14619


curl http://127.0.0.1:8000/predict (client call server for prediction)
output: "SERVER WORKS"

当我尝试使用 Nginx 时也会出现问题。

/etc/systemd/system/app.service

[Unit]
Description=Gunicorn instance to serve app
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/server
ExecStart=/usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/app

server {
    listen 80;
    server_name [SERVER_IP_ADDRESS];

    location / {
        include proxy_params;
        proxy_pass http://unix:/root/server/app.sock;
    }

}

我的 systemd 的状态看起来不错:

systemctl status app
● app.service - Gunicorn instance to serve app
   Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-06-11 23:24:07 UTC; 1s ago
 Main PID: 14664 (gunicorn)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/app.service
           ├─14664 /usr/bin/python /usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app
           └─14681 /usr/bin/python /usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app

systemd[1]: Started Gunicorn instance to serve app.
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Starting gunicorn 19.9.0
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Listening at: unix:app.sock (14664)
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Using worker: sync
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14681] [INFO] Booting worker with pid: 14681

当我向服务器发出请求时,我无法连接:

curl http://[SERVER_IP_ADDRESS]:80/predict

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>

编辑:

我尝试从/etc/nginx/sites-available/app 中删除server_name [SERVER_IP_ADDRESS];。我现在收到“欢迎使用 nginx!” http://SERVER_IP_ADDRESShttp://SERVER_IP_ADDRESS/predict 的“404 Not Found”

仅供参考,我的烧瓶应用只有一条路线,即“/predict”

【问题讨论】:

  • 主机上的80端口是否开放?
  • @BradenHolt 是的,它现在已经打开,我现在得到的是“502 Bad Gateway”
  • 502 Bad Gateway 可能是由于应用程序错误(即您的烧瓶应用程序)造成的。你能检查你的 Gunicorn 日志吗? sudo journalctl -n 100 -u gunicorn 可能会根据您的操作系统工作或检查您的系统日志。

标签: nginx flask gunicorn


【解决方案1】:

您似乎没有打开 80 端口,所以这里有一个快速的iptables 命令可以这样做:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

【讨论】:

  • 看起来端口现在已打开,但我收到“502 Bad Gateway”。我已经相应地更新了我的帖子
猜你喜欢
  • 2016-11-12
  • 2013-03-02
  • 2018-09-13
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2013-11-03
  • 2020-09-16
  • 2021-01-16
相关资源
最近更新 更多