The goal of efficiency is more slack.

Friday, March 22, 2019

Django in a subdirectory

How to serve a Django project as a subdirectory of another Django project's URL. For example, the first Django site is at www.example.com/ and the second site is at www.example.com/second-site/.

Software Used

  • Nginx
  • Uwsgi 2
  • Django

Procedure

yum install uwsgi-router-rewrite

Add route-uri to uwsgi configuration /etc/uwsgi.d/example.ini
 [uwsgi]

# Python
plugin = python36, router_rewrite

# Django
chdir = /srv/sites/example
module = example.wsgi
home = /srv/.virtualenvs/example
route-uri = ^/second-site(.*) rewrite:$1

# Process
master = true
processes = 5
socket = /run/uwsgi/example.sock
vacuum = true
procname-prefix-spaced = example
Nginx configuration
server {
...
    location /
second-site/static {
        access_log off;
        alias /
var/www/second-site/static;
    }

    location /second-site/ {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/example.sock;
    }
Configure Django to prefix the subdirectory name to redirects. In settings.py:
FORCE_SCRIPT_NAME = '/second-site'

STATIC_URL = FORCE_SCRIPT_NAME + '/static/'
STATIC_ROOT = '/var/www/
second-site/static'

MEDIA_URL = FORCE_SCRIPT_NAME + '/media/'
ADMIN_MEDIA_PREFIX = FORCE_SCRIPT_NAME + '/admin_media/'
SESSION_COOKIE_PATH = FORCE_SCRIPT_NAME
Reference: https://stackoverflow.com/questions/44987110/django-in-subdirectory-admin-site-is-not-working
(TODO)

0 comments:

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Powered by Blogger.
Scroll To Top