nginx + 用于 django 应用程序的 FastCGI——运行两个还是一个 Web 服务器?
我即将在 nginx Web 服务器上部署 Django 应用程序,并希望确保正确构建系统。
常识似乎是,如果您在 apache 服务器上部署 Django,那么您仍然应该在应用程序前面放置一个 nginx 服务器来提供静态文件,这样 nginx 的性能更高。
如果我想使用 nginx + FastCGI 代替 Django 代码来托管 Django 应用程序,是否有任何理由配置第二个 nginx 安装以位于提供动态内容的 nginx 服务器前面,以处理静态内容内容以及重定向到动态内容?
具体来说,静态和动态内容是否有不同的配置参数,这会让我想要将服务器分开,或者我可以将其全部托管在单个 nginx 安装中,其中一些 URL 映射到 django 内容,并且其余的被映射到同一个 nginx 安装提供的静态内容吗?
谢谢你的建议!
I'm about to deploy a Django application on a nginx web server, and want to make sure I'm building the system correctly.
It seems to be common wisdom that if you are deploying Django on an apache server, then you should still put an nginx server in front of the application to serve static files, at which nginx is more performant.
If instead of apache for the Django code, I would like to use nginx + FastCGI to host the Django application, is there any reason to configure a second nginx install to sit in front of the nginx server that is serving dynamic content, to handle static content as well as redirection to the dynamic content?
Specifically, will there be different configuration parameters for the static and dynamic content that would make me want to keep the servers separate, or can I host it all in a single nginx installation, with some of the URLs being mapped to django content, and the rest being mapped to static content served from the same nginx install?
Thanks for your advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我确信可以使用一台 nginx 服务器在一个配置文件中配置所有动态和静态内容
I'm sure it possible to configure all dynamic and static contents in one config file with one nginx server
大多数配置指令可以存在于位置块内(即,它们不是全局的),并且在实践中这样做是很常见的。 仅使用 1 个 nginx 实例进行设置应该不会有任何问题。
这样做的好处之一是,您可以最初以这种方式进行设置,然后通过切换位置块以传递到后端服务器而改变主意,而外界不可见任何内容。
因此,现在就在一台服务器上执行此操作,并知道您可以在以后需要扩展时添加后端服务器或集群。
Most config directives can live inside location blocks (i.e., they are not global-only) and it's very common to do this is practice. You should have no trouble setting this up using only 1 instance of nginx.
One of the great things about this is that you can set it up this way initially and then change your mind later by switching the location block to pass through to a backend server without any of that being visible to the outside world.
So go ahead and do it on one server now, knowing that you can put in a backend server or cluster later as you need to scale up.
回答关于将一个 nginx 服务器放在另一个 nginx 前面的问题:不,通常没有充分的理由这样做。 这个旧建议来自 Apache,特别是当 mod_python 与 Apache prefork MPM 一起使用时。 在此设置中,Django 的每个实例都将作为单独的进程在 mod_python / Apache 容器内运行,这将使用大量 RAM。 这个想法是通过在繁重的 Apache 进程前面放置一个轻量级事件驱动的 HTTP 服务器(如 nginx)来保持静态文件服务远离 Apache。 这节省了 RAM 并提高了性能。 当使用像 nginx 这样的轻量级服务器来处理所有请求时,这不是问题。
nginx 对 URL 重写有很好的处理能力,请查看 Rewrite 模块。
您的问题没有说明您期望的负载(连接数/秒),也没有说明您为什么要首先使用 nginx。 如果这是针对 VPS 服务器上的博客或类似的低负载设置,那么请考虑在守护程序模式下使用带有 mod_wsgi 的 Apache。 这具有性能和 RAM 使用情况非常接近 FastCGI,并且 mod_wsgi 最近成为官方推荐的托管 Django 的方式,请参阅
http://docs.djangoproject.com/en/dev/howto/deployment /modwsgi/
一般来说,如果可能的话,我建议使用 Apache / mod_wsgi,它是一个稳定且灵活的组合。 确保您没有使用 nginx 来“过早优化”,而 Apache + mod_wsgi 就可以做得很好。 有关守护进程模式下 mod_wsgi 的性能概述,请参阅:
http://code.google.com/p/modwsgi/wiki/PerformanceEstimates
nginx 很棒,但对于 Django 解决方案,恕我直言,nginx 更适合作为许多 Apache 实例的负载均衡器,或者静态文件的单独服务器。 这两种使用场景都只对大负载有意义。
To answer your question about putting an nginx server in front of another nginx: No, there is normally no good reason for doing that. This old advice comes from Apache, especially when mod_python was used with the Apache prefork MPM. In this setup each instance of Django would run as a separate process, inside a mod_python / Apache container, and this would use a lot of RAM. The idea was to keep keep static file serving away from Apache, by placing a light-weight event-driven HTTP server like nginx in front of the heavy Apache processes. This saved RAM and increased performance. When using a light-weight server like nginx for all requests this is a non-issue.
nginx has good handling of URL rewriting, look into the Rewrite module.
Your question doesn't say what load (connections/second) you are expecting, or why you want to use nginx in the first place. If this is for a blog on a VPS server or similar low-load setup, then look into using Apache with mod_wsgi in daemon mode. This has performance & RAM usage very close to FastCGI, and mod_wsgi recently became the officially recommended way of hosting Django, see
http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
In general I would suggest using Apache / mod_wsgi if possible, it is a stable and flexible combination. Be sure that you're not "prematurely optimizing" by using nginx where Apache + mod_wsgi would do just fine. For a performance overview of mod_wsgi in daemon mode see:
http://code.google.com/p/modwsgi/wiki/PerformanceEstimates
nginx is awesome, but for a Django solution nginx is IMHO a better fit as a load balancer for many Apache instances, or a separate server for static files. Both of these usage scenarios are only meaningful for large loads.
我想说的是,如果你使用 mod_python,代理 django 到它自己的服务器才真正起作用,即:使用 nginx 提供静态服务,并将 django 代理到运行 mod_python 的 apache 实例。 我很高兴通过 fastcgi 在 lighttpd 中运行 django,并且同样的 lighttpd 也提供静态内容。
I would say proxy-ing django to it's own server only really comes in if your rolling with mod_python, ie: serve static with nginx and proxy django to an apache instance running mod_python. I happily run django in lighttpd via fastcgi with the same lighttpd serving static content too.