Overview of Single vs. Multi Server Architecture
apache (5), 建筑(30), nginx(5)Each time I post asetup guide for configuring a Django serverthere are questions about how I came upon the Nginx and Apache multi-server approach as opposed to the myriad of alternatives. There are two parts to this question:
- Why a multi-server approach instead of a single-server approach?
- Why Nginx, Apache and
mod_wsgi
,特别是,而不是nginx的某种组合,而不是nginx,mod_python.
, fcgi, Lighttpd, etc?
This article will take a stab at explaining my reasoning for the first question: why use a multi-server setup to serve Django projects?
组件
服务器设置中使用了许多竞争组件,并且对于此讨论,该组件专门使用的组件不是特别有趣。更具体
When I refer toNginx,它实际上可以是任何轻量级HTTP服务器。这可能意味着Lighttpd或者even a stripped downApacheinstance.
当我引用Apache和
mod_wsgi, that could be Apache andmod_python., or even Nginx runningFCGI。它是任何服务的服务器Djangoprojects.
Postgres是my relational database of choice, but you could substitute it withMySQL或者SQLite。地狱,你甚至可以使用CouchdB.for the extent of this discussion.
For caching I mentionmemcached,这没有许多直接竞争对手,但在某种程度上,它们可以互换使用。
单服务器方法
服务Django项目的最简单方法是配置一台服务器,以帮助动态(Django渲染页面)和静态内容(图像,CSS,JavaScript等)。
这种单一服务器方法的最大优点是简单性。特别是apache - 但其他系统也非常快速,非常无痛,可以为上面的配置设置(我找到nginx和fcgi一点直截了当,但肯定也是如此)。
另一个优点是您只需要一次运行一台服务器,而服务器可以吞噬内存和CPU,尤其是在像小型VP等低功耗解决方案上。
That said, the single-server approach is a bit unwieldy when it comes to scaling your site for higher loads. As with other approaches, you can begin scaling the single-server setup vertically by purchasing a more powerful VPS or server, but trouble starts to sneak in when it is no longer cost-effective to increase capacity by upgrading your machine.
在达到这种情况时,您的第一部举措通常是将数据库移动到第二台机器,这很容易完成。但是,当您需要添加更多用于服务动态或静态内容的容量时,设置有点不灵活。您的第一个也是唯一的选择是在负载平衡器后面添加其他机器。
Although in reality you'd likely have Postgres on a separate machine.
In review, the single-server setup may be the most efficient option for low resource environments, is relatively simple to configure1, but must serve static and dynamic content with the same configuration, and thus may open itself up for inefficiencies as demands grow (we'll discuss this more below).
The Multi-Server Approach
任何阅读教程描述的人mod_python.
使用可能已阅读该术语脂肪螺纹, which is the word du jour for describing Apache worker threads whenmod_python.
已启用。这是因为每个mod_python.
worker thread needs a Python interpreter associated with it, causing each worker thread to consume approximately 20 megs of ram.
There are good reasons for that, andmod_python.
在服务动态请求时非常快速,因此成本肯定是合理的。但是,为什么当一个更轻的时,为什么有一个沉重的线程服务静态文件是足够的?此外,我们如何通过缓慢连接垄断我们相对较低的繁重线程(并且作为能够为更少的并发用户提供服务)的群数?
该问题的标准答案是具有一个轻量级服务器,该服务器是用户的第一个内容,服务于静态媒体,并对第二服务器上的重度线程进行动态媒体的代理请求。
Essentially the argument being made be the multi-server setup is that specialization leads to efficiency. By allowing one server to focus only on serving dynamic media we can tweak its settings to maximize performance for that task. By allowing a second server to focus on serving static media, we can optimize it as well.
Beyond thespecialization leads to optimizationargument, the multi-server approach has another benefit over the single-server approach: ease of efficient scaling. Because we have separated the concerns from one another, it makes it very easy to scale efficiently by adding capacity exactly where it is needed.
With only minimal configuration file changes you could switch the simple one machine model to a many machine model. This is especially simple if the narrowest point in your pipe is serving dynamic content.
不可否认,该系统确实将Nginx询问为代理和静态媒体服务器,如果服务静态介质需要多于一台机器的吞吐量,则需要移动到服务器池方法(最终确定关注的分离)。
To a certain extent it's much easier to scale vertically (purchase a larger VPS or server) than to scale horizontally, and that should always be the first choice. However, should you reach a point where scaling vertically ceases to be cost-effective, the multi-server approach provides a tremendous amount of flexibility和allows higher gains per server due to specialization.
In the end, there isn't a right-wrong choice between using the single-server or the multi-server approach. While the multi-server approach willeventuallyout-perform the single-server approach, with limited resources may underperform it.
That said, my experience with the multi-server approach has been very positive, even with very limited resources (256 megabytes of RAM on a VPS), so--if you're willing to do some extra configuration--I'd personally recommend going with multi-server from the start.
Although I'd argue that anyease-of-configurationbenefits for either approach are outweighed by the complexity of configuring the entire system for firewalls, ssh, etc. With a bit of experience and good references, neither the single-server or multi-server approach is very difficult to configure.↩