繁体   English   中英

Nginx:CentOS 7上的Gunicorn套接字被拒绝

[英]Nginx: Permission denied to Gunicorn socket on CentOS 7

我正在进行Django项目部署。 我在提供ma 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 restart myproject.servicesudo systemctl enable myproject.servicedjango.sock文件正确生成到/home/centos/myproject_app/

我已经在/ etc / nginx / sites-available /文件夹中创建了我的nginx conf flie,其中包含以下内容:

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

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

如果我检查nginx错误日志,我看到以下消息:

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

我真的尝试了很多改变sock文件权限的解决方案。 但我不明白如何解决它。 我该如何修复此权限错误?...非常感谢你

如果myproject_app文件夹下的所有权限都是正确的,并且centos用户或nginx组可以访问这些文件,我会说它看起来像是安全增强型Linux(SELinux)问题。

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

sudo semanage permissive -a httpd_t

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

来自NGINX博客的这篇文章可能会有所帮助: NGINX:升级到RHEL 6.6 / CentOS 6.6时SELinux会发生变化

受类似问题的影响,我刚刚写了一篇关于如何在RHEL 7上部署Django应用程序的教程。 它应该与CentOS 7非常相似。

最有可能是两个中的一个

1- nginx /home/centos/myproject_app/无法访问该目录

$ ls -la /home/centos/myproject_app/

如果它不可访问,尝试将路径更改为/etc/nginx如果没有,请尝试该命令

$ /home/centos/myproject_app/django_env/bin/gunicorn --workers 3 --bind unix:/home/centos/myproject_app/django.sock app.wsgi:application

如果仍然没有工作然后激活环境和python manage.py runserver 0.0.0.0:8000去浏览器并转到http:// ip:8000问题可能在这里,但它的gunicorn命令运作良好,然后nginx用户的目录访问问题

这里完全相同的问题。

删除Group=www-data为我解决了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM