slash3b
slash3b

Reputation: 71

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

I am following this tutorial on how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04

When I'm trying to connect, Nginx gives me this error:

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

Here are socket permissions:

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

Running gunicorn:

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

Here is systemd service file:

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 conf file:

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;
    }
}

Running 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

So supposedly nginx worker (www-data) has an access to the socket but it looks like it doesn't?

It does not work event when I set socket permission to 777.

I can't understand what is going on. At least would be nice to somehow debug this unix socket. I seems that I'm able to connect to it with nc -U /path_to_socket and with socat but I do no understand how debug it and send requests.

Please help!

Update: I found a way to curl socket!

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>
...

Either way nginx is still failing with error.

Upvotes: 3

Views: 3174

Answers (1)

slash3b
slash3b

Reputation: 71

Huge thanks to metallic for a hint!

So the final permissions I was fighting with were drwxrw-r-- 5 slash3b www-data anyway it did not worked. Then I did su - www-data -s /bin/bash and became www-data user, and I tried to list files in the socket directory and it gave me permission error.

So the solution was to give www-data user permission to execute for the folder with socket file. I didn't realize execution bit must be on for this, what a silly mistake : )

Upvotes: 4

Related Questions