Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support to access_log, error_log log_not_found per location #1471

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions manifests/resource/location.pp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@
# @param reset_timedout_connection
# Enables or disables resetting timed out connections and connections closed
# with the non-standard code 444.
# @param format_log
# Log_format to use with the defined access_log
# @param access_log
# Where to write access log (log format can be set with $format_log). This
# can be either a string or an array; in the latter case, multiple lines will
# be created. Additionally, unlike the earlier behavior, setting it to
# 'absent' in the server context will remove this directive entirely from the
# server stanza, rather than setting a default. Can also be disabled for this
# server with the string 'off'.
# @param error_log
# Where to write error log. May add additional options like error level to
# the end. May set to 'absent', in which case it will be omitted in this
# server stanza (and default to nginx.conf setting)
# @param log_not_found
# Enables or disables the logging of not found errors in error_log
#
# @example Simple example
# nginx::resource::location { 'test2.local-bob':
Expand Down Expand Up @@ -231,7 +246,7 @@
Enum['present', 'absent'] $ensure = 'present',
Boolean $internal = false,
String $location = $name,
Variant[String[1],Array[String[1],1]] $server = undef,
Variant[String[1], Array[String[1], 1]] $server = undef,
Optional[String] $www_root = undef,
Optional[String] $autoindex = undef,
Optional[Enum['on', 'off']] $autoindex_exact_size = undef,
Expand Down Expand Up @@ -297,15 +312,19 @@
Optional[String] $auth_basic_user_file = undef,
Optional[String] $auth_request = undef,
Array $rewrite_rules = [],
Integer[401,599] $priority = 500,
Integer[401, 599] $priority = 500,
Boolean $mp4 = false,
Boolean $flv = false,
Optional[String] $expires = undef,
Hash $add_header = {},
Optional[Enum['on', 'off', 'always']] $gzip_static = undef,
Optional[Enum['on', 'off']] $reset_timedout_connection = undef,
Optional[Variant[Array[String[1], 1], String[1]]] $access_log = undef,
Optional[Variant[Array[String[1], 1], String[1]]] $error_log = undef,
Optional[String[1]] $format_log = $nginx::http_format_log,
Optional[Enum['on', 'off']] $log_not_found = undef,
) {
if ! defined(Class['nginx']) {
if !defined(Class['nginx']) {
fail('You must include the nginx base class before using any defined resources')
}

Expand All @@ -331,7 +350,8 @@
# Use proxy, fastcgi or uwsgi template if $proxy is defined, otherwise use directory template.
# fastcgi_script is deprecated
if ($fastcgi_script != undef) {
warning('The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
warning(
'The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
}

# Only try to manage these files if they're the default one (as you presumably
Expand Down
65 changes: 64 additions & 1 deletion spec/defines/resource_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,70 @@
attr: 'proxy_busy_buffers_size',
value: '16k',
match: %r{\s+proxy_busy_buffers_size\s+16k;}
}
},
{
title: 'access_log undef',
attr: 'access_log',
value: :undef,
notmatch: %r{\s+access_log\s+.+;}
},
{
title: 'disabling access_log ',
attr: 'access_log',
value: 'off',
match: %r{\s+access_log\s+off;}
},
{
title: 'override access_log ',
attr: 'access_log',
value: '/var/log/nginx/specific-location.log',
match: %r{\s+access_log\s+/var/log/nginx/specific-location\.log;}
},
{
title: 'override access_log with an array',
attr: 'access_log',
value: [
'/var/log/nginx/specific-location.log',
'syslog:server=10.0.0.1'
],
match: [
%r{\s+access_log\s+/var/log/nginx/specific-location\.log;},
%r{\s+access_log\s+syslog:server=10\.0\.0\.1\s*;}
]
},
{
title: 'enabling logging errors not found',
attr: 'log_not_found',
value: 'off',
match: %r{\s+log_not_found\s+off;}
},
{
title: 'enabling logging errors not found',
attr: 'log_not_found',
value: 'on',
match: %r{\s+log_not_found\s+on;}
},
{
title: 'should set error_log',
attr: 'error_log',
value: '/path/to/error.log',
match: ' error_log /path/to/error.log;'
},
{
title: 'should allow multiple error_log directives',
attr: 'error_log',
value: ['/path/to/error.log', 'syslog:server=localhost'],
match: [
' error_log /path/to/error.log;',
' error_log syslog:server=localhost;'
]
},
{
title: 'should not include error_log in server when set to absent',
attr: 'error_log',
value: 'absent',
notmatch: 'error_log'
},
].each do |param|
context "when #{param[:attr]} is #{param[:value]}" do
let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } }
Expand Down
25 changes: 25 additions & 0 deletions templates/server/location_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,28 @@
<% if @reset_timedout_connection -%>
reset_timedout_connection <%= @reset_timedout_connection %>;
<% end -%>
<% if @log_not_found -%>
log_not_found <%= @log_not_found %>;
<% end -%>
<% if @access_log -%>
<% if @access_log.is_a?(Array) -%>
<%- @access_log.each do |log_item| -%>
access_log <%= log_item %><% unless @format_log.nil? -%> <%= @format_log %><% end -%>;
<%- end -%>
<% elsif @access_log == 'absent' -%>
<% elsif @access_log == 'off' -%>
access_log off;
<% else -%>
access_log <%= @access_log %><% unless @format_log.nil? -%> <%= @format_log %><% end -%>;
<% 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 -%>