简体   繁体   English

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

[英]Running multiple sites from a single Python web framework

What are come good (or at least clever) ways of running multiple sites from a single, common Python web framework (ie: Pylons, TurboGears, etc)? 从单个常见的Python Web框架(即:Pylons,TurboGears等)运行多个站点的好方法(或至少是聪明的)是什么? 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. 我知道你可以根据域或路径进行重定向,重写URI指向特定于站点的位置,我也看到了一些野蛮的“ if site == 'site1' / elseif / elseif / etc ”,我会喜欢避免。

Django has this built in. See the sites framework . Django内置了这个。请参阅站点框架

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. 作为一种通用技术,在数据库模式中包含一个“主机”列,该列附加到您希望特定于主机的数据,然后在检索数据时在查询中包含Host HTTP标头。

Using Django on apache with mod_python, I host multiple (unrelated) django sites simply with the following apache config: 使用带有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>

No need for multiple apache instances or proxy servers. 不需要多个apache实例或代理服务器。 Using a different PythonInterpreter directive for each site (the name you enter is arbitrary) keeps the namespaces separate. 为每个站点使用不同的PythonInterpreter指令(您输入的名称是任意的)使命名空间保持独立。

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. 我使用CherryPy作为我的Web服务器(与Turbogears捆绑在一起),我只是在绑定到localhost的不同端口上运行CherryPy Web服务器的多个实例。 Then I configure Apache with mod_proxy and mod_rewrite to transparently forward requests to the proper port based on the HTTP request. 然后我使用mod_proxy和mod_rewrite配置Apache,根据HTTP请求透明地将请求转发到正确的端口。

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. 在本地端口上使用多个服务器实例是个好主意,但您不需要功能齐全的Web服务器来重定向HTTP请求。

I would use pound as a reverse proxy to do the job. 我会用pound作为反向代理来完成这项工作。 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. SSL包装器:Pound将解密来自客户端浏览器的HTTPS请求,并将它们作为普通HTTP传递给后端服务器。
  4. an HTTP/HTTPS sanitizer: Pound will verify requests for correctness and accept only well-formed ones. HTTP / HTTPS清理程序:Pound将验证正确性请求并仅接受格式正确的请求。
  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. 故障转移服务器:如果后端服务器发生故障,Pound会注意到这一事实并停止向其发送请求,直到它恢复。
  6. a request redirector: requests may be distributed among servers according to the requested URL. 请求重定向器:可以根据请求的URL在服务器之间分配请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM