Skip to content

Commit

Permalink
Merge pull request #1546 from m1keru/access_log_in_redirect_location
Browse files Browse the repository at this point in the history
Add access and error logs directives when `www_to_non_www` or `non_www_to_www` is used
  • Loading branch information
bastelfreak authored Jun 28, 2023
2 parents 99c8849 + 82e024c commit 86918a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/defines/resource_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@

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 not contain www to non-www rewrite',
attr: 'rewrite_www_to_non_www',
Expand Down Expand Up @@ -418,6 +426,14 @@
let(:title) { 'rspec.example.com' }

[
{
title: 'should contain access and error logs directives inside the non-www rewrite',
attr: 'rewrite_non_www_to_www',
value: true,
match: %r{\s+return\s+301\s+http://www.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 non-www to www rewrite',
attr: 'rewrite_non_www_to_www',
Expand Down Expand Up @@ -477,6 +493,14 @@
let(:title) { 'rspec.example.com' }

[
{
title: 'should contain access and error logs directives inside the non-www rewrite',
attr: 'rewrite_non_www_to_www',
value: true,
match: %r{\s+return\s+301\s+https://www.rspec\.example\.com\$request_uri;\n
\s+access_log\s+/var/log/nginx/ssl-rspec.example.com.access.log;\n
\s+error_log\s+/var/log/nginx/ssl-rspec.example.com.error.log;\n}x
},
{
title: 'should not contain non-www to www rewrite',
attr: 'rewrite_non_www_to_www',
Expand Down
22 changes: 22 additions & 0 deletions templates/server/server_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand Down
23 changes: 22 additions & 1 deletion templates/server/server_ssl_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) %>

}
Expand Down

0 comments on commit 86918a6

Please sign in to comment.