nginx + gunicorn + django + aws糟糕的请求

时间:2015-01-31 23:29:24

标签: django amazon-web-services nginx gunicorn

我正在关注https://ashokfernandez.wordpress.com/2014/03/11/deploying-a-django-app-to-amazon-aws-with-nginx-gunicorn-git/的教程 部署django应用程序。这是我目前的情况:

fab spawn实例创建了安装了nginx和gunicorn的aws实例但是当我尝试访问该机器上的网站时,我收到了400 Bad Request。我检查了nginx-error日志,但是这是空的,nginx-access日志显示它已收到请求。主管日志具有以下内容:

[2015-01-31 21:26:20 +0000] [15823] [INFO] Starting gunicorn 19.2.0
[2015-01-31 21:26:20 +0000] [15823] [INFO] Listening at: http://127.0.0.1:8002/ (15823)
[2015-01-31 21:26:20 +0000] [15823] [INFO] Using worker: sync
[2015-01-31 21:26:20 +0000] [15832] [INFO] Booting worker with pid: 15832
[2015-01-31 21:26:20 +0000] [15833] [INFO] Booting worker with pid: 15833
[2015-01-31 21:26:20 +0000] [15834] [INFO] Booting worker with pid: 15834
[2015-01-31 21:26:20 +0000] [15835] [INFO] Booting worker with pid: 15835
[2015-01-31 21:26:20 +0000] [15836] [INFO] Booting worker with pid: 15836
[2015-01-31 21:26:31 +0000] [15837] [INFO] Starting gunicorn 19.2.0
[2015-01-31 21:26:31 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:31 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:32 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:32 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:33 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:33 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:34 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:34 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:35 +0000] [15837] [ERROR] Connection in use: (‘127.0.0.1′, 8002)
[2015-01-31 21:26:35 +0000] [15837] [ERROR] Retrying in 1 second.
[2015-01-31 21:26:36 +0000] [15837] [ERROR] Can’t connect to (‘127.0.0.1′, 8002)
[2015-01-31 21:26:37 +0000] [15846] [INFO] Starting gunicorn 19.2.0
[2015-01-31 21:26:37 +0000] [15846] [INFO] Listening at: http://127.0.0.1:8002 (15846)
[2015-01-31 21:26:37 +0000] [15846] [INFO] Using worker: sync
[2015-01-31 21:26:37 +0000] [15855] [INFO] Booting worker with pid: 15855
[2015-01-31 21:26:37 +0000] [15856] [INFO] Booting worker with pid: 15856
[2015-01-31 21:26:37 +0000] [15857] [INFO] Booting worker with pid: 15857
[2015-01-31 21:26:38 +0000] [15858] [INFO] Booting worker with pid: 15858
[2015-01-31 21:26:38 +0000] [15859] [INFO] Booting worker with pid: 15859

我将ALLOWED_HOSTS从[]改为[" *"]然后' *' 当我将其更改为字符串时,我得到了#34;在此服务器上找不到请求的URL。"在其他情况下,我得到了400个不良请求。

这是我第一次在nginx上部署django应用程序而我无法弄清楚问题可能是什么。你能帮我调试一下这个错误吗? 在此先感谢!!

PS:如果我需要发布任何配置文件,请告诉我。到目前为止,我刚刚遵循了教程,并没有更改任何配置。

更新: common.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = 'blah'
ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mosaic.urls'

WSGI_APPLICATION = 'mosaic.wsgi.application'

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

prod.py

from mosaic.settings.common import *

DEBUG = True #Change this after the project starts working

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mosaic',
        'USER' : 'adminuser',
        'PASSWORD' : '****',
        'HOST' : 'abcd.cjcgmgnogsvc.us-west-1.rds.amazonaws.com',
        'PORT' : '5432',
    }
}

# Static files via Amazon S3 (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

INSTALLED_APPS += ('storages',)
AWS_STORAGE_BUCKET_NAME = "xyz"
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = S3_URL

另外在我的项目配置文件中     fabconf [' DOMAINS'] ="" 我认为这也可能是相关的

1 个答案:

答案 0 :(得分:0)

允许的主机必须匹配您将使用的主机名。因此,将ALLOWED_HOSTS = []放在特定于您的环境(开发或生产)的设置文件中。在开发方面将其设置为ALLOWED_HOSTS = ['localhost', '127.0.0.1'],在生产设置上将其设置为您的域名&服务器IP。

来自文档:

  

此列表中的值可以是完全限定名称(例如'www.example.com'),在这种情况下,它们将与完全匹配请求的Host标题(不区分大小写, 不包括端口)。以句点开头的值可用作子域通配符:'.example.com'将匹配example.comwww.example.com以及example.com的任何其他子域。值'*'将匹配任何内容;在这种情况下,您有责任提供自己的Host标头验证(可能在中间件中;如果是,则必须首先在MIDDLEWARE_CLASSES中列出此中间件)。

您获得的状态400响应是由于当您的主机标头与该列表中的任何值不匹配时引发SuspiciousOperation exception