Django Proxied Behind Apache Path
Middleware and Context Processor for django applications behind a proxy like Apache's mod_proxy. Setting an HTTP header with the base path of the django application in the Apache mod_proxy configuration, we can pull that variable out of the request and adjust our template URIs and redirects. This allows us to serve the same Django application out from behind multiple VirtualHosts at different URL paths.
Example use in templates:
<a href="{{ base_path }}/login/">Login</a>
Example Apache configuration:
<LocationMatch ^/path/to/django/app/.*>
RequestHeader set X-Base-Path "/path/to/django/app"
</LocationMatch>
RewriteRule ^/path/to/django/app/(.*) http://django-host:8080/$1 [P,L]
In settings.py:
VHOST_HEADER = "HTTP_X_BASE_PATH"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #
# middleware
#
from django.conf import settings
from django import http
class VhostMiddleware(object):
def process_request(self, req):
'''
Pull HTTP header containing the vhost base path into the request object
'''
vhost_header_name = getattr(settings, "VHOST_HEADER", "HTTP_X_BASE_PATH")
base_path = req.META.get(vhost_header_name, "")
## eliminate trailing slash ##
if base_path.endswith('/'):
base_path = base_path[:-1]
req.base_path = base_path
return None
def process_response(self, req, res):
'''
Adjust redirects to include the base path
'''
if res.status_code in (301,302) and not res["Location"].startswith(req.base_path) and res["Location"].startswith("/"):
res["Location"] = "%s%s" % (req.base_path,res["Location"])
return res
#
# Context Processor
#
def VhostContextProcessor(request):
ctx = {"base_path": ""}
try:
ctx["base_path"] = request.base_path
except:
pass
return ctx
|
More like this
- Browser-native date input field by kytta 1 month, 1 week ago
- Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
- My firs Snippets by GutemaG 1 month, 3 weeks ago
- FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 3 months, 1 week ago
Comments
Please login first before commenting.