Django uWsgi Nginx 无数据库数据,以及页面上的 502 错误

2024-10-04 05:31:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让我的产品门户网站用Django编写,但是我有一些问题。页面将解析为登录页面,身份验证将起作用并将您带到第一个页面。但是没有数据库数据在那里,而且如果我试图切换页面,它将给我一个502错误。在

当我使用Django web服务器时,一切正常。我相信这只是uWsgi设置的问题。这是我第一次用django将服务器发布到生产环境中,因此我为自己的知识不足道歉。在

This is the error that triggers inside the log file upon switching pages
2014/10/22 17:19:03 [error] 21424#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 172.17.1.16, ser$

我读到这是因为需要设置uWsgi文件:

^{pr2}$

设置位置:

我的项目/应用程序结构是

/var/www/dashboard/holon   # this is the project roon where I can run manage.py test server and everything will work.

/var/www/dashboard/holon/holon  #this is the project settings directory

/var/www/dashboard/holon/portal/ #this is the app directory

/var/www/dashboard/conf #this is where the Nginx and uWsgi config files live

uWsgi配置:

[uwsgi]
# variables
projectname = holon
projectdomain = enki.co
base = /var/www/dashboard

# config
plugin = python
master = true
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/%(projectname)
module = %(projectname).wsgi
socket = 127.0.0.1:8001
logto = %(base)/logs/uwsgi.log
#below line runs it as a daemon in background
daemonize = /var/log/uwsgi/holon_dashboard.log

Nginx配置:

server {
    listen 80;
    server_name dashboard.enki.co;
    root /var/www/dashboard/holon;
    access_log /var/www/dashboard/logs/access.log;
    error_log /var/www/dashboard/logs/error.log;

    location /static/ { # STATIC_URL
        alias /var/www/dashboard/holon/portal/static/; # STATIC_ROOT
        expires 30d;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8001;
    }
}

我的设置.py文件

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import ldap
from django_auth_ldap.config import *
import logging


BASE_DIR = os.path.dirname(os.path.dirname(__file__))

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'holon.backend.ActiveDirectoryBackend',
)

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

TEMPLATE_DEBUG = False

ALLOWED_HOSTS = [
         '127.0.0.1',
         '.enki.co',
         '172.19.32.220']


# Application definition

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

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'holon.urls'

WSGI_APPLICATION = 'holon.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'metering',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '5433',
    },
    'OPTIONS': {
    'threaded': True,
    'use_returning_into': True,
    },
}

SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    TEMPLATE_PATH,
)


LOGIN_URL = '/login/'
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/tmp/debug.log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },

    },
}

STATIC_URL = '/static/'

Tags: thepathdjangologtrueisosvar
1条回答
网友
1楼 · 发布于 2024-10-04 05:31:57

我也有类似的问题。django-run服务器一切正常,但在uwsgi中有一个数据库访问错误。我的解决方案是确保pythonpath配置正确。 确保已配置。在

尝试将其更改为硬编码链接,而不是:

pythonpath = %(base)/%(projectname)

相关问题 更多 >