Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, why would I need nginx and uWSGI during hosting?

I have experience in hosting simple Django projects in Pythonanywhere platform(I did't have to install nginx and uWSGI ).

Many people use nginx+Uwsgi with Django, why would that required ?

I hope nginx is a web server, load balancer, mail proxy and HTTP cache. Uwsgi is a webs server gateway interface.

Does all those things are being included default in Heroku / Pythonanywhere platforms ?

like image 284
Anoop K George Avatar asked Feb 03 '23 14:02

Anoop K George


1 Answers

PythonAnywhere developer here: yes, that's right -- we do have nginx and uWSGI installed. When you create a website on the "Web" page on our site, what happens under the hood is (simplifying a bit) that we generate the appropriate nginx/uWSGI configuration files for you and start everything up so that you only need to work on the Django code.

The reason that those tools (or similar ones like Apache and mod_wsgi) are necessary is that Django's built-in webserver is not designed for production use. You can run its "manage.py runserver" command to make it serve up pages on your own machine, but the system it uses for that is not designed for security or efficiency -- it just provides an easy way for you to get something running for debugging purposes. The same is true for the built-in webservers for the other Python web frameworks like Flask and web2py.

nginx is designed to be fast, efficient, and secure, so it's a better choice to handle incoming web requests when your website is on the public Internet and thus subject to large amounts of traffic (if you're lucky and your site takes off) and also to abuse from hackers. That's not to say that it automatically makes your site fast and secure, of course, but at least it means that you're starting with the right system. It's also much better at serving static files (like your CSS, JavaScript, images, and so on) than Django is, because that's what it was built for.

uWSGI is designed to receive incoming web requests and rapidly and efficiently delegate processing them to multiple worker processes, then collate the responses and send them back to nginx.

Of course, all of that could in theory be built into Django instead -- but it would be a lot of work for the Django team to do that, and it would be a waste of time for them to re-invent the wheel rather than focusing on the areas where Django provides its real benefits, of making it easy to quickly develop complex websites.

like image 73
Giles Thomas Avatar answered Feb 08 '23 16:02

Giles Thomas