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

前端之家收集整理的这篇文章主要介绍了django-Nginx:在CentOS 7上对Gunicorn套接字的权限被拒绝 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_1@我正在Django项目部署中工作.我正在由EC2(AWS)提供的CentOS 7服务器中工作.我已尝试通过多种方式修复此错误,但我无法理解我所缺少的内容.

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

@H_502_1@

  1. [Unit]
  2. Description=gunicorn daemon
  3. After=network.target
  4. [Service]
  5. User=centos
  6. Group=Nginx
  7. WorkingDirectory=/home/centos/myproject_app
  8. ExecStart=/home/centos/myproject_app/django_env/bin/gunicorn --workers 3 --bind unix:/home/centos/myproject_app/django.sock app.wsgi:application
  9. [Install]
  10. WantedBy=multi-user.target
@H_502_1@当我运行sudo systemctl重新启动myproject.service并启用sudo systemctl启用myproject.service时,django.sock文件已正确生成到/ home / centos / myproject_app /中.

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

@H_502_1@

  1. server {
  2. listen 80;
  3. server_name my_ip;
  4. charset utf-8;
  5. client_max_body_size 10m;
  6. client_body_buffer_size 128k;
  7. # serve static files
  8. location /static/ {
  9. alias /home/centos/myproject_app/app/static/;
  10. }
  11. location / {
  12. include proxy_params;
  13. proxy_pass http://unix:/home/centos/myproject_app/django.sock;
  14. }
  15. }
@H_502_1@之后,我使用以下命令重新启动Nginx

@H_502_1@

  1. sudo systemctl restart Nginx
@H_502_1@如果我运行命令sudo Nginx -t,则响应为:

@H_502_1@

  1. Nginx: configuration file /etc/Nginx/Nginx.conf test is successful
@H_502_1@当我在网络浏览器中访问my_ip时,收到502错误的网关响应.

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

@H_502_1@

  1. 1 connect() to unix:/home/centos/myproject_app/django.sock Failed (13: Permission denied) while connecting to upstream
@H_502_1@我确实尝试了许多更改袜子文件权限的解决方案.但是我不明白如何解决它.我该如何解决此权限错误?…非常感谢
最佳答案
如果myproject_app文件夹下的所有权限都是正确的,并且centos用户Nginx组可以访问这些文件,那么我认为这看起来像是安全性增强Linux(SELinux)问题.

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

@H_502_1@

  1. sudo semanage permissive -a httpd_t
@H_502_1@它与SELinux的安全策略有关,您必须将httpd_t添加到允许域列表中.

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

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

猜你在找的Nginx相关文章