【问题标题】:nginx+gunicorn+django+aws bad requestnginx+gunicorn+django+aws 错误请求
【发布时间】:2015-03-31 05:13:41
【问题描述】:

我正在关注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 从 [] 更改为 ["*"] 然后 '*' 当我将其更改为字符串时,我得到“在此服务器上找不到请求的 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'] = "" 我认为这也可能是相关的

【问题讨论】:

    标签: django amazon-web-services nginx gunicorn


    【解决方案1】:

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

    来自文档:

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

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

    【讨论】:

    • 感谢您的回复。我已经用 django 设置文件更新了这个问题。我仍然收到 400 Bad request。
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多