大网数据广告活动
服务器运维问答

No socket created by Gunicorn causes nginx to throw 502 error

· 52CE 我爱测

Following this tutorial , I was able to set up Django, Gunicorn & nginx inside a virtualenv on an AWS EC2 instance (running Ubuntu 16.04), and then proceed to create an Upstart file to "daemonize" the entire thing. After activati…

问题原文

原帖链接 · Tom Granot · CC BY-SA 4.0

Following this tutorial, I was able to set up Django, Gunicorn & nginx inside a virtualenv on an AWS EC2 instance (running Ubuntu 16.04), and then proceed to create an Upstart file to "daemonize" the entire thing.

After activating the virtualenv, I checked that:

  1. Django works - I was able to access my Django project via port 8000 by running the following:

./manage.py runserver 0.0.0.0:8000

When visiting mydomain.com:8000 I was welcomed by the default Django page.

  1. Gunicorn works & is able to serve the Django app - I was able to access my Django app by running this from my project's folder (replace projectname with my actual project name):

gunicorn --bind 0.0.0.0:8000 projectname.wsgi:application

When visiting mydomain.com:8000 this time I was greeted again by the welcome page, and when appending /admin to the end of the URL I was able to see the login screen minus the CSS (because Gunicorn isn't aware of the static CSS files, which is OK for now according to the tutorial).

From here I proceeded to create the following systemd file (when masterfolder is used to show the folder in which all the action takes place, myuser is my system user (ubuntu, as this is an Ubuntu instance) :

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

[Service]
User=myuser
Group=www-data
WorkingDirectory=/home/myuser/masterfolder
ExecStart=/home/myuser/masterfolder/myvirtualenv/bin/gunicorn --workers 3 --bind unix:/home/myuser/masterfolder/projectname.sock$

[Install]
WantedBy=multi-user.target`

I encountered two problems:

  1. No socket is being created - when checking the nginx log files outside as well as inside the virtualenv (not entirely sure why they are the same, btw) I saw the same error:

2017/01/17 15:12:43 [crit] 12403#12403: *3 connect() to unix:/home/myuser/masterfolder/projectname.sock failed (2: No such file or directory) while connecting to upstream

Now, note that masterfolder is consistent with the folder hierarchy django-admin creates when I run a startproject:

masterfolder/ ├── manage.py └── projectname ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py

When looking inside masterfolder there is indeed no .sock file at all, never mind one that is named projectname.sock.

  1. Therefore, despite the fact that nginx works, it throws a 502 -

This is how my etc/nginx/sites-availabe/projectname file looks like:

server {
    listen 80;
    server_name www.mydomain.com mydomain.com MYIPADDRESS;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/masterfolder/projectname;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/myuser/mastefolder/projectname.sock;
    }
}

It's symlinked correctly, of course, and after every change I make to try and fix it I run:

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx

I feel as if I'm missing something basic here in the understanding of how all of this infrastructure works together. Feel free to point out errors in my understanding of the process, of course.

已采纳解答

原帖链接 · akhilsp · CC BY-SA 3.0

Have you checked permissions of the masterfolder directory?

The permissions should be myuser:www-data.

Also there is a spelling mistake in your nginx config file. Hope that's a typo:)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。