【问题标题】:Gunicorn listening to sock file that doesn't existGunicorn 正在监听不存在的 sock 文件
【发布时间】:2020-10-02 21:47:24
【问题描述】:

我正在尝试使用 Gunicorn 和 nginx 在 Amazon EC2 实例上提供 Flask 应用程序。因为我不在 Ubuntu 机器上,所以我一直在关注这个 tutorial 并稍作修改。 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 只是为了看看这是否可行但也没有运气。

我读到this answer 提到更改放置 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/ 时,那里没有文件。

【问题讨论】:

    标签: nginx flask amazon-ec2 gunicorn


    【解决方案1】:

    这似乎是一个权限问题。在我的 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
    

    【讨论】:

    • 感谢您的详细回答。不幸的是,我的 EC2 实例正在运行 RHEL Fedora,因此大多数这些 Linux 命令和位置都不存在。我没有systemdsystemctl 命令。我创建了一个类似于你的系统服务文件,但它位于/etc/init.d/myservice。我在原始帖子中添加了一些附加信息。
    【解决方案2】:

    我之前也遇到过同样的问题,但在任何地方都找不到结果。 我可以看到日志,它说的是权限问题。 即使给予适当的权限,问题仍然存在。

    所以我去尝试一下。

    解决方案非常简单。 在别处给出 .sock 文件的位置,例如 /run/gunicorn_1.sock。 在我的假设中,调用该文件的进程无法通过 ec2-user 目录。

    希望我节省了某人的时间和精力。

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2017-02-16
      • 2020-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      相关资源
      最近更新 更多