mod_deflate vs Django GZipMiddleware, which one to use for deployment? - django

We're deploying Django apps with Apache 2.2 + mod_wsgi. Should we enable mod_deflate in Apache or use Django's GZipMiddleware? Which option performs better?

You should probably test to know for sure, but if I were to guess, mod_deflate would be better for requests that totally bypass Django completely (like zipped up static resources that should be deployed separately i.e. media).
For things that are already generated by Django responses, it's probably a toss-up -- in either case it would be native code doing the zipping.

It depends. If you enable it in Apache, then it will also be used for static content (e.g. CSS, Javascript, images); but some static content (like JPG, GIF, SWF) is pretty well compressed anyway.

mod_deflate is a better choice because it allows you to choose which content types are compressed (defaults to html, css and js).
GZipMiddleware is very naive and will try to compress anything and just check if the result is smaller than the original response. If you're serving images that way you will take the performance hit for each request with 0 benefit.

I would definitely go with mod_deflate, on the assumption that you have static css and js files which apache is serving directly and can also be compressed.

Related

Django: caching images

I am currently using django-compressor which provides great benefits by caching compressed CSS and JS files.
Now I would like to cache images. I know that browser caching can be enabled using HTTP headers (downstream caching) but I would like to use Redis here (it is already used by django-compressor).
So this is my first question: is it right to cache images with Redis?
I have read interesting things about sorl-thumbnail and its caching through the thumbnail template tag. I thought maybe I could use it in a raw way but the tag requires a geometry argument.
Is there an existing Django package I could use to cache images? Should I write a template tag similar to sorl-thumbnail's thumbnail tag to enable image caching by using Django's built-in cache?
This is not a job for redis and isn't a good use case for django. If you want to cache images in memory to serve from the server, front your django app server (like uwsgi or gunicorn) and nginx and use varnish to cache the images in memory. Redis does not do great for storing/retrieving large blobs.

Content negotiation with Django staticfiles in runserver

I'm managing static files (JS, CSS, images, etc) in my Django application using staticfiles. This works fine, but I'd like to start dynamically serving pre-compressed sources when the user's browser is capable.
I went through the linked tutorial and, in production (on Apache) this works fine. I can include files using
<script src="/static/js/my-site"></script>
and it will load my-site.js in older browsers and my-site.js.gz when GZip encoding is supported. Great! But: this breaks local development using runserver. Of course, the staticfiles default view has no idea how to turn /js/my-site into /js/my-site.js (or .gz). To get runserver working, I need to specify the extension, which breaks content negotiation.
Is there a better way to configure Apache, so that I can always request .js (or .css, etc) and get served the compressed version transparently? Or can I tell Django how to find the requested resource without specifying an extension? I wouldn't think I'm the only one trying to do this...
there is no simple resolution. mostly because you are using something that was designed for the apache web server only (afaik).
i my opinion there are 3 solutions:
keep source .{js,css} files in separate directory, in development you can serve them from source dir or compressed one - simple, transparent and you can hide your uncompressed and non-obfuscated sources far from the reach
compress files with the .min.{js,css} ending - no need for separate directory, you can hide sources in apache (mod_rewrite)
write your own small middleware that will be simulating what apache does (it is few lines to select and rewrite path, you can even have different behavior depending on DEBUG config var)
use some dynamic solution e.g Django Compressor which will compile those files on-demand
(I'm using option 4 :) )

How much of a performance boost I can get by using nginx on top of Django CMS?

The static page content comes from Django CMS which makes it dynamic , How is using nginx in such a scenario beneficial ?
It is generally considered a best practice to have "static" content served by a traditional web server such as Nginx or Apache. By static content, I am referring to things like CSS, JavaScript, and Images. Since these files often don't require modification between requests, there is little sense in having Django serve them. And Nginx/Apache can be serving these files to the client's browser concurrently.
However, your dynamic content served by Django needs to be rendered within the context of one or more Django templates. Not to mention, content such as blog posts needs to be retrieved from the database. Fortunately, Django has a robust caching system that you can use to cache the rendered HTML output and decrease server load and response times.
How much of a performance boost you gain really depends on the situation. I can tell you from experience that caching a complex response has reduced the response time from 300ms down to 30ms in one scenario.
EDIT: Adding a great article on Scaling Django: Caching and Static Content.

Django: Cache-Control: max-age. How to specify? What is cached?

I'm making a webpage and ideally I'd like the user's browser to cache all javascript, css, and images forever (or the max allowed 1 year). These items are either versioned or they do not change. However, I do not want the html to be cached.
I'm confused about Cache-Control: max-age. Do I set this for each type of resource (css, js, etc)? Or once for everything? Or for specific files? Will it cache the html?
I'm using Django. Where would I even specify it:
return render_to_response(my_template, {},context_instance=RequestContext(request))
I've noticed that my browser (I've tried several) caches images and js even though I have not specified anything. But can I depend on this always? And for how long will it be cached?
You need to set this header on individual static files. However you should not be serving static files from Django in production. Use a web server such as Nginx, Lighttpd for that, or better yet, a CDN.

serving static files on a separate server

I'm creating a website with django. There isn't much static content ( maybe 20 images, and 5-10 css/javascript docs).
I read up on Managing Static files in django. Do I need to deploy my static content on a separate server, or will it work fine since I have very little static content? currently, I'm accessing all my css files and images with the actual path name instead of using "{{STATIC_URL}}".
First you need to use {% static %} to access static files. Please see Django's official docs on this.
Answering your question: you do not need to keep your static files on a separate server but it is highly recommended. The main reason is performance. They will be served directly from HTTP server avoiding additional load on application server. Also, they will be cached by server/client.
You can find a lot of article on this topic. Also check official docs: deploying static files.
You will at least need an HTTP server running on whatever you're running your django project from, and it's highly recommended that you use a separate server for your static files apart from your app logic.
Secondly, it's very bad practice not to use {{ STATIC_URL }} or a similar item. Absolute paths are evil. If the project changes machines, or if it needs multiple versions, etc. These paths could very well change.