简体   繁体   中英

Django + uWsgi + Nginx No Database Data, Plus 502 Error on page change

I'm trying to get my production Web Portal written in Django to work, however I'm having some issues. The page will resolve to the login page, and the authentication will work and take you to the first page. However none of the database data will be there, furthermore if I try to switch pages it will give me a 502 error.

When I use the Django web server everything works correctly. I believe its just an issue with uWsgi settings. This is my first time releasing a server into production with django so I apologize for my lack of knowledge.

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$

I've read that its because the uWsgi file needs to be set:

plugin = Python

which it is set:

My project / app structure is

/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 config:

[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 Config:

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;
    }
}

my settings.py file

# 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/'

I had a similar problem. everything worked fine with django run server but in uwsgi I had a database access error. my solution was to make sure that the pythonpath is configured correctly. make sure it's configured.

try change it to hard coded link and not :

pythonpath = %(base)/%(projectname)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM