Django:应用程序在服务器上部署时显示 400 错误
Django: app displays 400 error when deployed on server
实际上我有两个问题 here.I 有一个 vps(测试的最低限度)我有 2-3 apps.I 刚刚在上面部署了另一个应用程序并得到了一个来自 freenom.com.I 的免费域名使用 supervisor+nginx
托管 app.For 没有理由我在访问 domain.Generally 时得到这个 Bad Request (400)
当你不访问时就会发生这种情况将域名放在 ALLOWED_HOSTS
列表中,但我已经设置好了。
第二个问题是我 DEBUG = True
仍然只是显示最低限度的错误响应,而不是完整的堆栈跟踪,因此我不确定我实际上遇到了什么错误并且无法调试它。
gunicorn-error.log
文件中没有错误记录。
nginx-error.log
文件中没有错误日志。
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
settings.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;
}
}
listen [::]:80;
——这意味着 nginx 将只处理 IPv6 请求。但是查看浏览器日志,请求是通过 IPv4 发送的。
要同时接受 IPv4 和 IPv6,您可以这样做:
listen 80; # IPv4
listen [::]:80; # IPv6
此外,您的域 galentichrms.tk
只有一个 A
DNS 记录(用于 IPv4)。它没有将 IPv6 地址映射到域的 AAAA
记录。这就是浏览器通过 IPv4 发送请求的原因。
如果您的托管服务提供商为您提供了 IPv6 地址,您还需要配置 AAAA
记录才能接受 IPv6 流量。
实际上我有两个问题 here.I 有一个 vps(测试的最低限度)我有 2-3 apps.I 刚刚在上面部署了另一个应用程序并得到了一个来自 freenom.com.I 的免费域名使用 supervisor+nginx
托管 app.For 没有理由我在访问 domain.Generally 时得到这个 Bad Request (400)
当你不访问时就会发生这种情况将域名放在 ALLOWED_HOSTS
列表中,但我已经设置好了。
第二个问题是我 DEBUG = True
仍然只是显示最低限度的错误响应,而不是完整的堆栈跟踪,因此我不确定我实际上遇到了什么错误并且无法调试它。
gunicorn-error.log
文件中没有错误记录。nginx-error.log
文件中没有错误日志。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
settings.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;
}
}
listen [::]:80;
——这意味着 nginx 将只处理 IPv6 请求。但是查看浏览器日志,请求是通过 IPv4 发送的。
要同时接受 IPv4 和 IPv6,您可以这样做:
listen 80; # IPv4
listen [::]:80; # IPv6
此外,您的域 galentichrms.tk
只有一个 A
DNS 记录(用于 IPv4)。它没有将 IPv6 地址映射到域的 AAAA
记录。这就是浏览器通过 IPv4 发送请求的原因。
如果您的托管服务提供商为您提供了 IPv6 地址,您还需要配置 AAAA
记录才能接受 IPv6 流量。