Multiple Django installations - How to set up a transparent multiplex through a web server (Lighttpd)?

This question arises from the answer to: How to set up multiple accounts with separate databases for Django on the same server?

I haven’t seen anything like it on Google or elsewhere (maybe I have the wrong dictionary), so I believe that input can be a valuable addition to online discussions.

How to configure the server configuration:

  • One Lighttpd installation
  • Several Django projects running FastCGI
  • Django projects can be added / removed as desired and do not require a web server restart
  • Transparently redirecting all requests / responses to a specific Django installation depending on the current user

those. Given the Django projects (with the corresponding FastCGI socket):

  • Bob (/tmp/bob.fcgi)
  • Sue (/tmp/sue.fcgi)
  • Joe (/tmp/joe.fcgi)

Django projects start with a (simplified) script likeso:

#!/bin/sh
NAME=bob

SOCKET=/tmp/$NAME.fcgi

PROTO=fcgi
DAEMON=true

/django_projects/$NAME/manage.py runfcgi protocol=$PROTO socket=$SOCKET
  daemonize=$DAEMON

I want traffic to http://www.example.com/ to redirect the request to the correct Django application depending on the user who is logged in.

In other words, http://www.example.com should show "be" /tmp/bob.fcgi if bob is logged in, /tmp/joe.fcgi if joe is logged in, /tmp/sue.fcgi, if sue is logged in. If no one is logged in, he should redirect to the login page.

I reviewed the demultiplexing "plexer" FastCGI script with the following algorithm:

  • If the cookie is set to $ PLEX, the channel request in / tmp / $ PLEX.fcgi

  • ( cookie PLEX Username = > PLEX)

, $PLEX , $PLEX .

Lighttpd ( Apache, Nginx .. ):

fastcgi.server = ( "plexer.fcgi" =>
                           ( "localhost" =>
                             (   
                               "socket" => "/tmp/plexer.fcgi",
                               "check-local" => "disable"
                             )
                           )   
                 )

, , plexer FastCGI, .

.

+1
1

, :

lighttpd.conf

$SERVER["socket"] == "localhost:81" {
  include_shell "/opt/bin/lighttpd_conf.py"
}

lighttpd_conf.py:

#!/usr/bin/python
import fileinput
ACCOUNT_LIST_FILE = "/opt/servers/account_list.txt"

for user in fileinput.input(ACCOUNT_LIST_FILE):
    print """
    $HTTP[\"url\"] =~ \"^/%s/\" {
        scgi.server = ( \"/\" => 
            (
            (
                \"socket\" => \"/tmp/user-socket-%s.scgi\",
                \"check-local\" => \"disable\",
            )
            )
        )
    }
    """ % (user, user)

ACCOUNT_LIST_FILE ,

abc1
abc2
abc3

http://example.com/abc1 /tmp/user -socket-abc1.scgi, Django abc1 SCGI.

, - ( ).

+1

Source: https://habr.com/ru/post/1712789/


All Articles