diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index 650a90d9d..772f18159 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -121,6 +121,7 @@ $proxy_set_body = undef, $auth_basic = undef, $auth_basic_user_file = undef, + $rewrite_rules = [], $priority = 500 ) { File { @@ -207,6 +208,7 @@ if !is_integer($priority) { fail('$priority must be an integer.') } + validate_array($rewrite_rules) if ($priority < 401) or ($priority > 599) { fail('$priority must be in the range 401-599.') } diff --git a/manifests/resource/vhost.pp b/manifests/resource/vhost.pp index e95f7d1f6..457d40ba7 100644 --- a/manifests/resource/vhost.pp +++ b/manifests/resource/vhost.pp @@ -172,6 +172,7 @@ $error_log = undef, $passenger_cgi_param = undef, $use_default_location = true, + $rewrite_rules = [], ) { validate_re($ensure, '^(present|absent)$', @@ -298,6 +299,7 @@ validate_hash($passenger_cgi_param) } validate_bool($use_default_location) + validate_array($rewrite_rules) # Variables $vhost_dir = "${nginx::config::nx_conf_dir}/sites-available" @@ -381,6 +383,7 @@ index_files => [], location_custom_cfg => $location_custom_cfg, notify => Class['nginx::service'], + rewrite_rules => $rewrite_rules, } } else { $root = $www_root diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index b3fb4d2d3..14610e2c3 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -97,6 +97,26 @@ ' test2 test value 2;', ], }, + { + :title => 'should contain rewrite rules', + :attr => 'rewrite_rules', + :value => [ + '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last', + '^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last', + '^/users/(.*)$ /show?user=$1? last', + ], + :match => [ + ' rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;', + ' rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;', + ' rewrite ^/users/(.*)$ /show?user=$1? last;', + ], + }, + { + :title => 'should not set rewrite_rules', + :attr => 'rewrite_rules', + :value => [], + :notmatch => /rewrite/ + }, ].each do |param| context "when #{param[:attr]} is #{param[:value]}" do let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end @@ -373,6 +393,26 @@ ' test2 test value 2;', ], }, + { + :title => 'should contain rewrite rules', + :attr => 'rewrite_rules', + :value => [ + '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last', + '^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last', + '^/users/(.*)$ /show?user=$1? last', + ], + :match => [ + ' rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;', + ' rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;', + ' rewrite ^/users/(.*)$ /show?user=$1? last;', + ], + }, + { + :title => 'should not set rewrite_rules', + :attr => 'rewrite_rules', + :value => [], + :notmatch => /rewrite/ + }, ].each do |param| context "when #{param[:attr]} is #{param[:value]}" do let :default_params do { :location => 'location', :www_root => '/var/www/root', :vhost => 'vhost1' } end diff --git a/templates/vhost/vhost_location_directory.erb b/templates/vhost/vhost_location_directory.erb index ffb56091a..74d4b6710 100644 --- a/templates/vhost/vhost_location_directory.erb +++ b/templates/vhost/vhost_location_directory.erb @@ -20,6 +20,9 @@ <% if @index_files.count > 0 -%> index <% Array(@index_files).each do |i| %> <%= i %><% end %>; <% end -%> +<% @rewrite_rules.each do |rewrite_rule| -%> + rewrite <%= rewrite_rule %>; +<% end -%> <% if defined? @auth_basic -%> auth_basic "<%= @auth_basic %>"; <% end -%> @@ -30,4 +33,3 @@ <%= key %> <%= value %>; <% end -%><% end -%> } - diff --git a/templates/vhost/vhost_location_proxy.erb b/templates/vhost/vhost_location_proxy.erb index 1b7eb4c67..441449bb8 100644 --- a/templates/vhost/vhost_location_proxy.erb +++ b/templates/vhost/vhost_location_proxy.erb @@ -14,6 +14,9 @@ <% if @proxy_set_body -%> proxy_set_body <%= @proxy_set_body %>; <% end -%> +<% @rewrite_rules.each do |rewrite_rule| -%> + rewrite <%= rewrite_rule %>; +<% end -%> <% if @location_cfg_append -%><% @location_cfg_append.sort_by {|k,v| k}.each do |key,value| -%> <%= key %> <%= value %>; <% end -%><% end -%>