Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the Django docs suggest a separate server for static files?

Tags:

From the Django on mod_wsgi page:

We recommend using a separate Web server – i.e., one that’s not also running Django – for serving media.

Why?

like image 360
Snowball Avatar asked Feb 14 '13 17:02

Snowball


People also ask

How does Django serve static files?

Django also provides a mechanism for collecting static files into one place so that they can be served easily. Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT .

Where should I put static files in Django?

Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

Why do we need to serve static files?

Static content is any content that can be delivered to an end user without having to be generated, modified, or processed. The server delivers the same file to each user, making static content one of the simplest and most efficient content types to transmit over the Internet.

What is the difference between media and static files in Django?

Static files are meant for javascript/images etc, but media files are for user-uploaded content.


1 Answers

In general, it's a good idea to put static content - e.g. images, CSS and JS files - on a different server and, furthermore, on a different domain/subdomain. This allows the software that serves the static files to be highly optimized and blazingly fast (for example, nginx).

The other main benefit comes from decreases in network traffic. If you serve static content from the same domain as your dynamic Django app, then client browsers send your domain's cookies as part of their HTTP request, even for static files. This is unnecessary overhead - static files will always be static - but is required because the client can't tell the difference between static and dynamic content. If, on the other hand, the static content was being served from a different domain, then it could be configured as a "cookieless domain", thus minimizing request overhead.

like image 113
voithos Avatar answered Sep 28 '22 04:09

voithos