views:

396

answers:

4

What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? I know you can do redirection based on the domain or path to rewrite the URI to point at a site-specific location and I've also seen some brutish "if site == 'site1' / elseif / elseif / etc" that I would like to avoid.

+8  A: 

Django has this built in. See the sites framework.

As a general technique, include a 'host' column in your database schema attached to the data you want to be host-specific, then include the Host HTTP header in the query when you are retrieving data.

Jim
+1  A: 

I use CherryPy as my web server (which comes bundled with Turbogears), and I simply run multiple instances of the CherryPy web server on different ports bound to localhost. Then I configure Apache with mod_proxy and mod_rewrite to transparently forward requests to the proper port based on the HTTP request.

Eli Courtwright
A: 

Using multiple server instances on local ports is a good idea, but you don't need a full featured web server to redirect HTTP requests.

I would use pound as a reverse proxy to do the job. It is small, fast, simple and does exactly what we need here.

WHAT POUND IS:

  1. a reverse-proxy: it passes requests from client browsers to one or more back-end servers.
  2. a load balancer: it will distribute the requests from the client browsers among several back-end servers, while keeping session information.
  3. an SSL wrapper: Pound will decrypt HTTPS requests from client browsers and pass them as plain HTTP to the back-end servers.
  4. an HTTP/HTTPS sanitizer: Pound will verify requests for correctness and accept only well-formed ones.
  5. a fail over-server: should a back-end server fail, Pound will take note of the fact and stop passing requests to it until it recovers.
  6. a request redirector: requests may be distributed among servers according to the requested URL.
Marcel
+6  A: 

Using Django on apache with mod_python, I host multiple (unrelated) django sites simply with the following apache config:

<VirtualHost 1.2.3.4>
        DocumentRoot /www/site1
        ServerName site1.com
        <Location />
                SetHandler python-program
                SetEnv DJANGO_SETTINGS_MODULE site1.settings
                PythonPath "['/www'] + sys.path"
                PythonDebug On
                PythonInterpreter site1
        </Location>
</VirtualHost>

<VirtualHost 1.2.3.4>
        DocumentRoot /www/site2
        ServerName site2.com
        <Location />
                SetHandler python-program
                SetEnv DJANGO_SETTINGS_MODULE site2.settings
                PythonPath "['/www'] + sys.path"
                PythonDebug On
                PythonInterpreter site2
        </Location>
</VirtualHost>

No need for multiple apache instances or proxy servers. Using a different PythonInterpreter directive for each site (the name you enter is arbitrary) keeps the namespaces separate.

Gabriel Ross