首页 > 解决方案 > Gunicorn 正在监听不存在的 sock 文件

问题描述

我正在尝试使用 Gunicorn 和 nginx 在 Amazon EC2 实例上提供 Flask 应用程序。因为我不在 Ubuntu 机器上,所以我一直在按照本教程进行一些细微的修改。EC2 实例正在运行 RHEL Fedora。我不确定为什么 nginx 没有将 80 上的流量路由到 Gunicorn。

当我激活我的虚拟环境并启动 Gunicorn 时:

gunicorn --bind 0.0.0.0:5000 --user ec2-user --workers 2 wsgi:app

我可以通过访问成功访问我的应用程序http://[mydomain].co:5000。但是,当我更改bind为收听 sock 文件时:

gunicorn --bind unix:/home/ec2-user/run/myApp.sock --user ec2-user --group ec2-user --workers 2 wsgi:app

并在我的添加以下文件/etc/nginx/sites-available

server {
        listen 80;
        server_name [mydomain].co www.[mydomain].co;

        location / {
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_pass http://unix:/home/ec2-user/run/myApp.sock;
        }
}

我无法访问我的网站。我最初有一种感觉,问题是显然没有创建 sock 文件,因为当我ls /home/ec2-user/run/目录为空时。我尝试让 gunicorn 监听127.0.0.1:5000并将我的站点 nginx conf 文件更改为proxy_pass http://127.0.0.1:5000只是看看这是否可行,但也没有运气。

我阅读了这个答案,其中提到了更改放置 sock 文件的文件夹的权限,但我认为这不是我的问题。该文件夹的权限是:

drwxrwxr-x 2 ec2-user ec2-user 4096 Jun 13 02:44 /home/ec2-user/run/

FWIW,当我开始将 gunicorn 绑定到 sock 文件时,它开始没有错误,并说它正在侦听该 sock 文件位置,唯一的问题是那里似乎没有任何文件,所以我很迷茫。我感谢任何和所有的帮助或澄清。

编辑 1

我的 EC2 实例的 Linux 发行版是 RHEL Fedora,所以我没有systemd链接教程中包含的任何相关命令。然而,我创建了一个服务文件,但它位于下面\etc\init.d\,当我运行它时\etc\init.d\myservice start\,输出是:

[2020-06-13 13:31:46 +0000] [24177] [INFO] Starting gunicorn 20.0.4
[2020-06-13 13:31:46 +0000] [24177] [INFO] Listening at: unix:/home/ec2-user/run/reformapp.sock (24177)
[2020-06-13 13:31:46 +0000] [24177] [INFO] Using worker: sync
[2020-06-13 13:31:46 +0000] [24180] [INFO] Booting worker with pid: 24180
[2020-06-13 13:31:46 +0000] [24181] [INFO] Booting worker with pid: 24181

然而,当我退出并运行时,ls /home/ec2-user/run/那里没有文件。

标签: nginxflaskamazon-ec2gunicorn

解决方案


这似乎是一个权限问题。在我的 ec2 机器上,我这样做并为我工作。

创建一个 systemd 服务文件

sudo nano /etc/systemd/system/myapi.service

文件内容如下:

[Unit]
Description=this is my test flask api
After=network.target

[Service]
User=ec2-user
Group=nginx
WorkingDirectory=/path/to/flaskapp
Environment="PATH=/path/to/flaskapp/.env/bin"
ExecStart=/path/to/flaskapp/.env/bin/gunicorn --workers 3 --bind unix:myapi.sock -m 007 wsgi
Restart=always

[Install]
WantedBy=multi-user.target

然后启动服务

sudo systemctl enable myapi
sudo systemctl start myapi
sudo systemctl status myapi

现在你应该可以在你的flaskapp目录中看到socket文件然后你可以创建一个nginx配置

sudo nano /etc/nginx/sites-available/myapi
sudo ln -s /etc/nginx/sites-available/myapi /etc/nginx/sites-enabled

myapi.conf

server {
    listen 8080;
    server_name _;

    location / {
        proxy_pass http://unix:/path/to/flaskapp/myapi.sock;
    }
}

server_name "_" 表示此服务器捕获所有可能的值。

sudo chmod 710 /home/ec2-user
sudo usermod -aG ec2-user nignx

重启服务并测试url

sudo nginx -t
sudo systemctl restart nginx
sudo systemctl status nginx
curl http://localhost:8080

推荐阅读