【问题标题】:502 bad gateway because of permissions denied502 bad gateway 因为权限被拒绝
【发布时间】:2020-05-25 03:20:52
【问题描述】:

我正在尝试使用 nginx 和 gunicorn 在 digitalocean 上部署 django 项目。 我的项目具有以下结构

projects
|_isli
  |_isli
    |_forecast #project directory
      |_manage.py
      |_forecast.sock
      |_forecast
        |_wsgi.py
        |_settings.py
        |_urls.py

我的项目在根目录中创建而没有创建额外的 sudo 用户。我知道这不是正确的解决方案,但我决定这样做。 在我的settings.py 文件内允许的主机中,我指定了 IP 地址

ALLOWED_HOSTS = ['165.22.23.233']

在 digitalocean 官方文档中有关于使用 nginx 和 gunicorn 部署 django 的教程Deploying django using Nginx and Gunicorn

在本文中使用了 gunicorn 设置为 soce 的方法,这是我的设置 /etc/systemd/system/gunicorn.service

[Unit]                                                        
Description=gunicorn daemon                                   
After=network.target


[Service]                                                     
User=root                                                   
Group=root                                                  
WorkingDirectory=/root/projects/isli/isli/forecast         
ExecStart=/root/projects/isli/env/bin/gunicorn --log-level debug --error-logfile /var/log/gunicorn/error.log --access-logfile /var/log/gunicorn/access.log --workers 3 --bind unix:/root/projects/isli/isli/forecast/forecast.sock forecast.wsgi:application

[Install]                                                     
WantedBy=multi-user.target

创建 gunicorn.service 文件后,我运行 systemctl start gunicorn 而不是 systemctl enable gunicorn 在我的项目目录中创建了 forecast.sock 文件 比我在/etc/nginx/sites-available/forecast 中设置 nginx 与以下

server {
    listen 165.22.23.233:80;

    location = /favicon.ico {access_log off; log_not_found off;}                                                                                                                       
    location / { 
          include proxy_params;                                       
          proxy_pass http://unix:/root/projects/isli/isli/forecast/forecast.sock;
    }
 }

systemctl restart nginx

当我尝试从浏览器访问 http://165.22.23.233:80 时,它会提示我 502 错误网关。在 /var/log/nginx/error.log 文件中之后,我看到以下内容

2020/02/09 16:29:01 [crit] 13533#13533: *11 connect() to unix:/root/projects/isli/isli/forecast/forecast.sock failed (
13: Permission denied) while connecting to upstream, client: 178.176.218.110, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/root/projects/isli/isli/forecast/forecast.sock:/", host: "165.22.23.233"

据我了解,通过此错误,我的问题是 nginx 无法访问 /root/projects/isli/isli/forecast/forecast.sock 文件。之后,我尝试通过

检查对上述路径的每个实体的权限
namei -nom /root/projects/isli/isli/forecast/forecast.sock

这是输出

f: /root/projects/isli/isli/forecast/forecast.sock
drwxr-xr-x root root /
drwx------ root root root
drwxr-xr-x root root projects
drwxr-xr-x root root isli
drwxr-xr-x root root isli
drwxr-xr-x root root forecast
srwxrwxrwx root root forecast.sock

在上面的输出中,root 用户对我的套接字路径的每个实体都有权限,但为什么错误说我权限被拒绝

【问题讨论】:

  • 看不懂你的项目结构,forecast.sock是不是里面有settings.py的目录???
  • @dirkgroten 抱歉,我做错了。我刚刚编辑。预测目录中的Settings.py
  • 如果您不进行任何更改,通常 nginx 会作为 www-data 运行。您是如何将其更改为以 root 身份运行的?
  • @dirkgroten 你的意思是我在 gunicorn.service 文件中指定了User=root 吗?
  • 没有。 nginx。您显然没有更改 nginx 的默认值,因此它作为 www-data 运行。如果您还使用用户 www-data 运行 gunicorn 并将项目文件夹的所有权限设置为 www-data 而不是 root,则会更容易。

标签: django nginx gunicorn


【解决方案1】:

你需要将listen与server_name分开:

server {
        listen 80;
        server_name 165.22.23.233; # (Example 192.168.0.1, example.com)
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /root/projects/isli/isli/forecast/;
        }
        location / {
                include proxy_params;
                proxy_pass http://unix:/root/projects/isli/isli/forecast/forecast.sock;
        }
}

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2014-02-07
    • 2016-08-06
    • 2018-01-06
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多