From a9838b9ffa79979bcfe22ddc4873d09bd3678af9 Mon Sep 17 00:00:00 2001 From: Mike Tiurin Date: Thu, 6 Apr 2023 09:58:22 +0300 Subject: [PATCH] Add access and error logs directives when www_to_non_www or non_www_to_www is used. --- spec/defines/resource_server_spec.rb | 16 ++++++++++++++++ templates/server/server_header.erb | 22 ++++++++++++++++++++++ templates/server/server_ssl_header.erb | 23 ++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/spec/defines/resource_server_spec.rb b/spec/defines/resource_server_spec.rb index 87d03bf52..dce45ef16 100644 --- a/spec/defines/resource_server_spec.rb +++ b/spec/defines/resource_server_spec.rb @@ -81,6 +81,22 @@ describe 'server_header template content' do [ + { + title: 'should contain access and error logs directives inside the www rewrite', + attr: 'rewrite_www_to_non_www', + value: true, + match: %r{\s+return\s+301\s+http://rspec\.example\.com\$request_uri;\n + \s+access_log\s+/var/log/nginx/www.rspec.example.com.access.log;\n + \s+error_log\s+/var/log/nginx/www.rspec.example.com.error.log;\n}x + }, + { + title: 'should contain access and error logs directives inside the www rewrite', + attr: 'rewrite_non_www_to_www', + value: true, + match: %r{\s+return\s+301\s+http://rspec\.example\.com\$request_uri;\n + \s+access_log\s+/var/log/nginx/rspec.example.com.access.log;\n + \s+error_log\s+/var/log/nginx/rspec.example.com.error.log;\n}x + }, { title: 'should not contain www to non-www rewrite', attr: 'rewrite_www_to_non_www', diff --git a/templates/server/server_header.erb b/templates/server/server_header.erb index 185432f98..2b6b74ddd 100644 --- a/templates/server/server_header.erb +++ b/templates/server/server_header.erb @@ -34,6 +34,28 @@ server { return 301 http://www.<%= s %>$request_uri; <%- end -%> <%- end -%> +<% if @access_log.is_a?(Array) -%> + <%- @access_log.each do |log_item| -%> + access_log <%= log_item %><% if @format_log %> <%= @format_log%><% end %>; + <%- end -%> +<% elsif @access_log == 'absent' -%> +<% elsif @access_log == 'off' -%> + access_log off; +<% elsif not @access_log -%> + access_log <%= scope['::nginx::config::log_dir'] %>/<%= @name_sanitized %>.access.log<% if @format_log %> <%= @format_log%><% end %>; +<% else -%> + access_log <%= @access_log %><% if @format_log %> <%= @format_log%><% end %>; +<% end -%> +<% if @error_log.is_a?(Array) -%> + <%- @error_log.each do |log_item| -%> + error_log <%= log_item %>; + <%- end -%> +<% elsif @error_log == 'absent' -%> +<% elsif not @error_log -%> + error_log <%= scope['::nginx::config::log_dir'] %>/<%= @name_sanitized %>.error.log; +<% else -%> + error_log <%= @error_log %>; +<% end -%> } <% end -%> diff --git a/templates/server/server_ssl_header.erb b/templates/server/server_ssl_header.erb index 973ad9603..0c6f319ac 100644 --- a/templates/server/server_ssl_header.erb +++ b/templates/server/server_ssl_header.erb @@ -17,7 +17,28 @@ server { server_name <%= s.gsub(/^www\./, '') %>; return 301 https://www.<%= s %>$request_uri; <%- end -%> - +<% if @access_log.is_a?(Array) -%> + <%- @access_log.each do |log_item| -%> + access_log <%= log_item %><% if @format_log %> <%= @format_log%><% end %>; + <%- end -%> +<% elsif @access_log == 'absent' -%> +<% elsif @access_log == 'off' -%> + access_log off; +<% elsif not @access_log -%> + access_log <%= scope['::nginx::config::log_dir'] %>/ssl-<%= @name_sanitized %>.access.log<% if @format_log %> <%= @format_log%><% end %>; +<% else -%> + access_log <%= @access_log %><% if @format_log %> <%= @format_log%><% end %>; +<% end -%> +<% if @error_log.is_a?(Array) -%> + <%- @error_log.each do |log_item| -%> + error_log <%= log_item %>; + <%- end -%> +<% elsif @error_log == 'absent' -%> +<% elsif not @error_log -%> + error_log <%= scope['::nginx::config::log_dir'] %>/ssl-<%= @name_sanitized %>.error.log; +<% else -%> + error_log <%= @error_log %>; +<% end -%> <%= scope.function_template(["nginx/server/server_ssl_settings.erb"]) %> }