Sat 23 Feb 2008
Jared Kuolt has developed a StaticGenerator for Django - Cache Script. It is helpful, original and easy to use. But the script and nginx-configuration shown by Jared have some drawbacks:
1) actual only for web sites without authorization;
2) working with only simple links;
3) cache creating only by change/add content and/or comments.
I have corrected the StaticGenerator and added something interesting to the script and get StaticGenerator Pro.
Its additional features:
The StaticGenerator Pro is here to download. The file only differs in the added class ResponseStaticGenerator and some changed function delete_from_path.
May be Jared would like to put this changes to his basic version.
1) actual only for web sites without authorization;
2) working with only simple links;
3) cache creating only by change/add content and/or comments.
I have corrected the StaticGenerator and added something interesting to the script and get StaticGenerator Pro.
Its additional features:
- Caching for only anonymous
Requests of the registered users for whom site may look otherwise, always forward directly to Django without caching, that in this case makes no sense. So, the StaticGenerator Pro is primarily useful for anonymous (as a rule there are more than 99%).
Definition of the user happens not only in the script, but also in nginx (it is based on the presence of the sessionid in cookies).
Therefore I had to make changes in the script of Generator and in nginx configuration.
A part of the changed nginx configuration to distinguish anonymous from authorized users (instead of Apache backend Django works through fcgi) looks like:
http { # [here must be all standard parameters] server { server_name example.com; listen 80; root /home/mydjangoproject/www; # send everyone to django initially set $django 1; # redirect works incorrectly with question mark, # therefore a intermediate variable is essential if ($is_args = "?") { set $args_old ?$args; } if ($is_args = "") { set $args_old ""; } # it is very important, otherwise will be incorrectly # given files with the arguments in a name default_type text/html; location / { if (-f $request_filename/index.html$args_old) { set $django 0; } # determine an authorized user if ($http_cookie ~* "sessionid=([^;]+)(?:;|$)" ) { set $django 1; } # give back cache if ($django = 0) { rewrite (.*) $1/index.html$args_old break; } # forwarding to Django if ($django) { fastcgi_pass unix:/home/mydjangoproject/dj.sock; break; } index index.html; include conf/fastcgi.conf; access_log logs/project.log main; } } }Yet another pleasant thing:
it can be seen in nginx logs, whether a page was given back from Cache, or through Django. Just enough to add $django to the log format.
For example:log_format main '$remote_addr [$time_local] "$request" ' '$status $bytes_sent $body_bytes_sent $gzip_ratio ' '$django "$http_referer" "$http_user_agent"';
In logs
0 - page from cache
1 - page through fcgi from Django
- Middleware, which generates the static files at the first request, all subsequent requests will be taken from the cache.
The static files are only created if the answer is positive(code 200) and with GET-request.
To install you need to put generatorpro.py in a folder of the project
and to add the 'generatorpro.ResponseStaticGenerator' to MIDDLEWARE_CLASSES in settings.py.
Remember to set the path to the folder www.
from os import path WEB_ROOT = path.realpath("www")Like this you can place cache files in the project directory.
- Working with the URLs with the arguments like: http://www.alrond.com/?test=1
Each unique version will be cached separately.
- In settings.py it is possible to set paths, which are excluded from the caching:
STATIC_GENERATOR_EXCLUDED = ( '/comments/postfree', '/rating', '/rss', '/admin', )In that way, you exclude all paths started with /comments/postfree, /rating, /rss, /admin.
Of course, you must have in mind for your application, that sometimes unique data can be also displayed for the anonymous users, then you can just exclude these paths too.
- "Intelligent" removing from the сache
Removing of the certain path leads to the removal of all versions with arguments (of course, till the first request).
For example, removing http://www.alrond.com/en/index.html , will be also removed http://www.alrond.com/en/index.html?test=1 and http://www.alrond.com/en/index.html?tag=django&sort=desc
- The possibility of the exclusion from the cache in the controller views
Suffice to put the response['DisableStaticGenerator'] = 1 in any controller before return response
The StaticGenerator Pro is here to download. The file only differs in the added class ResponseStaticGenerator and some changed function delete_from_path.
May be Jared would like to put this changes to his basic version.
English
Deutsch
Русский

March 13th, 2008 at 9:07 a.m.
В чем смысл проверять анонимусов по куке, если она появится после первого же обращения к джанге?
March 16th, 2008 at 6:51 p.m.
Неправда, sessionid ставится только после авторизации
April 10th, 2008 at 12:06 a.m.
"""
Для установки надо разместить generatorpro.py в папку проекта
и добавить в MIDDLEWARE_CLASSES в settings.py 'generator.ResponseStaticGenerator'.
"""
наверное все-таки 'generatorpro.ResponseStaticGenerator'
April 10th, 2008 at 3:41 p.m.
да, конечно так, спасибо