我在 Nginx 服务器 Ubuntu 21.10 实例上尝试部署我的烧瓶应用程序时得到了经典的 "Permission denied while connecting to upstream"

I'm getting the classic "Permission denied while connecting to upstream" while trying to deploy my flask app on an Nginx server, Ubuntu 21.10 instance

我知道这个问题已被问过一百万次,而且我几乎每一次 'solution' 都试过了,但都无济于事。我在想我的问题可能是独一无二的。

uwsgi.ini:

module = wsgi:app
master = true
processes = 5
socket = elekto.sock
chmod-socket = 666
vacuum = true
die-on-term = true
logto = /home/richardgrecoson/elekto/elekto.log

/etc/nginx/sites-enabled/elekto :

server {
    listen 80;
    server_name my_ip;
location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/richardgrecoson/elekto/elekto.sock;
    }
}

/etc/systemd/system/elekto.服务

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=richardgrecoson
Group=www-data
WorkingDirectory=/home/richardgrecoson/elekto
Environment="PATH=/home/richardgrecoson/elekto/.venv/bin"
ExecStart=/home/richardgrecoson/elekto/.venv/bin/uwsgi --ini uwsgi.ini

[Install]
WantedBy=multi-user.target

不允许您的 NGINX r/w/x 套接字文件。让我们尝试一些东西。

将socket的权限改成0777,用ls /home/richardgrecoson/elekto/elekto.sock检查

一般来说,允许 NGINX 的运行用户 r/w/x 套接字是很重要的。 nginx的runuser是“正常”nginx。通过键入 ps -elf |grep nginx

进行检查

如果是 NGINX,我会将套接字的权限改回 660,并将用户和组更改为 nginx

有了这个,nginx 用户将可以访问套接字。

如果您想为套接字使用与 nginx 不同的运行用户,请创建一个新用户并将其添加到 nginx 组 usermod -a -G nginx yourusername。这也适用于 660