套接字文件权限被拒绝 - NGINX + Gunicorn + Flask

问题描述 投票:0回答:1

编辑:我正在使用 Debian 更新:找到解决方案 - 我会在这里回答

我花了大约 5 个小时尝试进行设置,但就是做不到。

我正在尝试使用 nginx + Gunicorn 运行 Flask api,但我不断收到 sock 文件的权限被拒绝的消息。我尝试了这里每个帖子中的每个解决方案,但它对我不起作用。

/etc/systemd/system/app.service

[Unit]
Description=Gunicorn instance to serve licenses-server Flask app
After=network.target

[Service]
User=<root>
Group=www-data
WorkingDirectory=/home/<root>/services/licenses-server
Environment="PATH=/home/<root>/<app>/<app>/bin:/user/bind:/bin"
ExecStart=/home/<root>/services/licenses-server/licenses/bin/gunicorn --workers 3 --bind 
unix:/var/sockets/licenses.sock -m 007 wsgi:app
PrivateTmp=No

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/app.conf

server {
    listen 80;
    server_name mydomain.com www.mydomain.com;

    location / {
        try_files $uri $uri/ @flask;
    }

    location @flask {
        proxy_pass http://unix:/var/sockets/licenses.sock;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X_Forwared-For $proxy_add_x_forwarded_for;
    }
}

/var/log/nginx/error.log

2024/09/19 23:46:05 [crit] 17111#17111: *1 connect() to unix:/var/sockets/<my_sock>.sock failed (13: Permission denied) while connecting to upstream, client: 172.17.0.2, server: mydomain.com, request: "GET <my_url> HTTP/1.1", upstream: "http://unix:/var/sockets/licenses.sock:<my_url>", host: "mydomain.com"

权限

$ ls -l /
...
drwxrwxr-x  13 root www-data  4096 Sep 19 23:12 var

$ ls -l /var
...
drwxrwxrwx  2 root www-data 4096 Sep 19 23:43 sockets

$ ls -l /var/sockets
...
srwxrwx--- 1 <root> www-data 0 Sep 19 23:45 licenses.sock

/etc/nginx/nginx.conf

user nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-available/licenses-server.conf;
}

/etc/nginx/conf.d/default.conf

server {
  listen       80;
  server_name  localhost;

  access_log  /var/log/nginx/host.access.log  main;

  #location / {
      #root   /usr/share/nginx/html;
      #index  index.html index.htm;
  #}

  #error_page  404              /404.html;

  # redirect server error pages to the static page /50x.html
  #
  #error_page   500 502 503 504  /50x.html;
  #location = /50x.html {
      #root   /usr/share/nginx/html;
  #}

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass   http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #    root           html;
  #    fastcgi_pass   127.0.0.1:9000;
  #    fastcgi_index  index.php;
  #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  #    include        fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny  all;
  #}
}

如果我可以提供任何其他信息,请随时询问=)

python linux flask nginx gunicorn
1个回答
0
投票

在这里找到解决方案:nginx connet to .sock failed (13:Permission returned) - 502 bad gateway

我的 /etc/nginx/nginx.conf 文件使用默认且不存在的“nginx”用户。

user nginx;

所以我只是将其更改为我的用户:

user <my_user>;

希望对其他人有帮助! =)

© www.soinside.com 2019 - 2024. All rights reserved.