views:

275

answers:

2

I have a django project (django+apache+mod_wsgi+nginx) with multiple apps, I'd like to map each app as a subdomain:

project/
      app1 (domain.com)
      app2 (sub1.domain.com)
      app3 (sub3.domain.com)

I have a single .wsgi script serving the project, which is stored in a folder /apache. Below is my vhost file. I'm using a single vhost file instead of separate ones for each sub-domain:

<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName www.domain.com
    ServerAlias domain.com
    DocumentRoot /home/path/to/app/
    Alias /admin_media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media
    <Directory /home/path/to/wsgi/apache/>
        Order deny,allow
        Allow from all
    </Directory>
    LogLevel warn
    ErrorLog   /home/path/to/logs/apache_error.log
    CustomLog /home/path/to/logs/apache_access.log combined
    WSGIDaemonProcess domain.com user=www-data group=www-data threads=25
    WSGIProcessGroup domain.com
    WSGIScriptAlias / /home/path/to/apache/kcdf.wsgi
</VirtualHost>

<VirtualHost *:8081>
    ServerAdmin [email protected]
    ServerName sub1.domain.com
    ServerAlias sub1.domain.com
    DocumentRoot /home/path/to/app
    Alias /admin_media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media
    <Directory /home/path/to/wsgi/apache/>
        Order deny,allow
        Allow from all
    </Directory>
    LogLevel warn
    ErrorLog   /home/path/to/logs/apache_error.log
    CustomLog /home/path/to/logs/apache_access.log combined
    WSGIDaemonProcess sub1.domain.com user=www-data group=www-data threads=25
    WSGIProcessGroup sub1.domain.com
    WSGIScriptAlias / /home/path/to/apache/kcdf.wsgi
</VirtualHost>

My Nginx configuration for the domain.com:

server {
    listen       80;
    server_name  domain.com;

    access_log  off;
    error_log off;

    # proxy to Apache 2 and mod_wsgi
    location / {
        proxy_pass         http://127.0.0.1:8080/;
        proxy_redirect     off;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

Configuration for the sub.domain.com:

server {
    listen       80;
    server_name  sub.domain.com;

    access_log  off;
    error_log off;

    # proxy to Apache 2 and mod_wsgi
    location / {
        proxy_pass         http://127.0.0.1:8081/;
        proxy_redirect     off;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_max_temp_file_size 0;

        client_max_body_size       10m;
        client_body_buffer_size    128k;

        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
}

This set up doesn't seem to work, everything seems to point to the main domain. I've tried http://effbot.org/zone/django-multihost.htm which kind of worked but seems to have issues with loading my css,images,js files.

+1  A: 

Assuming nginx is working okay, you do have appropriate NameVirtualHost directives set in Apache so that it will map virtual hosts correctly. If you don't, then all requests will got to first virtual host found in configuration file.

Graham Dumpleton
do the different sub-domains have to be configured on separate ports on apache like 8080,8081 etc?
jwesonga
No, the whole point of virtual hosts is that multiple name servers can run on the same port.
Graham Dumpleton
A: 

By default Django isn't designed to have different behavior per hostname. The django-multihost middleware solution works fine for me to enable this functionality. I see that you have configured your admin_media location in your Apache configuration, but not a media location in either the Apache or Nginx configurations, this may be why you are having issues with media?

Beau
I finally got django-multihost to work on my local machine, configured nginx to serve the static files
jwesonga