django-Nginx:在CentOS 7上对Gunicorn套接字的权限被拒绝

我正在Django项目部署中工作.我正在由EC2(AWS)提供的CentOS 7服务器中工作.我已尝试通过多种方式修复此错误,但我无法理解我所缺少的内容.

我正在使用ningx和gunicorn来部署我的项目.我创建了具有以下内容的/etc/systemd/system/myproject.service文件

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

[Service]
User=centos
Group=Nginx
WorkingDirectory=/home/centos/myproject_app
ExecStart=/home/centos/myproject_app/django_env/bin/gunicorn --workers 3 --bind unix:/home/centos/myproject_app/django.sock app.wsgi:application
[Install]
WantedBy=multi-user.target

当我运行sudo systemctl重新启动myproject.service并启用sudo systemctl启用myproject.service时,django.sock文件已正确生成到/ home / centos / myproject_app /中.

我在/ etc / Nginx / sites-available /文件夹中创建了Nginx conf文件,其内容如下:

server {
    listen       80;
    server_name  my_ip;
    charset      utf-8;

    client_max_body_size       10m;
    client_body_buffer_size    128k;

    # serve static files
    location /static/ {
        alias /home/centos/myproject_app/app/static/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/centos/myproject_app/django.sock;
    }
}

之后,我使用以下命令重新启动Nginx

sudo systemctl restart Nginx

如果我运行命令sudo Nginx -t,则响应为:

Nginx: configuration file /etc/Nginx/Nginx.conf test is successful

当我在网络浏览器中访问my_ip时,收到502错误的网关响应.

如果检查Nginx错误日志,则会看到以下消息:

1 connect() to unix:/home/centos/myproject_app/django.sock Failed (13: Permission denied) while connecting to upstream

我确实尝试了许多更改袜子文件权限的解决方案.但是我不明白如何解决它.我该如何解决此权限错误?…非常感谢

最佳答案
如果myproject_app文件夹下的所有权限都是正确的,并且centos用户Nginx组可以访问这些文件,那么我认为这看起来像是安全性增强Linux(SELinux)问题.

我遇到了类似的问题,但是使用RHEL7.我设法通过执行以下命令解决了该问题:

sudo semanage permissive -a httpd_t

它与SELinux的安全策略有关,您必须将httpd_t添加到允许域列表中.

Nginx博客的这篇帖子可能会有所帮助:NGINX: SELinux Changes when Upgrading to RHEL 6.6 / CentOS 6.6

出于类似的问题,我不久前在How to Deploy a Django Application on RHEL 7上写了一个教程.对于CentOS 7,它应该非常相似.

相关文章

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一...
本地项目配置 1 复制 luffy/settings/dev.py为prop.py 修改l...
nginx不仅可以隐藏版本信息,还支持自定义web服务器信息 先看...
一 、此次漏洞分析 1 nginx HTTP/2漏洞 [nginx-announce] ng...
###进入nginx 目录cd /usr/local/nginx###递归显示 2 级目录...
在cmd命令窗口输入下面命令进行查看 tasklist /fi "ima...