python - Nginx Django 和 Gunicorn。 Gunicorn socks 文件丢失了吗?

标签 python django python-2.7 nginx gunicorn

我有一个基于这个的 ansible 配置虚拟机 https://github.com/jcalazan/ansible-django-stack但出于某种原因,尝试启动 Gunicorn 会出现以下错误:

Can't connect to /path/to/my/gunicorn.sock

在 nginx 日志文件中:

connect() to unix:/path/to/my/gunicorn.sock failed (2: No such file or directory) while connecting to upstream

实际上指定目录中缺少套接字文件。我检查了目录的权限,他们没问题。

这是我的 gunicorn_start 脚本:

NAME="{{ application_name }}"
DJANGODIR={{ application_path }}
SOCKFILE={{ virtualenv_path }}/run/gunicorn.sock
USER={{ gunicorn_user }}
GROUP={{ gunicorn_group }}
NUM_WORKERS={{ gunicorn_num_workers }}

# Set this to 0 for unlimited requests. During development, you might want to
# set this to 1 to automatically restart the process on each request (i.e. your
# code will be reloaded on every request).
MAX_REQUESTS={{ gunicorn_max_requests }}

echo "Starting $NAME as `whoami`"

# Activate the virtual environment.
cd $DJANGODIR
. ../../bin/activate

# Set additional environment variables.
. ../../bin/postactivate

# Create the run directory if it doesn't exist.
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Programs meant to be run under supervisor should not daemonize themselves
# (do not use --daemon).
exec gunicorn \
    --name $NAME \
    --workers $NUM_WORKERS \
    --max-requests $MAX_REQUESTS \
    --user $USER --group $GROUP \
    --log-level debug \
    --bind unix:$SOCKFILE \
    {{ application_name }}.wsgi

任何人都可以建议还有什么可能导致丢失套接字文件吗?

谢谢

最佳答案

好吧,因为我没有足够的代表发表评论,所以我会在这里提到,丢失的 socket 并没有说明很多具体情况,但我可以告诉你一些关于我是如何从你的角度开始的,有事情要做。

总而言之,gunicorn 在由 upstart 运行时遇到了一个问题,要么永远无法启动并运行,要么就关闭了。以下是一些可以帮助您获取更多信息以追踪您的问题的步骤:

  • 在我的案例中,发生这种情况时,gunicorn 从来没有抽出时间记录任何错误,所以我不得不寻找其他地方。试试 ps auxf | grep gunicorn 查看是否有工作人员在运行。我没有。
  • 在系统日志中查找 Upstart 的投诉,grep init:/var/log/syslog,显示我的 gunicorn 服务已停止,因为它重生太快,但我对此表示怀疑'这将是你的问题,因为你的 conf 中没有重生。无论如何,您可能会在那里找到一些东西。
  • 在看到 gunicorn 无法运行或记录错误后,我决定尝试从命令行运行它。转到您的 manage.py 所在的目录,并针对您的 gunicorn 实例运行扩展版本的 upstart 命令。类似的东西(用适当的文字代替我使用的垃圾替换所有变量。):

    /path/to/your/virtualenv/bin/gunicorn --name myapp --workers 4 --max-requests 10 --user appuser --group webusers --log-level debug --error-日志文件/somewhere/I/can/find/error.log --bind unix:/tmp/myapp.socket myapp.wsgi

  • 如果幸运的话,您可能会在手动运行命令后获得 python 回溯或在 gunicorn 错误日志中找到某些内容。一些可能出错的事情:

    • django 错误(可能是加载设置模块时出现问题?)。确保您的 wsgi.py 引用了服务器上适当的设置模块。
    • upstart 脚本中的空格问题。我有一个隐藏在空间中的选项卡,可以把事情搞砸。
    • 用户/权限问题。最后,我能够在命令行上以 root 用户身份运行 gunicorn,但不能通过 upstart 配置以非 root 用户身份运行。

希望对您有所帮助。花了好几天时间来追踪这些东西。

关于python - Nginx Django 和 Gunicorn。 Gunicorn socks 文件丢失了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689445/

相关文章:

python - 如何在 django 中为表单创建下拉框?

python - Python 中的 OrderedDict 与字典

python - 为什么 Django 应该尝试在字段查找中自动调用参数?

python - 修改中间件中的 Django 设置变量

python - 整数的 astype float32 与 float64 错误

python - 在 cmd 模块 python 中调用菜单

python - Python 中的 NLP 短语搜索

python - 在 QTextEdit 小部件中解析和显示 HTML

python - 使用 Python 的 os.path,我如何上一个目录?

python - 正则表达式捕获在 python 中使用私有(private)成员的代码,除非它是函数 def