[英]How to correctly configure apache for reverse proxy serve django app
Hi I've been trying to correctly configure apache as reverse proxy for my django application.嗨,我一直在尝试将 apache 正确配置为我的 Django 应用程序的反向代理。 When served from port :4300 it goes all fine, but when I tried to use reverse-proxy all goes wrong.从端口 :4300 提供服务时一切正常,但是当我尝试使用反向代理时,一切都出错了。
When I try to acces when logged in当我在登录时尝试访问时
192.168.100.201/fact 192.168.100.201/事实
I get the expected functionality, but when I press anything to admin app of django i get我得到了预期的功能,但是当我对 django 的管理应用程序按任何东西时,我得到了
192.168.100.201/admin 192.168.100.201/管理员
when I should get我什么时候应该得到
192.168.100.201/fact/admin 192.168.100.201/事实/管理员
Even if I write it in the browser I'm still redirected to 192.168.100.201/admin即使我在浏览器中编写它,我仍然被重定向到 192.168.100.201/admin
I kwon that maybe is a dub question but it is something that I couldnt realize.我知道这可能是一个愚蠢的问题,但这是我无法意识到的。
settings.py设置.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')
In the urls.py file this is the configuration在 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),
]
In my django.conf file the configuration is the following在我的 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>
And the configuration within apache i have以及我在 apache 中的配置
<Location /fact>
ProxyPreserveHost On
ProxyPass http://192.168.100.205:4300
ProxyPassReverse http://192.168.100.205:4300
</Location>
So this is what did the trick所以这就是诀窍
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.