How to run Django On a subpath – DevOps

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top