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.service
和sudo systemctl enable myproject.service
时,django.sock
文件正确生成为/home/centos/myproject_app/
.
我在文件夹 /etc/nginx/sites-available/ 中创建了我的 nginx
配置文件,内容如下:
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
我确实尝试了很多更改 sock 文件权限的解决方案。但我不明白如何解决它。我该如何解决这个权限错误?...非常感谢
很可能是两个中的一个
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 用户的目录访问
如果 myproject_app
文件夹下的所有权限都正确,并且 centos
用户或 nginx
组可以访问这些文件,我会说它看起来像一个安全增强 Linux (SELinux) 问题。
我遇到了类似的问题,但使用的是 RHEL 7。我通过执行以下命令设法解决了它:
sudo semanage permissive -a httpd_t
这与 SE 的安全策略有关Linux,您必须将 httpd_t
添加到允许域列表中。
来自 NGINX 博客的 post 可能会有帮助:NGINX: SELinux Changes when Upgrading to RHEL 6.6 / CentOS 6.6
受类似问题的启发,我前一段时间在 How to Deploy a Django Application on RHEL 7 上写了一篇教程。 CentOS 7应该很相似。
这里有完全相同的问题。
删除 Group=www-data
解决了我的问题
我正在从事 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.service
和sudo systemctl enable myproject.service
时,django.sock
文件正确生成为/home/centos/myproject_app/
.
我在文件夹 /etc/nginx/sites-available/ 中创建了我的 nginx
配置文件,内容如下:
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
我确实尝试了很多更改 sock 文件权限的解决方案。但我不明白如何解决它。我该如何解决这个权限错误?...非常感谢
很可能是两个中的一个
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 用户的目录访问
如果 myproject_app
文件夹下的所有权限都正确,并且 centos
用户或 nginx
组可以访问这些文件,我会说它看起来像一个安全增强 Linux (SELinux) 问题。
我遇到了类似的问题,但使用的是 RHEL 7。我通过执行以下命令设法解决了它:
sudo semanage permissive -a httpd_t
这与 SE 的安全策略有关Linux,您必须将 httpd_t
添加到允许域列表中。
来自 NGINX 博客的 post 可能会有帮助:NGINX: SELinux Changes when Upgrading to RHEL 6.6 / CentOS 6.6
受类似问题的启发,我前一段时间在 How to Deploy a Django Application on RHEL 7 上写了一篇教程。 CentOS 7应该很相似。
这里有完全相同的问题。
删除 Group=www-data
解决了我的问题