如何为反向代理服务Django应用正确配置apache

时间:2018-06-28 16:22:11

标签: django apache reverse-proxy

嗨,我一直在尝试正确地将apache配置为我的django应用程序的反向代理。 从端口:4300提供服务时,一切正常,但是当我尝试使用反向代理时,一切都会出错。

登录后尝试访问时

  

192.168.100.201/事实

我获得了预期的功能,但是当我按任何按钮来管理django的管理应用程序时,我都会得到

  

192.168.100.201/admin

我什么时候应该得到

  

192.168.100.201/fact/admin

即使我在浏览器中编写它,我仍然会重定向到192.168.100.201/admin

我知道这可能是一个愚蠢的问题,但这是我无法意识到的。

settings.py

“”“

Django settings for SEMFAC project.

Generated by 'django-admin startproject' using Django 1.11.13.

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

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

import os
from django.utils.translation import ugettext_lazy as _

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'wkf8aie3kc6@xb0-_y%rwmp*tq)hqv7t+d0l6a9wop0l$1m336'

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

ALLOWED_HOSTS = ['192.168.100.205','192.168.100.203','192.168.100.201']


# Application definition

INSTALLED_APPS = [
    'fact.apps.FactConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    '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',
]

ROOT_URLCONF = 'SEMFAC.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'SEMFAC.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

LANGUAGES = (
    ('en-US', _('English')),
    ('es-MX', _('Espanol')),
)

LANGUAGE_CODE = 'es-MX'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/'
LOGOUT_REDIRECT_URL = '/'
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

在urls.py文件中,这是配置

    from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^',include('fact.urls')),
    url(r'^admin/', admin.site.urls),
]

在我的django.conf文件中,配置如下

<VirtualHost 192.168.100.205:4300>
Alias /static /opt/SEMFAC/fact/static
<Directory /opt/SEMFAC/fact/static>
    Require all granted
</Directory>

<Directory /opt/SEMFAC/SEMFAC>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>



WSGIDaemonProcess SEMFAC python-path=/opt/SEMFAC:/opt/SEMFAC/lib/python2.7/site-packages
WSGIProcessGroup SEMFAC
WSGIScriptAlias / /opt/SEMFAC/SEMFAC/wsgi.py
</VirtualHost>

我在apache中的配置

<Location /fact>
  ProxyPreserveHost On
  ProxyPass http://192.168.100.205:4300
  ProxyPassReverse http://192.168.100.205:4300
</Location>

1 个答案:

答案 0 :(得分:0)

这就是窍门

<VirtualHost 192.168.100.205:4300>
Alias /fact/static /opt/SEMFAC/fact/static
<Directory /opt/SEMFAC/fact/static>
    Require all granted
</Directory>

<Directory /opt/SEMFAC/SEMFAC>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>


WSGIDaemonProcess SEMFAC python-path=/opt/SEMFAC:/opt/SEMFAC/lib/python2.7/site-packages
WSGIProcessGroup SEMFAC
WSGIScriptAlias /fact /opt/SEMFAC/SEMFAC/wsgi.py
</VirtualHost>

<VirtualHost *:*>
    ProxyPreserveHost On
    ProxyPass /fact http://192.168.100.205:4300/fact
    ProxyPassReverse /fact http://192.168.100.205:4300/fact

    ServerName localhost
</VirtualHost>
相关问题