DevOps

How to run Django On a subpath – DevOps

JJ
Jobin Joseph · Founder and CEO
·Published 23 June 2019·1 min read

Today, we were trying to run the Django application on a subpath say /backend/ . We were able to run it but some URL redirection from the application was still pointing it to the / URL instead of /backend/

Let me share the configuration a high level, please comment if you need any clarification.

My Django project was running with gunicorn on the URL http://127.0.0.1:8180/

Below is the configuration on my Apache, and yes the application should be on h

SSLProxyEngine on
ProxyPreserveHost On

###########proxy settings###
ProxyPass http://127.0.0.1:8180/
ProxyPassReverse http://127.0.0.1:8180/
RequestHeader set "Host" "www.nixjango.com"
RequestHeader set X-SCRIPT-NAME '/backend/'
ProxyErrorOverride Off

With the above configuration the application will still forward any redirection from the /backend/ , ie say /backend/admin/ to /admin . So we need to add force /backend from the settings.py on the Django Code.

So, simply add these lines to your settings.py

FORCE_SCRIPT_NAME = '/backend'
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

Once the above lines are added, restart gunicorn and apache. Now, you will be able to access the /backend/ and /backend/admin/ URL redirection will just work fine.

Jobin Joseph
Jobin Joseph, Founder and CEO
5x AWS Certified, ISO 27001:2022 Lead Implementer, 17+ years building cloud infrastructure for regulated industries.
About Jobin →

Reading this because your AWS bill or uptime is a problem?

30 minutes, direct with the founder, no sales pitch: one specific recommendation for your AWS workload.

Book a free 30-minute review
30 min Free Consultation →