views:

170

answers:

1

I am trying to set up multiple virtual hosts on the same server with Nginx and Apache and have run into a curious configuration issue.

I have nginx is configured with a generic upstream to apache.

upstream backend {
  server 1.1.1.1:8080;
}

I'm trying to set up multiple subdomains in nginx that hit different mountpoints in apache. Each would act like the following examples.

server {
    listen 80;
    server_name foo.yoursite.com;

    location / {
            proxy_pass http://backend/bar/;
            include /etc/nginx/proxy.conf;
    }
    ...
}

server {
    listen 80;
    server_name delta.yoursite.com;

    location / {
            proxy_pass http://backend/gamma/;
            include /etc/nginx/proxy.conf;
    }
    ...
}

These mountpoints are pointed at django projects, however each of the url entries are coming back prepended with the apache mountpoint path. So, if I called the django url entry for foo.yoursite.com/wiki/biz/, django appears to be returning foo.yoursite.com/bar/wiki/biz/. Similarly, if I call for the url entry for delta.yoursite.com/wiki/biz/, I get delta.yoursite.com/gamma/wiki/biz/.

Is there any way get rid of the prefix being returned on the url entries by django and apache?

+1  A: 

Easiest way is to use following in WSGI script file:

... existing stuff

import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):
    # Wrapper to clear SCRIPT_NAME..
    environ['SCRIPT_NAME'] = ''
    return _application(environ, start_response)

Problem comes about because each server mounts at different URL. You thus have to make backend think it was actually mounted at root of server by clearing SCRIPT_NAME.

Note that this will cause problems if you are also accessing back end directly. In that situation you would need to modify above to only do this if request came via proxy.

Graham Dumpleton
Clearing the SCRIPT_NAME environment variable did the trick. Thanks, Graham!
Thomas