diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index 4d7e245c3..1ee255bac 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -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': @@ -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, @@ -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') } @@ -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 diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 369856e9e..8fa803a54 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -236,6 +236,69 @@ attr: 'reset_timedout_connection', value: 'on', match: %r{^\s+reset_timedout_connection\s+on;} + }, + { + 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 diff --git a/templates/server/location_header.erb b/templates/server/location_header.erb index 7e526d11f..cc978ead7 100644 --- a/templates/server/location_header.erb +++ b/templates/server/location_header.erb @@ -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 -%>