Nginx的错误:(13:权限被拒绝),而连接到上游

问题描述:

我得到这个错误在我nginx-error.log文件:Nginx的错误:(13:权限被拒绝),而连接到上游

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错误网关错误。 curl的输出是一样的,坏网关html

我试图通过更改/tmp/uwsgi.sock到777的权限来解决它。这没有奏效。我也加入了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烧瓶中的应用,只是在我的解释透彻。如果有人有任何想法,我会很感激他们。


编辑

我被要求提供本人uwsgi的配置文件。所以,我从来没有亲自写过我的nginx或我的uwsgi文件。我遵循了指南here,该指南使用完整的剧本来设置一切。 nginx.conf文件自动生成,但/etc/uwsgi文件中除apps-enabledapps-available文件夹中的README文件外没有任何文件。我是否需要为uwsgi创建自己的配置文件?我的印象是,负责所有这些事情。

我相信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) 
+1

显示uwsgi配置和nginx的代理。 – iurisilvio

的权限问题因为uwsgi会将/tmp/uwsgi.sock的所有权和权限重置为755,并且每次uwsgi启动时都会运行uwsgi。

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

  1. 将uwsgi作为www-data用户运行,以便该用户拥有由其创建的套接字文件。

    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的数据可以写入。

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

我更喜欢第一种方法,因为它不会留下uwsgi作为root运行。

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

第一个命令叶uwsgi运行作为WWW的数据用户。第二个和第三个命令使uwsgi作为运行该命令的实际用户运行。

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

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

+4

我面临同样的问题,但我正在使用'gunicorn'而不是'uwsgi'.How解决这个问题..? – Shiva

+3

@Shiva这个问题和答案不是关于gunicorn。如果你有关于gunicorn的问题,你应该发布一个单独的问题。 –

+0

我在centOS上遇到了这个问题,而你的命令对我来说不起作用。我用:chown -R www-data:www-data/tmp' – Roman

你要设置这些权限(chmod/chown )在uWSGI配置。

这是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

+0

我试着改变套接字上的权限(手动)到666,但它仍然给我同样的错误。 –

+0

@iurisilvio你能写出语法吗? –

+1

@MitulShah接受的答案已经有正确的语法。 – iurisilvio

你应该张贴您的应用程序都nginx的和uwsgi配置文件(在/ etc/nginx的/启用的站点 - /和/ etc/uwsgi /对的 - 或任何你把它们)。

通常检查是否有类似于下面的一个在你的nginx的应用程序配置的线:

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

,并在您uwsgi配置文件相同的套接字名称:

socket=/tmp/uwsgi.sock 
+0

我没有uwsgi配置文件,请参阅编辑问题。即使我能够运行uwsgi命令并且它能正常工作,我是否还需要一个? –

+0

@AlexChumbley如果您不需要创建配置文件,则无需创建配置文件。在我看来,配置文件使安装程序有点整齐,因为大多数命令行选项移动到配置文件,这意味着您必须在命令中使用较少的选项才能使用。 –

我知道这是为时已晚,但它可能有助于其他。我会建议遵循Running flask with virtualenv, uwsgi, and nginx非常简单和甜蜜的文档。如果你的virtualenv运行项目

必须激活您的环境。

这里是yolo.py

from config import application 

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

而且在/ tmp /目录下创建文件uwsgi.sock和留空。 作为@susanpal回答说:“发生的权限问题,因为uwsgi重置所有权和755 /tmp/uwsgi.sock的权限和每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 &