From aebc9980eafb3e523e36023022186a5da64ca5ac Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Sun, 29 May 2016 18:34:08 +0100 Subject: [PATCH] Add nginx caching support + enable for imagery --- .../nginx_imagery_layer_fragment.conf.erb | 1 + cookbooks/nginx/attributes/default.rb | 12 +++++++++++- cookbooks/nginx/recipes/default.rb | 16 ++++++++++++++++ cookbooks/nginx/templates/default/nginx.conf.erb | 7 +++++++ roles/imagery.rb | 7 +++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cookbooks/imagery/templates/default/nginx_imagery_layer_fragment.conf.erb b/cookbooks/imagery/templates/default/nginx_imagery_layer_fragment.conf.erb index 2e75717b0..0f5d2b9a3 100644 --- a/cookbooks/imagery/templates/default/nginx_imagery_layer_fragment.conf.erb +++ b/cookbooks/imagery/templates/default/nginx_imagery_layer_fragment.conf.erb @@ -5,6 +5,7 @@ location ~* "^/layer/<%= @layer %>/(\d+)/(\d+)/(\d+)\.(png|jpg|jpeg)$" { fastcgi_param QUERY_STRING "map=/srv/imagery/mapserver/layer-<%= @layer %>.map&mode=tile&layers=<%= @layer %>&tilemode=gmap&tile=$2+$3+$1"; fastcgi_pass "unix:/var/run/mapserver-fastcgi/layer-<%= @layer %>.socket"; fastcgi_buffers 8 64k; + fastcgi_cache "fastcgi_cache_zone" include fastcgi_params; gzip off; diff --git a/cookbooks/nginx/attributes/default.rb b/cookbooks/nginx/attributes/default.rb index f28efd253..4e22759bf 100644 --- a/cookbooks/nginx/attributes/default.rb +++ b/cookbooks/nginx/attributes/default.rb @@ -1 +1,11 @@ -# default[:nginx][:mpm] = "worker" +# Tuning for nginx fastcgi cache zone +default[:nginx][:cache][:fastcgi][:enable] = false +default[:nginx][:cache][:fastcgi][:keys_zone] = "fastcgi_cache_zone:32m" +default[:nginx][:cache][:fastcgi][:inactive] = "30d" +default[:nginx][:cache][:fastcgi][:max_size] = "2048m" + +# Tuning for nginx proxy cache zone +default[:nginx][:cache][:proxy][:enable] = false +default[:nginx][:cache][:proxy][:keys_zone] = "proxy_cache_zone:32m" +default[:nginx][:cache][:proxy][:inactive] = "30d" +default[:nginx][:cache][:proxy][:max_size] = "2048m" diff --git a/cookbooks/nginx/recipes/default.rb b/cookbooks/nginx/recipes/default.rb index 2c8b00a46..a5d06e1c0 100644 --- a/cookbooks/nginx/recipes/default.rb +++ b/cookbooks/nginx/recipes/default.rb @@ -28,6 +28,22 @@ template "/etc/nginx/nginx.conf" do mode 0644 end +if node['nginx']['cache']['fastcgi']['enable'] + directory "/var/lib/nginx/fastcgi-cache" do + owner "nginx" + group "nginx" + mode 0755 + end +end + +if node['nginx']['cache']['proxy']['enable'] + directory "/var/lib/nginx/proxy-cache" do + owner "nginx" + group "nginx" + mode 0755 + end +end + service "nginx" do action [:enable] supports :status => true, :restart => true, :reload => true diff --git a/cookbooks/nginx/templates/default/nginx.conf.erb b/cookbooks/nginx/templates/default/nginx.conf.erb index 13597d9fa..2099e8354 100644 --- a/cookbooks/nginx/templates/default/nginx.conf.erb +++ b/cookbooks/nginx/templates/default/nginx.conf.erb @@ -28,5 +28,12 @@ http { server_tokens off; + <% if node['nginx']['cache']['fastcgi']['enable'] -%> + fastcgi_cache_path /var/lib/nginx/fastcgi-cache levels=1:2 keys_zone=<%= node['nginx']['cache']['fastcgi']['keys_zone'] %> inactive=<%= node['nginx']['cache']['fastcgi']['inactive'] %> max_size=<%= node['nginx']['cache']['fastcgi']['max_size'] %>; + <% end -%> + <% if node['nginx']['cache']['proxy']['enable'] -%> + fastcgi_cache_path /var/lib/nginx/proxy-cache levels=1:2 keys_zone=<%= node['nginx']['cache']['proxy']['keys_zone'] %> inactive=<%= node['nginx']['cache']['proxy']['inactive'] %> max_size=<%= node['nginx']['cache']['proxy']['max_size'] %>; + <% end -%> + include /etc/nginx/conf.d/*.conf; } diff --git a/roles/imagery.rb b/roles/imagery.rb index 60bb1d930..9106f9329 100644 --- a/roles/imagery.rb +++ b/roles/imagery.rb @@ -28,6 +28,13 @@ default_attributes( "kernel.sched_wakeup_granularity_ns" => 15000000 } } + }, + :nginx => { + :cache => { + :fastcgi => { + :enable => true + } + } } ) -- 2.39.5