如何修复 Nginx 连接到套接字失败(权限被拒绝)

How to fix Nginx connection to socket failed (Permission denied)

我正在 how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04

上学习本教程

当我尝试连接时,Nginx 给我这个错误:

2019/05/10 17:21:03 [crit] 922#922: *22 connect() to 
unix:/var/www/patria/flask/pirata.sock failed (13: Permission denied) 
while connecting to upstream, client: my_ip_address, server: 
digital_ocean_ip, request: "GET / HTTP/1.1", upstream: 
"http://unix:/var/www/patria/flask/pirata.sock:/", host: "digital_ocean_ip"

Note: there is no typo in path to the socket

这里是套接字权限:

root@ageispolis:/var/www/patria/flask# ll pirata.sock
srwxrwxr-- 1 slash3b www-data 0 May 10 16:43 pirata.sock=

运行 独角兽:

root@ageispolis:/var/www/patria/flask# ps -aux|grep gunicorn
slash3b    863  0.0  2.2  60000 22304 ?        Ss   16:43   0:00 /var/www/patria/pirata/bin/python3.6 /var/www/patria/pirata/bin/gunicorn --workers=3 --bind unix:pirata.sock -m 007 wsgi:pirata
slash3b   1036  0.0  2.7  99884 28024 ?        S    16:43   0:00 /var/www/patria/pirata/bin/python3.6 /var/www/patria/pirata/bin/gunicorn --workers=3 --bind unix:pirata.sock -m 007 wsgi:pirata
slash3b   1040  0.0  2.7  99884 28024 ?        S    16:43   0:00 /var/www/patria/pirata/bin/python3.6 /var/www/patria/pirata/bin/gunicorn --workers=3 --bind unix:pirata.sock -m 007 wsgi:pirata
slash3b   1041  0.0  2.7  99904 28024 ?        S    16:43   0:00 /var/www/patria/pirata/bin/python3.6 /var/www/patria/pirata/bin/gunicorn --workers=3 --bind unix:pirata.sock -m 007 wsgi:pirata
root      3008  0.0  0.1  13136  1056 pts/1    S+   17:39   0:00 grep --color=auto gunicorn

这里是systemd服务文件:

root@ageispolis:/var/www/patria/flask# cat /etc/systemd/system/pirata.service 
[Unit]
Description=Gunicors instance to serve pirata.com
After=network.target

[Service]
User=slash3b
Group=www-data
WorkingDirectory=/var/www/patria/flask
Environment="PATH=/var/www/patria/pirata/bin"
ExecStart=/var/www/patria/pirata/bin/gunicorn --workers=3 --bind unix:pirata.sock -m 007 wsgi:pirata

[Install]
WantedBy=multi-user.target

Nginx 配置文件:

root@ageispolis:/var/www/patria/flask# cat /etc/nginx/sites-enabled/pirata 
server {
    listen 80;
    server_name digital_ocean_ip;

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/patria/flask/pirata.sock;
    }
}

运行 nginx:

root@ageispolis:/var/www/patria/flask# ps -aux|grep nginx
root       921  0.0  0.1 140628  1504 ?        Ss   16:43   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   922  0.0  0.6 143300  6164 ?        S    16:43   0:00 nginx: worker process
root      3116  0.0  0.0  13136  1008 pts/1    S+   17:46   0:00 grep --color=auto nginx

所以假设 nginx worker (www-data) 可以访问套接字,但看起来没有?

当我将套接字权限设置为 777 时,它不起作用事件。

我不明白这是怎么回事。至少以某种方式调试这个 unix 套接字会很好。我似乎可以使用 nc -U /path_to_socketsocat 连接到它,但我不明白如何调试它和发送请求。

请帮忙!

更新: 我找到了卷曲套接字的方法!

root@ageispolis:/var/www/patria/flask# curl -H --unix-socket pirata.sock http
curl: (6) Could not resolve host: pirata.sock
curl: (6) Could not resolve host: http
root@ageispolis:/var/www/patria/flask# curl -v  --unix-socket pirata.sock http
* Rebuilt URL to: http/
*   Trying pirata.sock...
* Connected to http (pirata.sock) port 80 (#0)
> GET / HTTP/1.1
> Host: http
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: gunicorn/19.9.0
< Date: Fri, 10 May 2019 19:03:17 GMT
< Connection: close
< Content-Type: text/html; charset=utf-8
< Content-Length: 17326
<
<!doctype html>
<html lang="en">

<head>
...

无论哪种方式,nginx 仍然因错误而失败。

非常感谢 metallic 的提示!

所以我一直在争取的最终权限是 drwxrw-r-- 5 slash3b www-data 无论如何它没有用。 然后我做了 su - www-data -s /bin/bash 并成为 www-data 用户,我试图列出套接字目录中的文件,但它给了我权限错误。

所以解决方案是给www-data用户权限,让其对带有套接字文件的文件夹执行。我没有意识到执行位必须为此打开,多么愚蠢的错误:)