代码之家  ›  专栏  ›  技术社区  ›  Ropali Munshi

Django:app在服务器上部署时显示400错误

  •  0
  • Ropali Munshi  · 技术社区  · 4 年前

    实际上,我这里有两个问题。我有一个vps(最低限度的测试),其中我有2-3个应用程序。我刚刚部署了另一个应用程序在它&得到了一个免费域名从freenom.com网站.我用 supervisor+nginx 主持会议应用程序没理由我会得到这个 Bad Request (400) 当我参观域。一般来说如果不将域名放入 ALLOWED_HOSTS 但我已经安排好了。 第二个问题是我有 DEBUG = True 不过,它只是显示最小错误响应,而不是完整的堆栈跟踪,因此我不知道我实际上得到了什么错误&无法调试它。

    1. 中没有错误日志 gunicorn-error.log 文件。
    2. 中没有错误日志 nginx-error.log 文件。
    3. 中没有错误日志 nginx-access.log 文件。

    我的 gunicorn_start 配置

    #!/bin/bash
    
    NAME="hrms_app"
    DIR=/home/kunsab/projects/hrms_venv/hrms
    USER=kunsab
    GROUP=kunsab
    WORKERS=3
    BIND=127.0.0.1:8081
    DJANGO_SETTINGS_MODULE=hrms.settings
    DJANGO_WSGI_MODULE=hrms.wsgi
    LOG_LEVEL=error
    
    cd $DIR
    source ../bin/activate
    
    export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
    export PYTHONPATH=$DIR:$PYTHONPATH
    
    exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
      --name $NAME \
      --workers $WORKERS \
      --user=$USER \
      --group=$GROUP \
      --bind=$BIND \
      --log-level=$LOG_LEVEL \
      --log-file=-
    

    主管配置:

    [program:hrms_app]
    command=/home/kunsab/projects/hrms_venv/bin/gunicorn_start
    user=kunsab
    autostart=true
    autorestart=true
    redirect_stderr=true
    stdout_logfile=/home/kunsab/projects/hrms_venv/logs/gunicorn-error.log
    

    设置.py

    """
    Django settings for hrms project.
    
    Generated by 'django-admin startproject' using Django 3.1.3.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/3.1/topics/settings/
    
    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/3.1/ref/settings/
    """
    
    from pathlib import Path
    
    # Build paths inside the project like this: BASE_DIR / 'subdir'.
    BASE_DIR = Path(__file__).resolve().parent.parent
    
    
    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
    
    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '9#h(zvlwo4gmci-jr-3t_5s1y_vu7_07rox6*(y=kcirm!w+cl'
    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True
    
    ALLOWED_HOSTS = ['gthrms.tk','galentichrms.tk','127.0.0.1']
    
    BASE_URL = 'http://galentichrms.tk/'
    
    INTERNAL_IPS = [
        '127.0.0.1',
    ]
    
    
    # Application definition
    DJANGO_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
    MY_APPS = [
        'employees',
        'clients',
        'projects',
        'payroll',
        'locations',
        'marketing',
    ]
    
    THIRD_PARTY_APPS = [
        'debug_toolbar',
        'mathfilters'
    ]
    
    INSTALLED_APPS = DJANGO_APPS + MY_APPS + THIRD_PARTY_APPS
    
    
    MIDDLEWARE = [
        'debug_toolbar.middleware.DebugToolbarMiddleware',
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        # 'django.middleware.clickjacking.XFrameOptionsMiddleware', # disable to view the pdf files in the model iframe
    ]
    
    ROOT_URLCONF = 'hrms.urls'
    
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [BASE_DIR.joinpath('templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                    'hrms.context.global_constants'
                ],
            },
        },
    ]
    
    WSGI_APPLICATION = 'hrms.wsgi.application'
    
    
    # Database
    # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'hrms',
            'HOST': '127.0.0.1',
            'USER': '****',
            'PASSWORD': '*****'
        }
    }
    
    
    # Password validation
    # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
    
    
    password_validators = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]
    
    AUTH_PASSWORD_VALIDATORS = [] if DEBUG else password_validators
    
    AUTH_USER_MODEL = 'accounts.User'
    
    AUTHENTICATION_BACKENDS = (
            'django.contrib.auth.backends.ModelBackend',
    )
    
    LOGIN_URL = '/accounts/login'
    
    # Internationalization
    # https://docs.djangoproject.com/en/3.1/topics/i18n/
    
    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE = 'Asia/Kolkata'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True
    
    DATE_INPUT_FORMATS = ("%d/%m/%Y",)
    
    USE_THOUSAND_SEPARATOR = True
    
    
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/3.1/howto/static-files/
    
    STATICFILES_DIRS = [
        BASE_DIR.joinpath('static')
    ]
    
    STATIC_URL = '/static/'
    
    STATIC_ROOT = BASE_DIR.joinpath('assets')
    
    MEDIA_URL = '/media/'
    
    MEDIA_ROOT = BASE_DIR.joinpath('media')
    
    EMAIL_HOST = 'smtp.mailtrap.io'
    EMAIL_HOST_USER = '******'
    EMAIL_HOST_PASSWORD = '*****'
    EMAIL_PORT = '2525'
    
    DEFAULT_EMAIL_RECEIPENTS = [
        'test@test.com'
    ]
    
    # CELERY CONFIG
    CELERY_BROKER_URL = 'redis://localhost:6379'
    CELERY_RESULT_BACKEND = 'redis://localhost:6379'
    # CELERY_ACCEPT_CONTENT = ['application/json']
    # CELERY_RESULT_SERIALIZER = 'json'
    # CELERY_TASK_SERIALIZER = 'json'
    

    nginx配置:

    server {
        listen [::]:80;
    
        # add here the ip address of your server
        # or a domain pointing to that ip (like example.com or www.example.com)
        server_name galentichrms.tk;
    
        keepalive_timeout 5;
        client_max_body_size 4G;
    
        access_log /home/kunsab/projects/hrms_venv/logs/nginx-access.log;
        error_log /home/kunsab/projects/hrms_venv/logs/nginx-error.log;
    
        
        location /static/ {
            alias /home/kunsab/projects/hrms_venv/hrms/assets/;
        }
        # media files
        location /media/ {
            alias /home/kunsab/projects/hrms_venv/hrms/media/;
        }
    
        
        location / {
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8081;
            proxy_set_header X-Real-IP $remote_addr;
            
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            #proxy_redirect http://127.0.0.1:8080;
        }
        
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   xyres    4 年前

    listen [::]:80; 这意味着nginx将只服务于IPv6请求。但是看到浏览器日志,请求是通过IPv4发送的。

    要同时接受IPv4和IPv6,可以执行以下操作:

    listen 80; # IPv4
    listen [::]:80; # IPv6
    

    还有,你的领域 galentichrms.tk 只有一个 A DNS记录(用于IPv4)。它没有 AAAA 将IPv6地址映射到域的记录。这就是浏览器通过IPv4发送请求的原因。

    如果宿主提供商给了您一个IPv6地址,您还需要配置 AAAA级 记录以便能够接受IPv6通信。