【问题标题】:Can't reach Django default app via Gunicorn on AWS EC2 instance无法通过 AWS EC2 实例上的 Gunicorn 访问 Django 默认应用程序
【发布时间】:2014-02-27 10:10:30
【问题描述】:

我已经在这个问题上苦苦挣扎了两天,但没有成功。我创建了一个名为“testdj”的默认 Django (1.6.1) 应用程序的实例,将其安装在运行 Ubuntu Server 13.10 的 Amazon AWS EC2 t1.micro 实例上,并且我正在尝试使用默认的 Django “它有效!”通过 Gunicorn 页面(第 18 节)。当我从命令行启动 gunicorn 时:

gunicorn testdj.wsgi:application --bind [ec2-public-dns]:8001

我输入这个网址就可以看到页面了:

http://[ec2-public-dns]:8001

但是,如果我使用我在阅读 Karzynski 的blogpost“使用 Nginx、Gunicorn、virtualenv、supervisor 和 PostgreSQL 设置 Django”后创建的“start-gunicorn”bash 脚本,我总是会出错。当我输入这个网址时...

http://[ec2-public-dns]

...我收到此错误:

Error 502 - Bad Request
The server could not resolve your request for uri: http://[ec2-public-dns]

这里是 start-gunicorn 脚本:

#!/bin/bash

NAME="testdj"
DJANGODIR=/usr/share/nginx/html/testdj
SOCKFILE=/usr/share/nginx/html/testdj/run/gunicorn.sock
USER=testdj
GROUP=testdj
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=testdj.settings
DJANGO_WSGI_MODULE=testdj.wsgi

WORKON_HOME=/home/testdj/venv
source `which virtualenvwrapper.sh`
workon $NAME
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGO_DIR:$PYTHONPATH

RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

exec gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --access-logfile /tmp/gunicorn-access.log \
  --error-logfile /tmp/gunicorn-error.log \
  --log-level=debug \
  --bind=unix:$SOCKFILE

如您所见,我在我的服务器上创建了一个名为“testdj”的特殊帐户来运行应用程序。我在虚拟环境中运行我的 Django 应用程序。我根本没有更改 Django wsgi.py 文件。因为我最终想使用 nginx 作为我的反向代理,所以我安装了 nginx 并将 Django 应用程序放在 nginx 的默认根目录 /usr/share/nginx/html 中。用户/组 www-data 拥有 /usr/share/nginx 和下面的所有东西,除了用户/组“testdj”拥有 /usr/share/nginx/html/testdj 和它下面的所有东西。 /usr/share/nginx/html/testdj 及其所有子目录的 perms 为 775,我已将 www-data 添加到 testdj 组。

我确实安装了 nginx,但我没有运行 nginx 服务。我确实尝试使用以下配置文件启动它并启用 nginx 虚拟服务器,但仍然出现错误。

upstream testdj_app_server {
    server unix:/usr/share/nginx/html/testdj/run/gunicorn.sock fail_timeout=0;
}
server {
    listen 80;
    server_name ec2-[my-public-dns-ip].us-west-2.compute.amazonaws.com;
    client_max_body_size 4G;
    access_log /var/log/nginx/testdj-access.log;
    error_log /var/log/nginx/testdj-error.log;

    location /static/ {
        alias /usr/share/nginx/html/testdj/static/;
    }
    location /media/ {
        alias /usr/share/nginx/html/testdj/media/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;   
        if (!-f $request_filename) {
            # This must match "upstream" directive above
            proxy_pass http://testdj_app_server;
            break;
        }
    }
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /usr/share/nginx/html/testdj/static/;
    }
}

问题似乎与 gunicorn 相关,因为如果我将 start-gunicorn 脚本中的“--bind=unix:$SOCKFILE”替换为“--bind --[ec2-public-dns]:8000”,我可以看到Django默认页面。但是,我不想在端口 8000 上绑定我的公共 DNS 名称,我想在端口 80 上运行并使用 nginx 作为我的前端反向代理。

我最初有 AWS 入站安全组规则,将站点的访问权限限制为我笔记本电脑的端口 80、8000 和 8001 上的 HTTP,但即使我删除这些规则并让站点完全打开,我仍然会收到 502 错误消息。

我的 gunicorn 访问日志没有显示任何活动,我在 gunicorn 错误日志中看到的唯一内容是 gunicorn 正在启动。当我访问默认的Django页面时,错误日志中没有错误:

2014-02-03 18:41:01 [19023] [INFO] Starting gunicorn 18.0
2014-02-03 18:41:01 [19023] [DEBUG] Arbiter booted
2014-02-03 18:41:01 [19023] [INFO] Listening at: unix:/usr/share/nginx/html/testdj/run/gunicorn.sock (19023)
2014-02-03 18:41:01 [19023] [INFO] Using worker: sync
2014-02-03 18:41:01 [19068] [INFO] Booting worker with pid: 19068
2014-02-03 18:41:01 [19069] [INFO] Booting worker with pid: 19069
2014-02-03 18:41:01 [19070] [INFO] Booting worker with pid: 19070

有人知道这里发生了什么吗?看起来我什至没有进入gunicorn。我为这篇冗长的帖子道歉,但这个问题似乎有很多“移动部分”。我会非常感谢任何帮助,因为我尝试了许多不同的东西,但都无济于事。我还在这里查看了其他问题,其他人也有类似的问题,但我没有看到与此问题相关的任何内容。谢谢!

【问题讨论】:

  • unix 权限是否允许 nginx 与 gunicorn SOCKFILE 对话?
  • 我相信是的。以下是目录、权限设置和用户/组所有者: /usr/share/nginx/html/testdj 775 testdj:testdj /usr/share/nginx/html/testdj/run 775 testdj:testdj /usr/share /nginx/html/testdj/run/gunicorn.sock s777 testdj:testdj
  • www-data,默认的nginx用户,是testdj组的成员。

标签: django nginx gunicorn


【解决方案1】:

我在我的 Linode 服务器上逐行重复了我的配置过程,完全没有问题。我不得不假设这个问题与 AWS EC2 实例的配置方式有关,可能与安全性有关。

【讨论】:

    【解决方案2】:

    我今天遇到了同样的问题。 Daniel Roseman在cmets里给我解释了here

    8000端口默认不对外开放;你要么需要 摆弄您的负载平衡器/防火墙设置以打开它,或运行 端口 80 上的 gunicorn (这意味着杀死 nginx 并启动 gunicorn 作为超级用户)。获取 nginx 设置要容易得多 正确的; gunicorn 上有一个完美可用的配置 deploy docs page.

    似乎可以直接运行 Gunicorn,但默认情况下 EC2 并未设置为这样做。

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2022-01-22
      • 2018-02-12
      • 2020-11-08
      相关资源
      最近更新 更多