Nginx + FastCGI for django application - run two web servers or one?

I am going to deploy a Django application on the nginx web server and want me to build the system correctly.

It seems common wisdom that if you are deploying Django on an Apache server, then you should still put the nginx server in front of the application to serve static files in which nginx is more efficient.

If instead of apache for Django code I would like to use nginx + FastCGI to host the Django application, is there any reason to configure a second nginx installation to work in front of the nginx server serving dynamic content, to handle static content, and also redirect to dynamic content ?

In particular, will there be different configuration options for static and dynamic content that make me want to keep the servers separate or can I put it all in one nginx installation, with some of the URLs mapped to django content, and the rest mapped to static content serviced by the same nginx installation?

Thank you for your advice!

+6
django nginx deployment fastcgi web-deployment-project
source share
4 answers

Most configuration directives can live inside location blocks (i.e. they are not global), and very often this is done in practice. You should have no problem setting this parameter using only 1 nginx instance.

One of the great things about this is that you can first set it up that way and then change your mind later by switching the location block to go to the server server, without the visibility of the outside world.

So go ahead and do it on the same server now, knowing that later you can add a server server or cluster to increase it.

+5
source share

To answer the question about placing the nginx server in front of another nginx: No, usually there is no good reason for this. This old tip comes from Apache, especially when mod_python was used with MPM Apache prefork. In this installation, each instance of Django started as a separate process, inside the mod_python / Apache container, and this would use a lot of RAM. The idea was to keep Apache's static file service by putting a lightweight event-driven HTTP server like nginx in front of Apache's heavy processes. This saves memory and increases productivity. Using a lightweight server like nginx for all requests is not a problem.

nginx has good URL rewriting handling, take a look at the Rewrite module.

Your question does not indicate what load (connections / second) you expect, 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, look at using Apache with mod_wsgi in daemon mode. This has performance and RAM usage very close to FastCGI, and mod_wsgi has recently become the officially recommended way to host Django, see http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/

In general, I would suggest using Apache / mod_wsgi, if possible, this is a stable and flexible combination. Make sure you don’t “prematurely optimize” using nginx, where Apache + mod_wsgi will work fine. For an overview of mod_wsgi's performance in daemon mode, see: http://code.google.com/p/modwsgi/wiki/PerformanceEstimates

nginx is awesome, but for Django's solution, nginx is IMHO better suited for load balancing for many Apache instances or for a standalone server for static files. Both of these usage scenarios make sense only for large loads.

+4
source share

I would say that proxy-ing django really comes to your own server if you ride with mod_python, that is: you stat with nginx and proxy-django into an apache instance running on mod_python. I happily run django in lighttpd via fastcgi with the same lighttpd, which is also static content.

+2
source share

I am sure that you can configure all dynamic and static content in one configuration file using one nginx server

0
source share

All Articles