django - nginx 权限在 Ubuntu 上被拒绝

标签 django ubuntu nginx permissions uwsgi

我正在尝试按照 this guide 使用 uWSGI 和 nginx 设置我的 Django 应用程序.我能够使用 Django 的开发服务器运行我的应用程序,并直接从 uWSGI 获得服务。

我在大学管理的 Ubuntu 16.04 虚拟机上运行所有内容,我的用户具有 sudo 访问权限。


我的问题:
当到达 this bit教程,并尝试获取图像,我从 nginx 收到 403 错误。
next section结果为 502。

/var/log/nginx/error.log 显示

connect() to unix:///me/myproject/media/image.jpg failed (13: Permission denied) while connecting to upstream

connect() to unix:///me/myproject/project.sock failed (13: Permission denied) while connecting to upstream

分别为 403 和 502。

我已经阅读了多个问题和指南(one hereanother hereyet another one,这不是全部),更改了我的权限,甚至将我的 .sock 移到了另一个文件夹(推荐的 SO 答案之一)。

我还能尝试什么?


更新:

我在评论中提到了它,但我有点更进一步了。问题的一部分是,显然,我 VM 上的 /home 目录是 NFS,这会混淆很多权限。

我做了什么:

  • 我已经在 /var/www/myproject/ 中设置了我的项目
  • 运行 chown -R me:www-data myproject
  • 运行 chmod -R 764 myproject

我的新结果:

  • 没有运行 nginx:
    • uwsgi --http :8000 --module myproject.wsgi
      完美运行
  • 在 nginx 运行的情况下:
    • uwsgi --socket myproject.sock --module myproject.wsgi --chmod-socket=664
      给我一个 502
    • uwsgi --ini myproject.ini
      给我一个 502

所以现在不是一般的权限问题,肯定是nginx的问题...


更新 #2:
目前,当 other 对套接字具有 read-write 权限,并且对项目的其余部分具有 read-execute 权限时,一切正常.
所以 nginx 没有像它应该的那样被识别......我已经仔细检查过,nginx 正在作为 www-data 用户运行,这是我整个项目的组所有者,并且有read-execute 权限,就像 other 现在拥有的一样。


这是我的(更新的)nginx.conf

# myproject_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server unix:///var/www/myproject/myproject.sock;
    # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name my.ip.goes.here; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /var/www/myproject/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /var/www/myproject/static; # your Django project's static files - amend as required

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /var/www/myproject/uwsgi_params; # the uwsgi_params file you installed
    }
}

这是我的(更新的)uwsgi.ini

# myproject_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir          = /var/www/myproject
# Django's wsgi file
module         = myproject.wsgi
# the virtualenv (full path)
home           = /var/www/myenv

# process-related settings
master         = true
# maximum number of worker processes
processes      = 10
# the socket (full path)
socket         = /var/www/myproject/myproject.sock
# ... with appropriate permissions - may be needed
chmod-socket   = 666
uid            = me
gid            = www-data
# clear environment on exit
vacuum         = true

最佳答案

根据我的经验,Web 服务器的大部分权限问题都是由访问 root 拥有的文件引起的,但 Apache (nginx) 在 www-data 用户下运行。

尝试运行 sudo chown www-data -R/path/to/your/data/folder

关于django - nginx 权限在 Ubuntu 上被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625238/

相关文章:

python - 安装到现有 Django 应用程序后如何将页面模板添加到 Wagtail

linux - 从 git repo pull 到生产服务器的良好实践

ubuntu - Postfix 已安装,但我该如何测试它?

Nginx 重定向可以在浏览器上工作,但是当使用curl 时,它只输出 301 moving permanent

Nginx 和 runit ......什么是最佳实践

python - Django-1.3 beta 的问题

python - Django 错误,来自 ('127.0.0.1' 的管道损坏,33609)

python - 是否可以从 Python 中的模块获取导入内容的列表?

PHP Session 随机死亡

javascript - Node.js AWS 部署错误 : Cannot find module