48

我的nginx-error.log文件中出现此错误:

2014/02/17 03:42:20 [crit] 5455#0: *1 connect() to unix:/tmp/uwsgi.sock failed (13: Permission denied) while connecting to upstream, client: xx.xx.x.xxx, server: localhost, request: "GET /users HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "EC2.amazonaws.com"

浏览器还显示 502 Bad Gateway Error。a的输出curl是一样的,Bad Gateway html

我试图通过将权限更改为/tmp/uwsgi.sock777 来修复它。那没有用。我也把自己加入了这个www-data小组(几个看起来相似的问题暗示了这一点)。此外,没有骰子。

这是我的nginx.conf文件:

nginx.conf

worker_processes 1;
worker_rlimit_nofile 8192;

events {
  worker_connections  3000; 
}

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

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-enabled/*;
}

我正在用 Nginsx 和 Uwsgi 运行一个 Flask 应用程序,只是为了在我的解释中透彻。如果有人有任何想法,我将不胜感激。


编辑

我被要求提供我的 uwsgi 配置文件。所以,我从来没有亲自写过我的 nginx 或我的 uwsgi 文件。我按照这里的指南使用 ansible-playbook 设置所有内容。该nginx.conf文件是自动生成的,但/etc/uwsgi除了和文件夹中的README文件外,没有任何内容。我需要为 uwsgi 创建自己的配置文件吗?我的印象是 ansible 处理了所有这些事情。apps-enabledapps-available

我相信ansible-playbook自从我运行此命令以来,我的 uwsgi 配置就知道了

uwsgi -s /tmp/uwsgi.sock -w my_app:app

它启动并输出:

*** Starting uWSGI 2.0.1 (64bit) on [Mon Feb 17 20:03:08 2014] ***
compiled with version: 4.7.3 on 10 February 2014 18:26:16
os: Linux-3.11.0-15-generic #25-Ubuntu SMP Thu Jan 30 17:22:01 UTC 2014
nodename: ip-10-9-xxx-xxx
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/username/Project
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 4548
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Python version: 2.7.5+ (default, Sep 19 2013, 13:52:09)  [GCC 4.8.1]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1f60260
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 3 seconds on interpreter 0x1f60260 pid: 26790 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 26790, cores: 1)
4

7 回答 7

55

出现权限问题是因为 uwsgi 将 /tmp/uwsgi.sock 的所有权和权限重置为 755 以及每次 uwsgi 启动时运行 uwsgi 的用户。

解决问题的正确方法是让 uwsgi 更改 /tmp/uwsgi.sock 的所有权和/或权限,以便 nginx 可以写入此套接字。因此,存在三种可能的解决方案。

  1. 以 www-data 用户身份运行 uwsgi,以便该用户拥有由它创建的套接字文件。

    uwsgi -s /tmp/uwsgi.sock -w my_app:app --uid www-data --gid www-data
    
  2. 更改套接字文件的所有权,以便 www-data 拥有它。

    uwsgi -s /tmp/uwsgi.sock -w my_app:app --chown-socket=www-data:www-data
    
  3. 更改套接字文件的权限,以便 www-data 可以对其进行写入。

    uwsgi -s /tmp/uwsgi.sock -w my_app:app --chmod-socket=666
    

我更喜欢第一种方法,因为它不会让 uwsgi 以 root 身份运行。

前两个命令需要以 root 用户身份运行。第三个命令不需要以 root 用户身份运行。

第一个命令让 uwsgi 以 www-data 用户身份运行。第二个和第三个命令让 uwsgi 以运行该命令的实际用户身份运行。

第一个和第二个命令只允许 www-data 用户写入套接字。第三个命令允许任何用户写入套接字。

我更喜欢第一种方法,因为它不会让 uwsgi 以 root 用户身份运行,并且不会使套接字文件 world-writeable 。

于 2014-03-25T16:36:52.753 回答
14

虽然公认的解决方案是正确的,但 SELinux 也可能会阻止访问。如果您确实正确设置了权限,但仍然收到权限被拒绝消息,请尝试:

sudo setenforce Permissive

如果它可以工作,那么 SELinux 就有问题——或者不如预期的那样工作!添加所需的权限nginx

  # to see what permissions are needed.
sudo grep nginx /var/log/audit/audit.log | audit2allow
  # to create a nginx.pp policy file
sudo grep nginx /var/log/audit/audit.log | audit2allow -M nginx
  # to apply the new policy
sudo semodule -i nginx.pp

之后将 SELinux 策略重置为强制执行:

sudo setenforce Enforcing
于 2018-03-26T13:28:26.103 回答
3

您必须在 uWSGI 配置中设置这些权限 ( chmod/ )。chown

它是chmod-socketchown-socket

http://uwsgi-docs.readthedocs.org/en/latest/Options.html#chmod-socket http://uwsgi-docs.readthedocs.org/en/latest/Options.html#chown-socket

于 2014-02-17T11:08:13.170 回答
1

我知道为时已晚,但它可能对其他人有所帮助。我会建议使用带有 virtualenv、uwsgi 和 nginx 的 Running flask非常简单和甜蜜的文档。

如果您在 virtualenv 中运行项目,则必须激活您的环境。

这是yolo.py

from config import application

if __name__ == "__main__":
    application.run(host='127.0.0.1')

并在 /tmp/ 目录中创建 uwsgi.sock 文件并将其留空。正如@susanpal 回答所说,“出现权限问题是因为 uwsgi 将 /tmp/uwsgi.sock 的所有权和权限重置为 755,并且每次 uwsgi 启动时用户都在运行 uwsgi。” 它是正确的。

因此,每当 uwsgi 启动时,您都必须授予 sock 文件的权限。所以现在按照以下命令

uwsgi -s /tmp/uwsgi.sock -w yolo:application -H /var/www/yolo/env --chmod-socket=666 

与@susanpal 有点不同的命令。对于持久连接,只需在命令末尾添加“ & ”即可

uwsgi -s /tmp/uwsgi.sock -w yolo:app -H /var/www/yolo/env --chmod-socket=666 &
于 2015-06-28T16:29:43.147 回答
1

在我的情况下,改变一些 php 权限就可以了

sudo chown user:group -R /run/php

我希望这可以帮助别人。

于 2019-07-29T08:24:35.587 回答
0

任何从 Google 来到这里并尝试在安装 nginx 后使用默认 Ubuntu 映像在 AWS 上运行 Flask 并且仍然无法弄清楚问题所在的人:

Nginx 默认以用户“www-data”运行,但 Digital Ocean 中最常见的 Flask WSGI 教程让您使用登录用户获取 systemd 服务文件。如果您的 Flask/wsgi 用户是“ubuntu”,那么在 /etc/nginx/nginx.conf 中将运行 nginx 的用户从“www-data”(这是默认值)更改为“ubuntu”,一切都将开始工作。您可以使用脚本中的一行来执行此操作:

sudo sed -i 's/user www-data;/user ubuntu;/' /etc/nginx/nginx.conf

试图让 Flask 和 uwsgi 作为 www-data 运行并没有立即奏效,但是让 nginx 作为 ubuntu 运行工作得很好,因为无论如何我使用这个实例运行的都是 Flask。

于 2021-11-16T16:00:21.173 回答
-1

您应该为您的应用程序发布 nginx 和 uwsgi 配置文件(在 /etc/nginx/sites-enabled/ 和 /etc/uwsgi/ 中的那些 - 或者您放置它们的任何位置)。

通常检查您的 nginx 应用程序配置中是否有类似于以下行的行:

uwsgi_pass unix:///tmp/uwsgi.sock;

和你的 uwsgi 配置文件中相同的套接字名称:

socket=/tmp/uwsgi.sock
于 2014-02-17T13:50:12.300 回答