Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Serving MEDIA/uploaded files in production

I currently have this in my project urls.py, the last line is what's important.

urlpatterns = patterns('',
    url(r'^', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I've been told and I've read that this is not suitable for a production environment. Why is this the case?

like image 946
Christopher Reid Avatar asked Apr 03 '14 21:04

Christopher Reid


2 Answers

Django is built to be an "application server", not a "web server".

In other words, serving static files from Django will have worse performance than using Apache or Nginx. These static content servers are (1) written in C and (2) optimized for performance.

In contrast, Django is (1) written in pure Python and (2) optimized for developing an application.

See the documentation.


That may be totally fine. I have used Django to serve static content in production, when I knew the load would not be high and I wasn't serving large files. It depends on what kind of environment "production" actually is.


FYI, A common production setup would be to use Nignx, Django, Gunicorn, and Supervisor. Nginx servers the static content from disk and reverse proxies the rest of it to Gunicorn, which runs multiple Django instances. Supervisor monitors Gunicorn and makes sure it stays running. It all depends on what level of web application you need.

like image 84
Paul Draper Avatar answered Oct 13 '22 19:10

Paul Draper


It is not recommended to serve static files from the django server itself. The recommended way is to serve them in a separate server. check static files deployment, there you will find all you need.

like image 32
argaen Avatar answered Oct 13 '22 18:10

argaen