added rewrite to location/proxy & vhost

With credit to https://github.com/OmarzT
This commit is contained in:
Matthew Haughton 2014-01-13 13:20:02 -05:00
commit dc3817f9ae
5 changed files with 51 additions and 1 deletions

View file

@ -121,6 +121,7 @@ define nginx::resource::location (
$proxy_set_body = undef,
$auth_basic = undef,
$auth_basic_user_file = undef,
$rewrite_rules = [],
$priority = 500
) {
File {
@ -207,6 +208,7 @@ define nginx::resource::location (
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.')
}

View file

@ -172,6 +172,7 @@ define nginx::resource::vhost (
$error_log = undef,
$passenger_cgi_param = undef,
$use_default_location = true,
$rewrite_rules = [],
) {
validate_re($ensure, '^(present|absent)$',
@ -298,6 +299,7 @@ define nginx::resource::vhost (
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 @@ define nginx::resource::vhost (
index_files => [],
location_custom_cfg => $location_custom_cfg,
notify => Class['nginx::service'],
rewrite_rules => $rewrite_rules,
}
} else {
$root = $www_root

View file

@ -97,6 +97,26 @@ describe 'nginx::resource::location' do
' 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 @@ describe 'nginx::resource::location' do
' 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

View file

@ -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 -%>
}

View file

@ -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 -%>