First sysadmin post!
I'm using Django and nginx with uwsgi for this webste. Right after the update of this site, I used PageSpeed Insights to check for the issues. It reports that my server is responding very slow(2 seconds!).
By googling around, I found that nginx has good caching support. With the use of both client cache and server cache, the rendering time of the cached pages can be reduced. Here is what I do to solve this problem:
#16m being size of cache key, 1m cache key can store 8k cache entities.
#60m being the inactivity invalidation time of cache
uwsgi_cache_path /var/www-newSite/serverCache levels=1:2 keys_zone=foo:16m inactive=60m;
server {
...
# Django media
location /media {
expires 1h;
alias /var/www-newSite/media; # your Django project's media files - amend as required
}
location /static {
expires 1h;
alias /var/www-newSite/static; # your Django project's static files - amend as required
}
location / {
uwsgi_pass unix:///path/to/django_site.sock;
include /etc/nginx/uwsgi_params;
#client cache
expires 5m; #Or whatever value you want
#server cache
uwsgi_cache foo;
uwsgi_cache_key $uri;
uwsgi_cache_valid any 1h; #Or whatever value you want
}
}
You may refer to the documentation of the nginx module ngx_http_uwsgi_module for what does the directives do.
Before caching, it takes two seconds to render a page. After caching, it only take ~20 ms.
The downside of this approach is that if the database is updated, the cache isn't invalidated immediately. It probably isn't an issue for this site. In case it's a problem for you, you may want to read a bit about Django’s cache framework.
3 years later and I'm looking for the solution of the same problem. Then I found this blogpost by googling around.
I don't regret creating this blog.