roles/nginx: Add fastcgi caching

Bypasses caching for logged in users (right now only for sessions
where the "wordpress_logged_in" cookie is set. Doubles the trans-
actions per second as measured by siege:

    $ siege -d1 -t1M -c50 https://mjanja.ch

Signed-off-by: Alan Orth <alan.orth@gmail.com>
This commit is contained in:
Alan Orth 2015-02-10 23:04:28 +03:00
parent 4ea152bf51
commit 0b90bad6a9
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9
4 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,4 @@
fastcgi_cache_key $scheme$host$request_uri;
# According to the docs, a 1m key zone can store ~8,000 keys, so 10m should
# be enough. See: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
fastcgi_cache_path /var/cache/nginx/cached/fastcgi levels=2:2 keys_zone=global:10m inactive=60m max_size=1G;

View File

@ -37,5 +37,6 @@ http {
client_max_body_size 12m; client_max_body_size 12m;
include fastcgi_cache;
include /etc/nginx/conf.d/*.conf; include /etc/nginx/conf.d/*.conf;
} }

View File

@ -15,6 +15,7 @@
copy: src={{ item }} dest=/etc/nginx/{{ item }} mode=0644 owner=root group=root copy: src={{ item }} dest=/etc/nginx/{{ item }} mode=0644 owner=root group=root
with_items: with_items:
- extra-security.conf - extra-security.conf
- fastcgi_cache
- nginx.conf - nginx.conf
notify: notify:
- reload nginx - reload nginx

View File

@ -59,7 +59,29 @@ server {
# set script path relative to document root in server block # set script path relative to document root in server block
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; include fastcgi_params;
fastcgi_cache global;
fastcgi_cache_valid any 1h;
# Allow use of stale entries if the cache is updating
fastcgi_cache_use_stale updating;
# Set X-Fastcgi-Cache header to "HIT", "MISS", "BYPASS", etc
add_header X-Fastcgi-Cache $upstream_cache_status;
# Don't cache when user shift-refreshes (Pragma: no-cache) or when a user is logged in!
fastcgi_cache_bypass $http_pragma $logged_in;
fastcgi_no_cache $http_pragma $logged_in;
} }
include extra-security.conf; include extra-security.conf;
} }
# Check if a user is logged in
# if so, set $logged_in = 1
# otherwise, set $logged_in = 0
# See: http://jeradbitner.com/2012/02/nginx-do-not-cache-logged-in-drupal-or-wordpress-users/
# See: http://syshero.org/post/50053543196/disable-nginx-cache-based-on-cookies
# See nginx bug: http://trac.nginx.org/nginx/ticket/707
map $http_cookie $logged_in {
default 0;
~wordpress_logged_in 1;
}