Merge remote-tracking branch 'jfryman/master' into merge_upstream

This commit is contained in:
Hunter Haugen 2012-11-08 10:09:49 -08:00
commit 003f415beb
19 changed files with 286 additions and 107 deletions

View file

@ -18,7 +18,7 @@ Setup a new virtual host
<pre>
node default {
class { 'mcollective': }
class { 'nginx': }
nginx::resource::vhost { 'www.puppetlabs.com':
ensure => present,
www_root => '/var/www/www.puppetlabs.com',
@ -27,10 +27,9 @@ Setup a new virtual host
</pre>
Add a Proxy Server(s)
<pre>
node default {
class { 'mcollective': }
class { 'nginx': }
nginx::resource::upstream { 'puppet_rack_app':
ensure => present,
members => [

View file

@ -13,7 +13,12 @@
# Sample Usage:
#
# This class file is not called directly
class nginx::config inherits nginx::params {
class nginx::config(
$worker_processes = $nginx::params::nx_worker_processes,
$worker_connections = $nginx::params::nx_worker_connections,
$proxy_set_header = $nginx::params::nx_proxy_set_header,
$confd_purge = $nginx::params::nx_confd_purge
) inherits nginx::params {
File {
owner => 'root',
group => 'root',
@ -27,6 +32,14 @@ class nginx::config inherits nginx::params {
file { "${nginx::params::nx_conf_dir}/conf.d":
ensure => directory,
}
if $confd_purge == true {
File["${nginx::params::nx_conf_dir}/conf.d"] {
ignore => "vhost_autogen.conf",
purge => true,
recurse => true,
}
}
file { "${nginx::config::nx_run_dir}":
ensure => directory,

View file

@ -28,20 +28,34 @@
# node default {
# include nginx
# }
class nginx {
class nginx (
$worker_processes = $nginx::params::nx_worker_processes,
$worker_connections = $nginx::params::nx_worker_connections,
$proxy_set_header = $nginx::params::nx_proxy_set_header,
$confd_purge = $nginx::params::nx_confd_purge,
$configtest_enable = $nginx::params::nx_configtest_enable,
$service_restart = $nginx::params::nx_service_restrart
) inherits nginx::params {
class { 'stdlib': }
include stdlib
class { 'nginx::package':
notify => Class['nginx::service'],
}
class { 'nginx::config':
require => Class['nginx::package'],
notify => Class['nginx::service'],
worker_processes => $worker_processes,
worker_connections => $worker_connections,
proxy_set_header => $proxy_set_header,
confd_purge => $confd_purge,
require => Class['nginx::package'],
notify => Class['nginx::service'],
}
class { 'nginx::service': }
class { 'nginx::service':
configtest_enable => $configtest_enable,
service_restart => $service_restart,
}
# Allow the end user to establish relationships to the "main" class
# and preserve the relationship to the implementation classes through

View file

@ -18,7 +18,7 @@ class nginx::package {
anchor { 'nginx::package::end': }
case $::operatingsystem {
centos,fedora,rhel,scientific: {
centos,fedora,rhel,redhat,scientific: {
class { 'nginx::package::redhat':
require => Anchor['nginx::package::begin'],
before => Anchor['nginx::package::end'],

View file

@ -15,7 +15,28 @@
# This class file is not called directly
class nginx::package::redhat {
$redhat_packages = ['nginx', 'GeoIP', 'gd', 'libXpm', 'libxslt']
if downcase($::operatingsystem) == "redhat" {
$os_type = "rhel"
} else {
$os_type = downcase($::operatingsystem)
}
if $::lsbmajdistrelease == undef {
$os_rel = regsubst($::operatingsystemrelease, '\..*$', '')
} else {
$os_rel = $::lsbmajdistrelease
}
yumrepo { "nginx-release":
baseurl => "http://nginx.org/packages/${os_type}/${os_rel}/\$basearch/",
descr => 'nginx repo',
enabled => '1',
gpgcheck => '0',
}
package { $redhat_packages:
ensure => present,
ensure => present,
require => Yumrepo['nginx-release'],
}
}

View file

@ -18,6 +18,7 @@ class nginx::params {
$nx_run_dir = '/var/nginx'
$nx_conf_dir = '/etc/nginx'
$nx_confd_purge = false
$nx_worker_processes = 1
$nx_worker_connections = 1024
$nx_multi_accept = off
@ -50,7 +51,14 @@ class nginx::params {
}
$nx_daemon_user = $::operatingsystem ? {
/(?i-mx:debian|ubuntu)/ => 'www-data',
/(?i-mx:fedora|rhel|centos|scientific|suse|opensuse)/ => 'nginx',
/(?i-mx:debian|ubuntu)/ => 'www-data',
/(?i-mx:fedora|rhel|redhat|centos|scientific|suse|opensuse)/ => 'nginx',
}
# Service restart after Nginx 0.7.53 could also be just "/path/to/nginx/bin -s HUP"
# Some init scripts do a configtest, some don't. If configtest_enable it's true
# then service restart will take $nx_service_restart value, forcing configtest.
$nx_configtest_enable = false
$nx_service_restart = "/etc/init.d/nginx configtest && /etc/init.d/nginx restart"
}

View file

@ -3,17 +3,22 @@
# This definition creates a new location entry within a virtual host
#
# Parameters:
# [*ensure*] - Enables or disables the specified location (present|absent)
# [*vhost*] - Defines the default vHost for this location entry to include with
# [*location*] - Specifies the URI associated with this location entry
# [*www_root*] - Specifies the location on disk for files to be read from. Cannot be set in conjunction with $proxy
# [*index_files*] - Default index files for NGINX to read when traversing a directory
# [*proxy*] - Proxy server(s) for a location to connect to. Accepts a single value, can be used in conjunction
# with nginx::resource::upstream
# [*proxy_read_timeout*] - Override the default the proxy read timeout value of 90 seconds
# [*ssl*] - Indicates whether to setup SSL bindings for this location.
# [*try_files*] - An array of file locations to try
# [*option*] - Reserved for future use
# [*ensure*] - Enables or disables the specified location (present|absent)
# [*vhost*] - Defines the default vHost for this location entry to include with
# [*location*] - Specifies the URI associated with this location entry
# [*www_root*] - Specifies the location on disk for files to be read from. Cannot be set in conjunction with $proxy
# [*index_files*] - Default index files for NGINX to read when traversing a directory
# [*proxy*] - Proxy server(s) for a location to connect to. Accepts a single value, can be used in conjunction
# with nginx::resource::upstream
# [*proxy_read_timeout*] - Override the default the proxy read timeout value of 90 seconds
# [*ssl*] - Indicates whether to setup SSL bindings for this location.
# [*ssl_only*] - Required if the SSL and normal vHost have the same port.
# [*location_alias*] - Path to be used as basis for serving requests for this location
# [*stub_status*] - If true it will point configure module stub_status to provide nginx stats on location
# [*location_cfg_prepend*] - It expects a hash with custom directives to put before anything else inside location
# [*location_cfg_append*] - It expects a hash with custom directives to put after everything else inside location
# [*try_files*] - An array of file locations to try
# [*option*] - Reserved for future use
#
# Actions:
#
@ -26,16 +31,37 @@
# location => '/bob',
# vhost => 'test2.local',
# }
#
# Custom config example to limit location on localhost,
# create a hash with any extra custom config you want.
# $my_config = {
# 'access_log' => 'off',
# 'allow' => '127.0.0.1',
# 'deny' => 'all'
# }
# nginx::resource::location { 'test2.local-bob':
# ensure => present,
# www_root => '/var/www/bob',
# location => '/bob',
# vhost => 'test2.local',
# location_cfg_append => $my_config,
# }
define nginx::resource::location(
$ensure = present,
$vhost = undef,
$www_root = undef,
$index_files = ['index.html', 'index.htm', 'index.php'],
$proxy = undef,
$proxy_read_timeout = $nginx::params::nx_proxy_read_timeout,
$ssl = false,
$try_files = undef,
$option = undef,
$ensure = present,
$vhost = undef,
$www_root = undef,
$index_files = ['index.html', 'index.htm', 'index.php'],
$proxy = undef,
$proxy_read_timeout = $nginx::params::nx_proxy_read_timeout,
$ssl = false,
$ssl_only = false,
$location_alias = undef,
$option = undef,
$stub_status = undef,
$location_cfg_prepend = undef,
$location_cfg_append = undef,
$try_files = undef,
$location
) {
File {
@ -54,6 +80,10 @@ define nginx::resource::location(
# Use proxy template if $proxy is defined, otherwise use directory template.
if ($proxy != undef) {
$content_real = template('nginx/vhost/vhost_location_proxy.erb')
} elsif ($location_alias != undef) {
$content_real = template('nginx/vhost/vhost_location_alias.erb')
} elsif ($stub_status != undef) {
$content_real = template('nginx/vhost/vhost_location_stub_status.erb')
} else {
$content_real = template('nginx/vhost/vhost_location_directory.erb')
}
@ -62,17 +92,19 @@ define nginx::resource::location(
if ($vhost == undef) {
fail('Cannot create a location reference without attaching to a virtual host')
}
if (($www_root == undef) and ($proxy == undef)) {
fail('Cannot create a location reference without a www_root or proxy defined')
if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef) ) {
fail('Cannot create a location reference without a www_root, proxy, location_alias or stub_status defined')
}
if (($www_root != undef) and ($proxy != undef)) {
fail('Cannot define both directory and proxy in a virtual host')
}
## Create stubs for vHost File Fragment Pattern
file {"${nginx::config::nx_temp_dir}/nginx.d/${vhost}-500-${name}":
ensure => $ensure_real,
content => $content_real,
if ($ssl_only != 'true') {
file {"${nginx::config::nx_temp_dir}/nginx.d/${vhost}-500-${name}":
ensure => $ensure_real,
content => $content_real,
}
}
## Only create SSL Specific locations if $ssl is true.

View file

@ -3,23 +3,30 @@
# This definition creates a virtual host
#
# Parameters:
# [*server_name*] - Server name (value to match in Host: header). Defaults to the resource's name.
# [*ensure*] - Enables or disables the specified vhost (present|absent)
# [*listen_ip*] - Default IP Address for NGINX to listen with this vHost on. Defaults to all interfaces (*)
# [*listen_port*] - Default IP Port for NGINX to listen with this vHost on. Defaults to TCP 80
# [*ipv6_enable*] - BOOL value to enable/disable IPv6 support (false|true). Module will check to see if IPv6
# support exists on your system before enabling.
# [*ipv6_listen_ip*] - Default IPv6 Address for NGINX to listen with this vHost on. Defaults to all interfaces (::)
# [*ipv6_listen_port*] - Default IPv6 Port for NGINX to listen with this vHost on. Defaults to TCP 80
# [*index_files*] - Default index files for NGINX to read when traversing a directory
# [*proxy*] - Proxy server(s) for the root location to connect to. Accepts a single value, can be used in
# conjunction with nginx::resource::upstream
# [*proxy_read_timeout*] - Override the default the proxy read timeout value of 90 seconds
# [*ssl*] - Indicates whether to setup SSL bindings for this vhost.
# [*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
# [*try_files*] - Specifies the locations for files to be checked as an array. Cannot be used in conjuction with $proxy.
# [*server_name*] - Server name (value to match in Host: header). Defaults to the resource's name.
# [*ensure*] - Enables or disables the specified vhost (present|absent)
# [*listen_ip*] - Default IP Address for NGINX to listen with this vHost on. Defaults to all interfaces (*)
# [*listen_port*] - Default IP Port for NGINX to listen with this vHost on. Defaults to TCP 80
# [*listen_options*] - Extra options for listen directive like 'default' to catchall. Undef by default.
# [*ipv6_enable*] - BOOL value to enable/disable IPv6 support (false|true). Module will check to see if IPv6
# support exists on your system before enabling.
# [*ipv6_listen_ip*] - Default IPv6 Address for NGINX to listen with this vHost on. Defaults to all interfaces (::)
# [*ipv6_listen_port*] - Default IPv6 Port for NGINX to listen with this vHost on. Defaults to TCP 80
# [*ipv6_listen_options*] - Extra options for listen directive like 'default' to catchall. Template will allways add ipv6only=on.
# While issue jfryman/puppet-nginx#30 is discussed, default value is 'default'.
# [*index_files*] - Default index files for NGINX to read when traversing a directory
# [*proxy*] - Proxy server(s) for the root location to connect to. Accepts a single value, can be used in
# conjunction with nginx::resource::upstream
# [*proxy_read_timeout*] - Override the default the proxy read timeout value of 90 seconds
# [*ssl*] - Indicates whether to setup SSL bindings for this vhost.
# [*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.
# [*ssl_port*] - Default IP Port for NGINX to listen with this SSL vHost on. Defaults to TCP 443
# [*server_name*] - List of vhostnames for which this vhost will respond. Default [$name].
# [*www_root*] - Specifies the location on disk for files to be read from. Cannot be set in conjunction with $proxy
# [*rewrite_www_to_non_www*] - Adds a server directive and rewrite rule to rewrite www.domain.com to domain.com in order to avoid
# duplicate content (SEO);
# [*try_files*] - Specifies the locations for files to be checked as an array. Cannot be used in conjuction with $proxy.
#
# Actions:
#
@ -34,21 +41,28 @@
# ssl_key => '/tmp/server.pem',
# }
define nginx::resource::vhost(
$server_name = $name,
$ensure = 'enable',
$listen_ip = '*',
$listen_port = '80',
$ipv6_enable = false,
$ipv6_listen_ip = '::',
$ipv6_listen_port = '80',
$ssl = false,
$ssl_cert = undef,
$ssl_key = undef,
$proxy = undef,
$proxy_read_timeout = $nginx::params::nx_proxy_read_timeout,
$index_files = ['index.html', 'index.htm', 'index.php'],
$www_root = undef,
$try_files = undef
$server_name = $name,
$ensure = 'enable',
$listen_ip = '*',
$listen_port = '80',
$listen_options = undef,
$ipv6_enable = false,
$ipv6_listen_ip = '::',
$ipv6_listen_port = '80',
$ipv6_listen_options = 'default',
$ssl = false,
$ssl_cert = undef,
$ssl_key = undef,
$ssl_port = '443',
$proxy = undef,
$proxy_read_timeout = $nginx::params::nx_proxy_read_timeout,
$index_files = ['index.html', 'index.htm', 'index.php'],
$server_name = [$name],
$www_root = undef,
$rewrite_www_to_non_www = false,
$location_cfg_prepend = undef,
$location_cfg_append = undef,
$try_files = undef
) {
File {
@ -72,36 +86,56 @@ define nginx::resource::vhost(
# Use the File Fragment Pattern to construct the configuration files.
# Create the base configuration file reference.
file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-001":
ensure => $ensure ? {
'absent' => absent,
default => 'file',
},
content => template('nginx/vhost/vhost_header.erb'),
notify => Class['nginx::service'],
if ($listen_port != $ssl_port) {
file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-001":
ensure => $ensure ? {
'absent' => absent,
default => 'file',
},
content => template('nginx/vhost/vhost_header.erb'),
notify => Class['nginx::service'],
}
}
if ($ssl == 'true') and ($ssl_port == $listen_port) {
$ssl_only = 'true'
}
# Create the default location reference for the vHost
nginx::resource::location {"${name}-default":
ensure => $ensure,
vhost => $name,
ssl => $ssl,
location => '/',
proxy_read_timeout => $proxy_read_timeout,
proxy => $proxy,
try_files => $try_files,
www_root => $www_root,
notify => Class['nginx::service'],
ensure => $ensure,
vhost => $name,
ssl => $ssl,
ssl_only => $ssl_only,
location => '/',
proxy => $proxy,
proxy_read_timeout => $proxy_read_timeout,
try_files => $try_files,
www_root => $www_root,
notify => Class['nginx::service'],
}
# Support location_cfg_prepend and location_cfg_append on default location created by vhost
if $location_cfg_prepend {
Nginx::Resource::Location["${name}-default"] {
location_cfg_prepend => $location_cfg_prepend
}
}
if $location_cfg_append {
Nginx::Resource::Location["${name}-default"] {
location_cfg_append => $location_cfg_append
}
}
# Create a proper file close stub.
file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-699":
ensure => $ensure ? {
'absent' => absent,
default => 'file',
},
content => template('nginx/vhost/vhost_footer.erb'),
notify => Class['nginx::service'],
if ($listen_port != $ssl_port) {
file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-699":
ensure => $ensure ? {
'absent' => absent,
default => 'file',
},
content => template('nginx/vhost/vhost_footer.erb'),
notify => Class['nginx::service'],
}
}
# Create SSL File Stubs if SSL is enabled

View file

@ -13,10 +13,14 @@
# Sample Usage:
#
# This class file is not called directly
class nginx::service {
class nginx::service(
$configtest_enable = $nginx::params::nx_configtest_enable,
$service_restart = $nginx::params::nx_service_restart
) {
exec { 'rebuild-nginx-vhosts':
command => "/bin/cat ${nginx::params::nx_temp_dir}/nginx.d/* > ${nginx::params::nx_conf_dir}/conf.d/vhost_autogen.conf",
refreshonly => true,
unless => "/usr/bin/test ! -f ${nginx::params::nx_temp_dir}/nginx.d/*",
subscribe => File["${nginx::params::nx_temp_dir}/nginx.d"],
}
service { "nginx":
@ -26,4 +30,9 @@ class nginx::service {
hasrestart => true,
subscribe => Exec['rebuild-nginx-vhosts'],
}
if $configtest_enable == true {
Service["nginx"] {
restart => $service_restart,
}
}
}

View file

@ -1,12 +1,12 @@
user <%= scope.lookupvar('nginx::config::nx_daemon_user') %>;
worker_processes <%= scope.lookupvar('nginx::params::nx_worker_processes')%>;
worker_processes <%= worker_processes %>;
error_log <%= scope.lookupvar('nginx::params::nx_logdir')%>/error.log;
pid <%= scope.lookupvar('nginx::params::nx_pid')%>;
events {
worker_connections <%= scope.lookupvar('nginx::params::nx_worker_connections') %>;
<% if scope.lookupvar('nginx::params::nx_multi_accept' == 'on') %>multi_accept on;<% end %>
worker_connections <%= worker_connections %>;
<% if scope.lookupvar('nginx::params::nx_multi_accept') == 'on' %>multi_accept on;<% end %>
}
http {
@ -17,17 +17,17 @@ http {
sendfile <%= scope.lookupvar('nginx::params::nx_sendfile')%>;
<% if scope.lookupvar('nginx::params::nx_tcp_nopush' == 'on') %>
<% if scope.lookupvar('nginx::params::nx_tcp_nopush') == 'on' %>
tcp_nopush on;
<% end %>
keepalive_timeout <%= scope.lookupvar('nginx::params::nx_keepalive_timeout')%>;
tcp_nodelay <%= scope.lookupvar('nginx::params::nx_tcp_nodelay')%>;
<% if scope.lookupvar('nginx::params::nx_gzip' == 'on') %>
<% if scope.lookupvar('nginx::params::nx_gzip') == 'on' %>
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
<% end %>
include /etc/nginx/conf.d/*.conf;
}
}

View file

@ -5,6 +5,6 @@ proxy_connect_timeout <%= scope.lookupvar('nginx::params::nx_proxy_connect_tim
proxy_send_timeout <%= scope.lookupvar('nginx::params::nx_proxy_send_timeout') %>;
proxy_read_timeout <%= scope.lookupvar('nginx::params::nx_proxy_read_timeout') %>;
proxy_buffers <%= scope.lookupvar('nginx::params::nx_proxy_buffers') %>;
<% scope.lookupvar('nginx::params::nx_proxy_set_header').each do |header| %>
<% proxy_set_header.each do |header| %>
proxy_set_header <%= header %>;
<% end %>

View file

@ -1 +1,9 @@
}
<% if rewrite_www_to_non_www %>
server {
listen <%= listen_ip %>;
server_name www.<%= name.gsub(/^www\./, '') %>;
rewrite ^ http://<%= name.gsub(/^www\./, '') %>$uri permanent;
}
<% end %>

View file

@ -1,6 +1,8 @@
server {
listen <%= listen_ip %>:<%= listen_port %>;
listen <%= listen_ip %>:<%= listen_port %> <% if @listen_options %><%= listen_options %><% end %>;
<% # check to see if ipv6 support exists in the kernel before applying %>
<% if ipv6_enable == 'true' && (defined? ipaddress6) %>listen [<%= ipv6_listen_ip %>]:<%= ipv6_listen_port %> default ipv6only=on;<% end %>
server_name <%= server_name %>;
access_log <%= scope.lookupvar('nginx::params::nx_logdir')%>/<%= name %>.access.log;
<% if ipv6_enable == 'true' && (defined? ipaddress6) %>
listen [<%= ipv6_listen_ip %>]:<%= ipv6_listen_port %> <% if @ipv6_listen_options %><%= ipv6_listen_options %><% end %> ipv6only=on;
<% end %>
server_name <%= rewrite_www_to_non_www ? name.gsub(/^www\./, '') : server_name.join(" ") %>;
access_log <%= scope.lookupvar('nginx::params::nx_logdir')%>/<%= name %>.access.log;

View file

@ -0,0 +1,9 @@
location <%= location %> {
<% if @location_cfg_prepend -%><% location_cfg_prepend.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
alias <%= location_alias %>;
<% if @location_cfg_append -%><% location_cfg_append.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
}

View file

@ -1,8 +1,16 @@
location <%= location %> {
<% if @location_cfg_prepend -%><% location_cfg_prepend.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
root <%= www_root %>;
index <% index_files.each do |i| %> <%= i %> <% end %>;
<% if has_variable?("try_files") then %>
try_files <% try_files.each do |try| -%> <%= try %> <% end -%>;
<% end %>
index <% index_files.each do |i| %> <%= i %> <% end %>;
<% if @location_cfg_append -%><% location_cfg_append.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
}

View file

@ -1,5 +1,10 @@
location <%= location %> {
<% if @location_cfg_prepend -%><% location_cfg_prepend.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
proxy_pass <%= proxy %>;
proxy_read_timeout <%= proxy_read_timeout %>;
<% if @location_cfg_append -%><% location_cfg_append.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
}

View file

@ -0,0 +1,9 @@
location <%= location %> {
<% if @location_cfg_prepend -%><% location_cfg_prepend.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
stub_status on;
<% if @location_cfg_append -%><% location_cfg_append.each do |key,value| -%>
<%= key %> <%= value %>;
<% end -%><% end -%>
}

View file

@ -1,5 +1,5 @@
server {
listen 443;
listen <%= ssl_port %>;
<% if ipv6_enable == 'true' && (defined? ipaddress6) %>listen [<%= ipv6_listen_ip %>]:<%= ipv6_listen_port %> default ipv6only=on;<% end %>
server_name <%= server_name %>;

8
tests/location_alias.pp Normal file
View file

@ -0,0 +1,8 @@
include nginx
nginx::resource::location { 'www.test.com-alias':
ensure => present,
location => '/some/url',
location_alias => '/new/url/',
vhost => 'www.test.com',
}