使用Nginx和Linux进行多个Django站点设置Gunicorn

时间:2014-02-22 05:40:01

标签: python django nginx gunicorn

我需要在我的服务器上运行多个Django项目。在我尝试在不同的端口(8000/8001)上运行Gunicorn后,我尝试了this thread。我假设它不起作用,因为我的Django项目位于不同的虚拟环境中。

要开始使用Gunicorn,我在不同的虚拟环境中运行了manage.py run_gunicorn -b 0.0.0.0:8000manage.py run_gunicorn 0.0.0.0:8001

在端口8001上运行第二个Gunicorn deamon并在Nginx中为我的第二个域添加V-Host后,在我的浏览器中打开URL打开了第一个网站。我重新启动了我的服务器并尝试在端口8001上为第二个站点运行Gunicorn,然后我从Nginx得到了502错误,所以我假设问题与Nginx有关。

Nginx V-Hosts

server {
    listen   80;
    server_name domain.me;
    # no security problem here, since / is alway passed to upstream
    root /opt/it/it/;
    # serve directly - analogous for static/staticfiles
    location /admin/static/admin/ {
        alias /opt/oloty/static/admin/;
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://127.0.0.1:8001/;
    }
    location /static {    
        autoindex on;    
        alias /opt/oloty/static/;    
    }
    # what to serve if upstream is not available or crashes
    #error_page 500 502 503 504 /media/50x.html;
}


server {
    listen   80;
    server_name domain.net;
    # no security problem here, since / is alway passed to upstream
    root /opt/it/it/;
    # serve directly - analogous for static/staticfiles
    location /admin/static/admin/ {
        alias /opt/it/static/admin/;
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    location /static {    
        autoindex on;    
        alias /opt/it/static/;    
    }
    # what to serve if upstream is not available or crashes
    #error_page 500 502 503 504 /media/50x.html;
}

Django设置

"""
Django settings for it project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

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

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'gunicorn',
)

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',
)

TEMPLATE_DIRS = (
    '/opt/it/templates/',
)

ROOT_URLCONF = 'it.urls'

WSGI_APPLICATION = 'it.wsgi.application'

SITE_ID = 1

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

DATABASES = {
    'default': {

    }
}

##Logging

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Detroit'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATICFILES_DIRS = (
    "/opt/it/lib/python2.7/site-packages/django/contrib/admin/static/admin/",
)

STATIC_URL = 'static/'
STATIC_ROOT = '/opt/it/static'


"""
Django settings for oloty project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

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

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'gunicorn',
)

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',
)

TEMPLATE_DIRS = (
    '/opt/it/templates/',
)

ROOT_URLCONF = 'oloty.urls'

WSGI_APPLICATION = 'oloty.wsgi.application'

SITE_ID = 2

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

DATABASES = {
    'default': {

    }
}

#Logging

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Detroit'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = 'static/'
STATIC_ROOT = '/opt/oloty/static'

0 个答案:

没有答案