Skip to content

Enable alias configuration similar to nginx::vhost #63

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 14 additions & 10 deletions manifests/resource/location.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
$auth_basic = undef,
$www_root = undef,
$create_www_root = false,
$alias_path = undef,
$owner = '',
$groupowner = '',
$redirect = undef,
Expand All @@ -47,6 +48,7 @@
$proxy_read_timeout = '90',
$proxy_set_header = ['Host $host', 'X-Real-IP $remote_addr', 'X-Forwarded-For $proxy_add_x_forwarded_for', 'X-Forwarded-Proto $scheme' ],
$proxy_redirect = undef,
$proxy_http_version = undef,
$ssl = false,
$ssl_only = false,
$option = undef,
Expand Down Expand Up @@ -99,21 +101,23 @@
}

## Check for various error condtiions
# Check vhost
if ($vhost == undef) {
fail('Cannot create a location reference without attaching to a virtual host')
}
if (($www_root == undef) and ($proxy == undef) and ($redirect == undef)) {
fail('Cannot create a location reference without a www_root, proxy or redirect defined')
# Check www_root/proxy/redirect/alias_path
if (($www_root == undef) and ($proxy == undef) and ($redirect == undef) and ($alias_path == undef)) {
fail('Cannot create a location reference without a www_root, proxy, redirect, or alias_path defined')
}
if (($www_root != undef) and ($proxy != undef)) {
fail('Cannot define both directory and proxy in a virtual host')
}
if (($www_root != undef) and ($redirect != undef)) {
fail('Cannot define both directory and redirect in a virtual host')
}
if (($proxy != undef) and ($redirect != undef)) {
fail('Cannot define both proxy and redirect in a virtual host')

$mutual_exclusive = [$www_root, $proxy, $redirect, $alias_path]

# count all values which are not nil/undef - must be 1
if (count($mutual_exclusive) != 1) {
fail("Cannot define more than one of the following values: www_root: '$www_root', redirect: '$redirect', proxy: '$proxy', and alias_path: '$alias_path'!")
}

# Check auth
if (($auth_basic_user_file != undef) and ($auth_basic == undef)) {
fail('Cannot define auth_basic_user_file without auth_basic')
}
Expand Down
20 changes: 19 additions & 1 deletion manifests/resource/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# [*ssl_cert*] - Pre-generated SSL Certificate file to reference for SSL Support. This is not generated by this module.
# [*ssl_key*] - Pre-generated SSL Key file to reference for SSL Support. This is not generated by this module.
# [*www_root*] - Specifies the location on disk for files to be read from. Cannot be set in conjunction with $proxy
# [*server_name*] - Virtual host server name.
# [*server_aliases*] - Additional virtual host names, also commonly referred to as server aliases.
#
# Actions:
#
Expand All @@ -47,6 +49,7 @@
$ipv6_listen_port = '80',
$default_server = false,
$server_name = $name,
$server_aliases = '',
$ssl = absent,
$ssl_only = false,
$ssl_cert = undef,
Expand All @@ -56,6 +59,7 @@
$proxy = undef,
$proxy_read_timeout = '90',
$proxy_set_header = undef,
$proxy_http_version = undef,
$proxy_redirect = undef,
$redirect = undef,
$index_files = ['index.html', 'index.htm', 'index.php'],
Expand All @@ -71,7 +75,9 @@
$create_www_root = false,
$owner = '',
$groupowner = '',
$fastcgi = absent
$fastcgi = absent,
$auth_basic = undef,
$auth_basic_user_file = undef
) {

File {
Expand All @@ -98,6 +104,15 @@
default => $groupowner,
}

# convert server_aliases to an array
$array_server_aliases = is_array($server_aliases) ? {
false => $server_aliases ? {
'' => [],
default => [$server_aliases],
},
default => $server_aliases,
}

$file_real = "${nginx::vdir}/${name}.conf"

# Some OS specific settings:
Expand Down Expand Up @@ -150,6 +165,7 @@
proxy_read_timeout => $proxy_read_timeout,
proxy_set_header => $proxy_set_header,
proxy_redirect => $proxy_redirect,
proxy_http_version => $proxy_http_version,
redirect => $redirect,
www_root => $www_root,
create_www_root => $create_www_root,
Expand All @@ -159,6 +175,8 @@
template_proxy => $template_proxy,
template_ssl_proxy => $template_ssl_proxy,
template_directory => $template_directory,
auth_basic => $auth_basic,
auth_basic_user_file => $auth_basic_user_file,
}

# Use the File Fragment Pattern to construct the configuration files.
Expand Down
19 changes: 17 additions & 2 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# - The $docroot provides the Documentation Root variable
# - The $template option specifies whether to use the default template or override
# - The $priority of the site
# - The $serveraliases of the site
# - The $server_aliases of the site
#
# Actions:
# - Install Nginx Virtual Hosts
Expand All @@ -27,7 +27,8 @@
$port = '80',
$template = 'nginx/vhost/vhost.conf.erb',
$priority = '50',
$serveraliases = '',
$serveraliases = undef,
$server aliases = '',
$create_docroot = true,
$enable = true,
$owner = '',
Expand All @@ -49,6 +50,20 @@

$bool_create_docroot = any2bool($create_docroot)

# $serveraliases is deprecated
if ($serveraliases != undef) {
warning('nginx: nginx::vhost serveraliases is deprecated. Please use server_aliases instead.')
}

# convert server_aliases to an array
$array_server_aliases = is_array($server_aliases) ? {
false => $server_aliases ? {
'' => [],
default => [$server_aliases],
},
default => $server_aliases,
}

file { "${nginx::vdir}/${priority}-${name}.conf":
content => template($template),
mode => $nginx::config_file_mode,
Expand Down
1 change: 1 addition & 0 deletions templates/conf.d/proxy.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ proxy_buffers <%= scope.lookupvar('nginx::params::nx_proxy_buffers') %
<% scope.lookupvar('nginx::params::nx_proxy_set_header').each do |header| %>
proxy_set_header <%= header %>;
<% end %>
<% unless @nx_proxy_http_version.nil? || @nx_proxy_http_version.empty? -%>proxy_http_version <%= scope.lookupvar('nginx::params::nx_proxy_http_version') %>;<% end %>
2 changes: 1 addition & 1 deletion templates/vhost/vhost_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server {
listen <%= @listen_ip %><% if defined? @listen_port %>:<%= @listen_port %><% end %><% if @bool_default_server %> default_server<% end %>;
<% # check to see if ipv6 support exists in the kernel before applying %>
<% if @bool_ipv6_enable && (defined? @ipaddress6) %>listen [<%= @ipv6_listen_ip %>]:<%= @ipv6_listen_port %> default ipv6only=on;<% end %>
server_name <%= @server_name %>;
server_name <%= @server_name %> <%= Array(@array_server_aliases).join(' ') %>;
access_log <%= scope.lookupvar('nginx::log_dir')%>/<%= @name %>.access.log;

<% if (defined? @www_root) %>
Expand Down
5 changes: 5 additions & 0 deletions templates/vhost/vhost_location_directory.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
location <%= @location %> {
<% unless @www_root.nil? || @www_root.empty? -%>
root <%= @www_root %>;
<% end -%>
<% unless @alias_path.nil? || @alias_path.empty? -%>
alias <%= @alias_path %>;
<% end -%>
index <% @index_files.each do |i| %> <%= i %> <% end %>;
<% unless @auth_basic.nil? || @auth_basic.empty? -%>
auth_basic <%= @auth_basic %>;
Expand Down
3 changes: 3 additions & 0 deletions templates/vhost/vhost_location_proxy.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<% unless @proxy_redirect.nil? || @proxy_redirect.empty? -%>
proxy_redirect <%= @proxy_redirect %>;
<% end -%>
<% unless @proxy_http_version.nil? || @proxy_http_version.empty? -%>
proxy_http_version <%= @proxy_http_version %>;
<% end %>
<% unless @proxy_set_header.nil? || @proxy_set_header.empty? -%>
<% @proxy_set_header.each do |header| -%>
proxy_set_header <%= header %>;
Expand Down
2 changes: 1 addition & 1 deletion templates/vhost/vhost_ssl_header.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server {
listen <%= @ssl_listen_ip %>:<%= @ssl_listen_port %><% if @bool_default_server %> default_server<% end %>;
<% if @bool_ipv6_enable && (defined? @ipaddress6) %>listen [<%= @ipv6_listen_ip %>]:<%= @ipv6_listen_port %> default ipv6only=on;<% end %>
server_name <%= @server_name %>;
server_name <%= @server_name %> <%= Array(@array_server_aliases).join(' ') %>;
access_log <%= scope.lookupvar('nginx::log_dir')%>/<%= @name %>.access.log;

ssl on;
Expand Down