diff --git a/.kitchen.local.yml b/.kitchen.local.yml index 5f9cc2cf..79c513ca 100644 --- a/.kitchen.local.yml +++ b/.kitchen.local.yml @@ -39,3 +39,4 @@ suites: password: r4nd0m database: intercity_sample_app ruby_version: 2.1.2 + gzip_enabled: true diff --git a/.kitchen.yml b/.kitchen.yml index bdf36bc7..efb41142 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -33,3 +33,4 @@ suites: password: r4nd0m database: intercity_sample_app ruby_version: 2.1.2 + gzip_enabled: true diff --git a/Gemfile.lock b/Gemfile.lock index 39ee0fc5..059a9349 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,10 +87,11 @@ GEM mixlib-config (2.1.0) mixlib-log (1.6.0) mixlib-shellout (1.4.0) + multi_json (1.10.1) multipart-post (2.0.0) net-scp (1.2.1) net-ssh (>= 2.6.5) - net-ssh (2.9.1) + net-ssh (2.9.2) net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) net-ssh-multi (1.2.0) @@ -120,28 +121,29 @@ GEM faraday rest-client (1.6.7) mime-types (>= 1.16) - rspec (3.0.0) - rspec-core (~> 3.0.0) - rspec-expectations (~> 3.0.0) - rspec-mocks (~> 3.0.0) - rspec-core (3.0.4) - rspec-support (~> 3.0.0) - rspec-expectations (3.0.4) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.0.0) - rspec-its (1.0.1) - rspec-core (>= 2.99.0.beta1) - rspec-expectations (>= 2.99.0.beta1) - rspec-mocks (3.0.4) - rspec-support (~> 3.0.0) - rspec-support (3.0.4) + rspec-support (~> 3.1.0) + rspec-its (1.1.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) safe_yaml (1.0.4) - serverspec (2.0.1) - rspec (~> 3.0.0) + serverspec (2.7.1) + multi_json + rspec (~> 3.0) rspec-its - specinfra (~> 2.0) + specinfra (~> 2.10) slop (3.6.0) - specinfra (2.0.2) + specinfra (2.11.3) net-scp net-ssh systemu (2.6.4) diff --git a/nodes/sample_host.json b/nodes/sample_host.json index 1525a285..81e5d8f4 100644 --- a/nodes/sample_host.json +++ b/nodes/sample_host.json @@ -53,7 +53,8 @@ "username": "", "password": "", "database": "_" - } + }, + "gzip_enabled": false } } } diff --git a/test/integration/full-stack/serverspec/nginx_spec.rb b/test/integration/full-stack/serverspec/nginx_spec.rb index c5b4826f..525144d5 100644 --- a/test/integration/full-stack/serverspec/nginx_spec.rb +++ b/test/integration/full-stack/serverspec/nginx_spec.rb @@ -7,3 +7,13 @@ end end + +describe file("/etc/nginx/sites-available/intercity_sample_app.conf") do + it { should be_file } + + context "GZip support enabled" do + its(:content) do + should match /location ~ \^\/\(assets\)\/.*gzip_static on;/m + end + end +end diff --git a/vendor/cookbooks/rails/libraries/default.rb b/vendor/cookbooks/rails/libraries/default.rb index f60d2bfa..2934bb5d 100644 --- a/vendor/cookbooks/rails/libraries/default.rb +++ b/vendor/cookbooks/rails/libraries/default.rb @@ -16,5 +16,13 @@ def nginx_custom_configuration(app_info) empty_conf.merge(app_info["nginx_custom"] || {}) end + + # Builds a public root path on server + # + # nginx_server_root("/u/apps", "my_app") # => "/u/apps/my_app/current/public" + # + def nginx_server_root(applications_root, app) + "#{applications_root}/#{app}/current/public" + end end end diff --git a/vendor/cookbooks/rails/recipes/default.rb b/vendor/cookbooks/rails/recipes/default.rb index 8c1022a2..9b6bd482 100644 --- a/vendor/cookbooks/rails/recipes/default.rb +++ b/vendor/cookbooks/rails/recipes/default.rb @@ -118,7 +118,10 @@ domain_names: app_info["domain_names"], redirect_domain_names: app_info["redirect_domain_names"], enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"), - custom_configuration: nginx_custom_configuration(app_info)) + server_root_public_path: nginx_server_root(applications_root, app), + custom_configuration: nginx_custom_configuration(app_info), + gzip_enabled: app_info["gzip_enabled"] + ) notifies :reload, resources(service: "nginx") end diff --git a/vendor/cookbooks/rails/recipes/passenger.rb b/vendor/cookbooks/rails/recipes/passenger.rb index aab970b6..6c1ee103 100644 --- a/vendor/cookbooks/rails/recipes/passenger.rb +++ b/vendor/cookbooks/rails/recipes/passenger.rb @@ -134,7 +134,10 @@ rails_env: rails_env, domain_names: app_info["domain_names"], enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"), - custom_configuration: nginx_custom_configuration(app_info)) + server_root_public_path: nginx_server_root(applications_root, app), + custom_configuration: nginx_custom_configuration(app_info), + gzip_enabled: app_info["gzip_enabled"] + ) notifies :reload, resources(:service => "nginx") end diff --git a/vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb b/vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb new file mode 100644 index 00000000..cd5de56d --- /dev/null +++ b/vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb @@ -0,0 +1,6 @@ +location ~ ^/(assets)/ { + root <%= @server_root_public_path %>; + gzip_static on; + expires max; + add_header Cache-Control public; + } diff --git a/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb b/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb index de13df6a..002949f5 100644 --- a/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb +++ b/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb @@ -14,7 +14,7 @@ server { server { listen <%= node['nginx']['port'] || '80' %>; server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= @server_root_public_path %> try_files $uri/index.html $uri.html $uri @app; @@ -39,7 +39,7 @@ server { server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= @server_root_public_path %> location / { try_files $uri @app; diff --git a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb index bd91afb7..f0a3a884 100644 --- a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb +++ b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb @@ -1,12 +1,16 @@ + <%= @custom_configuration["before_server"] %> server { listen <%= node['nginx']['port'] || '80' %>; server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= @server_root_public_path %>; passenger_enabled on; passenger_app_env <%= @rails_env %>; + + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: @server_root_public_path } if @gzip_enabled %> + <%= @custom_configuration["server_main"] %> } @@ -23,7 +27,10 @@ server { server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= @server_root_public_path %>; + + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: @server_root_public_path } if @gzip_enabled %> + <%= @custom_configuration["ssl_main"] %> }