views:

1421

answers:

4

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!

A: 

I'm sure it possible to configure all dynamic and static contents in one config file with one nginx server

ramusus
That's true. The way you do it is with multiple "location" directives in your "server" block. I'm wondering if the parameters for static and dynamic files (keepalives, etc.) are at odds with each other, and if the access patterns for static content and dynamic content somehow interfere with the worker processes if they are on the same daemon.
Adam
+4  A: 

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.

dwc
Thanks, dwc! Good point about using one server now, and passing the traffic along later by way of a location block if I do need to separate things out a bit.
Adam
+2  A: 

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.

sleepyjames
Wonderful! Thanks for the advice by way of lighttpd!
Adam
+4  A: 

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.

Jesper Mortensen
Great response! I'll try to compare the performance of nginx as just a proxy and nginx as the fastcgi bridge once I ge ta chance.
Adam
Thanks Adam. But what I really mean is you should look into Apache with mod_wsgi in daemon mode. mod_wsgi is a Apache loadable module, i.e. works with Apache only. Daemon mode resembles FastCGI in many ways, but uses the richer and more modern WSGI interface instead of CGI
Jesper Mortensen
I'm using nginx to serve both static files and a fastcgi django process on my shared webhosting package as the memory consumption of nginx is much less (around 30Mb for both nginx and django) as opposed to upwards of 100Mb for apache and django)
Frozenskys