Skydrow
By:
Skydrow

Extra domain for django admin or any other urls

November 7, 2014 1.8k views

Hi, guys, my django app is using 2 domains, but one of them needs to be for the django admin or any other urls and not the main website.

How i can solve this?, im using gunicorn and nginx.

Thank you

1 Answer

It's hard to give you a good answer without some more details about your project. If both domains are part of the same project, you'll probably need to use something like Django's “sites framework" in order to direct requests correctly. Is this something you are already doing?

Using gunicorn, you'll need to run a seperate processes for each site as each will have it's own settings file. For instance, to run the admin portal on a separate domain you could create a settings_admin.py like:

from settings import *
ROOT_URLCONF = 'urls_admin'

And an urls_admin.py file like:

from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
    url(r'', include(admin.site.urls)),
)

Then launch the gunicorn processes (of course you'll want to set this up with Upstart or supervisord in production):

gunicorn_django -w 2 -b 127.0.0.1:9000 /path/to/project/settings_admin.py --daemon
gunicorn_django -w 2 -b 127.0.0.1:9001 /path/to/project/settings.py --daemon

Then you need to configure Nginx:

upstream admin_site {
    server 127.0.0.1:9000 fail_timeout=0;
}

upstream main_site {
    server 127.0.0.1:900` fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    client_max_body_size 4G;
    server_name admin-site.com;

    keepalive_timeout 5;

    location /media  {
        alias /path/to/media;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://admin_site;
    }
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    client_max_body_size 4G;
    server_name main-site.com;

    keepalive_timeout 5;

    location /media  {
        alias /path/to/media;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://main_site;
    }
}
Have another answer? Share your knowledge.