从一个Python Web框架运行多个站点

时间:2020-03-05 18:59:20  来源:igfitidea点击:

从单个通用的Python Web框架(例如Pylons,TurboGears等)运行多个站点的好方法(或者至少聪明的方法)是什么?我知道我们可以根据域或者路径进行重定向以重写URI以指向特定于站点的位置,并且我也看到过一些粗鲁的" if site =='site1'/ elseif / elseif / etc"我想避免。

解决方案

回答

Django内置了此功能。请参阅sites框架。

作为一种通用技术,在数据库架构中包含一个"主机"列,该列添加到要特定于主机的数据上,然后在检索数据时在查询中包含"主机" HTTP标头。

回答

我使用CherryPy作为我的Web服务器(与Turbogears捆绑在一起),并且我只是在绑定到localhost的不同端口上运行CherryPy Web服务器的多个实例。然后,我用mod_proxy和mod_rewrite配置Apache,以根据HTTP请求将请求透明地转发到正确的端口。

回答

在本地端口上使用多个服务器实例是一个好主意,但是我们不需要功能齐全的Web服务器来重定向HTTP请求。

我会用英镑作为反向代理来完成这项工作。它体积小,速度快,简单,可以满足我们这里的需求。

WHAT POUND IS:
  
  
  a reverse-proxy: it passes requests from client browsers to one or more back-end servers.
  a load balancer: it will distribute the requests from the client browsers among several back-end servers, while keeping session information.
  an SSL wrapper: Pound will decrypt HTTPS requests from client browsers and pass them as plain HTTP to the back-end servers.
  an HTTP/HTTPS sanitizer: Pound will verify requests for correctness and accept only well-formed ones.
  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.
  a request redirector: requests may be distributed among servers according to the requested URL.

回答

通过mod_python在apache上使用Django,我仅使用以下apache配置来托管多个(不相关)django站点:

<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>

无需多个apache实例或者代理服务器。对每个站点使用不同的PythonInterpreter指令(我们输入的名称是任意的)可以将名称空间分隔开。