2011-06-07 01:05:27 +02:00
# define: nginx::resource::location
#
# This definition creates a new location entry within a virtual host
#
# Parameters:
2013-08-22 21:23:16 +02:00
# [*ensure*] - Enables or disables the specified location
# (present|absent)
2014-06-23 22:31:32 +02:00
# [*internal*] - Indicates whether or not this loation can be
# used for internal requests only. Default: false
2013-08-22 21:23:16 +02:00
# [*vhost*] - Defines the default vHost for this location
# entry to include with
# [*location*] - Specifies the URI associated with this location
# entry
2013-09-05 10:01:00 +02:00
# [*location_allow*] - Array: Locations to allow connections from.
# [*location_deny*] - Array: Locations to deny connections from.
2013-08-22 21:23:16 +02:00
# [*www_root*] - Specifies the location on disk for files to be
# read from. Cannot be set in conjunction with $proxy
2013-12-16 17:36:24 +01:00
# [*autoindex*] - Set it on 'on' to activate autoindex directory
# listing. Undef by default.
2013-08-22 21:23:16 +02:00
# [*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
2014-04-02 15:06:08 +02:00
# [*proxy_redirect*] - sets the text, which must be changed in
# response-header "Location" and "Refresh" in the response of the proxied
# server.
2013-08-22 21:23:16 +02:00
# [*proxy_read_timeout*] - Override the default the proxy read timeout
# value of 90 seconds
2014-04-02 15:06:08 +02:00
# [*proxy_connect_timeout*] - Override the default the proxy connect timeout
# value of 90 seconds
# [*proxy_set_header*] - Array of vhost headers to set
2013-03-20 18:44:45 +01:00
# [*fastcgi*] - location of fastcgi (host:port)
2014-08-13 12:24:59 +02:00
# [*fastcgi_param*] - Set additional custom fastcgi_params
2013-08-22 21:23:16 +02:00
# [*fastcgi_params*] - optional alternative fastcgi_params file to use
# [*fastcgi_script*] - optional SCRIPT_FILE parameter
# [*fastcgi_split_path*] - Allows settings of fastcgi_split_path_info so
# that you can split the script_name and path_info via regex
2014-06-24 18:17:08 +02:00
# [*uwsgi*] - location of uwsgi (host:port)
# [*uwsgi_params*] - optional alternative uwsgi_params file to use
2013-08-22 21:23:16 +02:00
# [*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
2014-06-26 05:40:22 +02:00
# [*raw_prepend*] - A single string, or an array of strings to
# prepend to the location directive (after custom_cfg directives). NOTE:
# YOU are responsible for a semicolon on each line that requires one.
# [*raw_append*] - A single string, or an array of strings to
# append to the location directive (after custom_cfg directives). NOTE:
# YOU are responsible for a semicolon on each line that requires one.
2013-08-22 21:23:16 +02:00
# [*location_custom_cfg*] - Expects a hash with custom directives, cannot
# be used with other location types (proxy, fastcgi, root, or stub_status)
# [*location_cfg_prepend*] - Expects a hash with extra directives to put
# before anything else inside location (used with all other types except
# custom_cfg)
2014-01-12 20:15:14 +01:00
# [*location_custom_cfg_prepend*] - Expects a array with extra directives
# to put before anything else inside location (used with all other types
# except custom_cfg). Used for logical structures such as if.
# [*location_custom_cfg_append*] - Expects a array with extra directives
# to put before anything else inside location (used with all other types
# except custom_cfg). Used for logical structures such as if.
2013-08-22 21:23:16 +02:00
# [*location_cfg_append*] - Expects a hash with extra directives to put
# after everything else inside location (used with all other types except
# custom_cfg)
2012-11-08 19:09:49 +01:00
# [*try_files*] - An array of file locations to try
2012-10-03 01:35:52 +02:00
# [*option*] - Reserved for future use
2013-07-08 21:06:02 +02:00
# [*proxy_cache*] - This directive sets name of zone for caching.
# The same zone can be used in multiple places.
2015-05-29 23:21:34 +02:00
# [*proxy_cache_key*] - Override the default proxy_cache_key of
# $scheme$proxy_host$request_uri
# [*proxy_cache_use_stale*] - Override the default proxy_cache_use_stale value
# of off.
2013-07-08 21:06:02 +02:00
# [*proxy_cache_valid*] - This directive sets the time for caching
# different replies.
2013-10-15 22:49:33 +02:00
# [*proxy_method*] - If defined, overrides the HTTP method of the
# request to be passed to the backend.
2013-10-16 00:01:50 +02:00
# [*proxy_set_body*] - If defined, sets the body passed to the backend.
2013-07-08 21:06:02 +02:00
# [*auth_basic*] - This directive includes testing name and password
# with HTTP Basic Authentication.
# [*auth_basic_user_file*] - This directive sets the htpasswd filename for
# the authentication realm.
# [*priority*] - Location priority. Default: 500. User priority
2014-01-03 19:03:55 +01:00
# 401-499, 501-599. If the priority is higher than the default priority,
2013-07-08 21:06:02 +02:00
# the location will be defined after root, or before root.
2014-08-19 03:07:22 +02:00
# [*mp4*] - Indicates whether or not this loation can be
# used for mp4 streaming. Default: false
# [*flv*] - Indicates whether or not this loation can be
# used for flv streaming. Default: false
2013-07-08 21:06:02 +02:00
#
2011-06-07 01:05:27 +02:00
#
# Actions:
#
# Requires:
#
# Sample Usage:
# nginx::resource::location { 'test2.local-bob':
# ensure => present,
# www_root => '/var/www/bob',
# location => '/bob',
# vhost => 'test2.local',
# }
2013-03-16 10:34:07 +01:00
#
2012-10-03 01:35:52 +02:00
# 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,
# }
2014-08-11 10:04:21 +02:00
#
# Add Custom fastcgi_params
# nginx::resource::location { 'test2.local-bob':
# ensure => present,
# www_root => '/var/www/bob',
# location => '/bob',
# vhost => 'test2.local',
2014-08-13 12:24:59 +02:00
# fastcgi_param => {
2014-08-11 10:04:21 +02:00
# 'APP_ENV' => 'local',
# }
# }
2012-08-27 20:18:50 +02:00
2013-04-13 13:50:10 +02:00
define nginx : : resource : : location (
2014-04-13 04:29:20 +02:00
$ensure = present ,
2014-06-23 22:31:32 +02:00
$internal = false ,
2014-04-13 04:29:20 +02:00
$location = $name,
$vhost = undef ,
$www_root = undef ,
$autoindex = undef ,
$index_files = [
2013-04-13 13:50:10 +02:00
'index.html' ,
'index.htm' ,
'index.php' ] ,
2014-04-13 04:29:20 +02:00
$proxy = undef ,
2014-11-27 05:33:57 +01:00
$proxy_redirect = $::nginx::config::proxy_redirect,
$proxy_read_timeout = $::nginx::config::proxy_read_timeout,
$proxy_connect_timeout = $::nginx::config::proxy_connect_timeout,
$proxy_set_header = $::nginx::config::proxy_set_header,
2014-04-13 04:29:20 +02:00
$fastcgi = undef ,
2014-08-13 12:24:59 +02:00
$fastcgi_param = undef ,
2014-11-27 05:33:57 +01:00
$fastcgi_params = "${::nginx::config::conf_dir}/fastcgi_params" ,
2014-04-13 04:29:20 +02:00
$fastcgi_script = undef ,
$fastcgi_split_path = undef ,
2014-06-24 18:17:08 +02:00
$uwsgi = undef ,
$uwsgi_params = "${nginx::config::conf_dir}/uwsgi_params" ,
2014-04-13 04:29:20 +02:00
$ssl = false ,
$ssl_only = false ,
$location_alias = undef ,
$location_allow = undef ,
$location_deny = undef ,
$option = undef ,
$stub_status = undef ,
2014-06-26 05:40:22 +02:00
$raw_prepend = undef ,
$raw_append = undef ,
2014-04-13 04:29:20 +02:00
$location_custom_cfg = undef ,
$location_cfg_prepend = undef ,
$location_cfg_append = undef ,
$location_custom_cfg_prepend = undef ,
$location_custom_cfg_append = undef ,
2014-07-31 19:59:37 +02:00
$include = undef ,
2014-04-13 04:29:20 +02:00
$try_files = undef ,
$proxy_cache = false ,
2015-05-29 23:21:34 +02:00
$proxy_cache_key = undef ,
$proxy_cache_use_stale = undef ,
2014-04-13 04:29:20 +02:00
$proxy_cache_valid = false ,
$proxy_method = undef ,
$proxy_set_body = undef ,
$auth_basic = undef ,
$auth_basic_user_file = undef ,
$rewrite_rules = [ ] ,
2014-08-19 03:07:22 +02:00
$priority = 500 ,
$mp4 = false ,
$flv = false ,
2014-04-13 04:29:20 +02:00
) {
2014-07-28 20:50:06 +02:00
2014-11-27 05:33:57 +01:00
$root_group = $::nginx::config::root_group
2014-07-28 20:50:06 +02:00
2011-06-15 21:40:42 +02:00
File {
owner => 'root' ,
2014-07-28 20:50:06 +02:00
group => $root_group,
2011-06-15 21:40:42 +02:00
mode => '0644' ,
2014-11-27 05:33:57 +01:00
notify => Class [ '::nginx::service' ] ,
2011-06-15 21:40:42 +02:00
}
2013-12-16 17:36:24 +01:00
2014-04-13 04:29:20 +02:00
validate_re ( $ensure, '^(present|absent)$' ,
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'." )
2014-01-06 18:41:47 +01:00
validate_string ( $location)
if ( $vhost ! = undef ) {
validate_string ( $vhost)
}
if ( $www_root ! = undef ) {
validate_string ( $www_root)
}
if ( $autoindex ! = undef ) {
validate_string ( $autoindex)
}
2013-10-20 23:58:25 +02:00
validate_array ( $index_files)
2014-01-06 18:41:47 +01:00
if ( $proxy ! = undef ) {
validate_string ( $proxy)
}
2014-09-24 02:06:15 +02:00
if ( $proxy_redirect ! = undef ) {
validate_string ( $proxy_redirect)
}
2014-01-06 18:41:47 +01:00
validate_string ( $proxy_read_timeout)
2014-04-02 15:06:08 +02:00
validate_string ( $proxy_connect_timeout)
validate_array ( $proxy_set_header)
2014-01-06 18:41:47 +01:00
if ( $fastcgi ! = undef ) {
validate_string ( $fastcgi)
}
2014-08-13 12:24:59 +02:00
if ( $fastcgi_param ! = undef ) {
validate_hash ( $fastcgi_param)
}
2014-01-06 18:41:47 +01:00
validate_string ( $fastcgi_params)
if ( $fastcgi_script ! = undef ) {
validate_string ( $fastcgi_script)
}
if ( $fastcgi_split_path ! = undef ) {
validate_string ( $fastcgi_split_path)
}
2014-08-12 18:06:13 +02:00
if ( $uwsgi ! = undef ) {
validate_string ( $uwsgi)
2014-06-24 18:17:08 +02:00
}
2014-08-12 18:06:13 +02:00
validate_string ( $uwsgi_params)
2014-06-23 22:31:32 +02:00
validate_bool ( $internal)
2014-01-06 18:41:47 +01:00
validate_bool ( $ssl)
validate_bool ( $ssl_only)
if ( $location_alias ! = undef ) {
validate_string ( $location_alias)
}
if ( $location_allow ! = undef ) {
validate_array ( $location_allow)
}
if ( $location_deny ! = undef ) {
validate_array ( $location_deny)
}
if ( $option ! = undef ) {
warning ( 'The $option parameter has no effect and is deprecated.' )
}
if ( $stub_status ! = undef ) {
validate_bool ( $stub_status)
}
2014-06-26 05:40:22 +02:00
if ( $raw_prepend ! = undef ) {
if ( is_array ( $raw_prepend)) {
validate_array ( $raw_prepend)
} else {
validate_string ( $raw_prepend)
}
}
if ( $raw_append ! = undef ) {
if ( is_array ( $raw_append)) {
validate_array ( $raw_append)
} else {
validate_string ( $raw_append)
}
}
2014-01-06 18:41:47 +01:00
if ( $location_custom_cfg ! = undef ) {
validate_hash ( $location_custom_cfg)
}
if ( $location_cfg_prepend ! = undef ) {
validate_hash ( $location_cfg_prepend)
}
if ( $location_cfg_append ! = undef ) {
validate_hash ( $location_cfg_append)
}
2014-07-31 19:59:37 +02:00
if ( $include ! = undef ) {
validate_array ( $include)
}
2014-01-06 18:41:47 +01:00
if ( $try_files ! = undef ) {
validate_array ( $try_files)
}
if ( $proxy_cache ! = false ) {
validate_string ( $proxy_cache)
}
2015-05-29 23:21:34 +02:00
if ( $proxy_cache_key ! = undef ) {
validate_string ( $proxy_cache_key)
}
if ( $proxy_cache_use_stale ! = undef ) {
validate_string ( $proxy_cache_use_stale)
}
2014-01-06 18:41:47 +01:00
if ( $proxy_cache_valid ! = false ) {
validate_string ( $proxy_cache_valid)
}
if ( $proxy_method ! = undef ) {
validate_string ( $proxy_method)
}
if ( $proxy_set_body ! = undef ) {
validate_string ( $proxy_set_body)
}
if ( $auth_basic ! = undef ) {
validate_string ( $auth_basic)
}
if ( $auth_basic_user_file ! = undef ) {
validate_string ( $auth_basic_user_file)
}
2014-01-03 19:03:55 +01:00
if ! is_integer ( $priority) {
2014-01-08 03:09:23 +01:00
fail ( '$priority must be an integer.' )
2014-01-03 19:03:55 +01:00
}
2014-01-13 19:20:02 +01:00
validate_array ( $rewrite_rules)
2014-03-28 08:20:15 +01:00
if ( $priority < 401 ) or ( $priority > 899 ) {
fail ( '$priority must be in the range 401-899.' )
2014-01-03 19:03:55 +01:00
}
2011-06-15 21:40:42 +02:00
2013-04-13 13:50:10 +02:00
# # Shared Variables
2014-04-13 04:29:20 +02:00
$ensure_real = $ensure ? {
2011-06-15 21:40:42 +02:00
'absent' => absent ,
default => file ,
}
2014-01-12 20:15:14 +01:00
2014-04-13 04:29:20 +02:00
## Check for various error conditions
2013-07-03 02:18:14 +02:00
if ( $vhost = = undef ) {
fail ( 'Cannot create a location reference without attaching to a virtual host' )
}
2015-05-08 17:00:35 +02:00
if ( ( $www_root = = undef ) and ( $proxy = = undef ) and ( $location_alias = = undef ) and ( $stub_status = = undef ) and ( $fastcgi = = undef ) and ( $uwsgi = = undef ) and ( $location_custom_cfg = = undef ) and ( $internal = = false ) ) {
fail ( 'Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, internal, or location_custom_cfg defined' )
2013-07-03 02:18:14 +02:00
}
if ( ( $www_root ! = undef ) and ( $proxy ! = undef ) ) {
fail ( 'Cannot define both directory and proxy in a virtual host' )
}
2014-06-24 18:17:08 +02:00
# Use proxy, fastcgi or uwsgi template if $proxy is defined, otherwise use directory template.
2014-08-13 12:24:59 +02:00
# 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!' )
2014-08-11 10:04:21 +02:00
}
2013-07-03 02:18:14 +02:00
2015-04-23 15:43:25 +02:00
$vhost_sanitized = regsubst ( $vhost, ' ' , '_' , 'G' )
$config_file = "${::nginx::config::conf_dir}/sites-available/${vhost_sanitized}.conf"
$location_sanitized_tmp = regsubst ( $location, '\/' , '_' , 'G' )
$location_sanitized = regsubst ( $location_sanitized_tmp, '\\\\', ' _ ', ' G ' )
2012-12-06 08:11:29 +01:00
# Use proxy or fastcgi template if $proxy is defined, otherwise use directory template.
2011-06-15 21:40:42 +02:00
if ( $proxy ! = undef ) {
2014-06-23 23:26:20 +02:00
$content_real = template ( 'nginx/vhost/locations/proxy.erb' )
2012-09-24 17:21:35 +02:00
} elsif ( $location_alias ! = undef ) {
2014-06-23 23:26:20 +02:00
$content_real = template ( 'nginx/vhost/locations/alias.erb' )
2012-10-03 00:44:47 +02:00
} elsif ( $stub_status ! = undef ) {
2014-06-23 23:26:20 +02:00
$content_real = template ( 'nginx/vhost/locations/stub_status.erb' )
2012-12-06 08:11:29 +01:00
} elsif ( $fastcgi ! = undef ) {
2014-06-23 23:26:20 +02:00
$content_real = template ( 'nginx/vhost/locations/fastcgi.erb' )
2014-06-24 19:05:10 +02:00
} elsif ( $uwsgi ! = undef ) {
$content_real = template ( 'nginx/vhost/locations/uwsgi.erb' )
2013-07-03 08:47:49 +02:00
} elsif ( $www_root ! = undef ) {
2014-06-23 23:26:20 +02:00
$content_real = template ( 'nginx/vhost/locations/directory.erb' )
2013-07-03 08:47:49 +02:00
} else {
2014-06-23 23:26:20 +02:00
$content_real = template ( 'nginx/vhost/locations/empty.erb' )
2011-06-15 21:40:42 +02:00
}
2014-12-08 20:27:58 +01:00
if $ensure = = present and $fastcgi ! = undef and ! defined ( File [ $fastcgi_params]) {
2014-06-13 12:29:02 +02:00
file { $fastcgi_params:
2013-07-31 23:07:59 +02:00
ensure => present ,
mode => '0770' ,
content => template ( 'nginx/vhost/fastcgi_params.erb' ) ,
}
}
2015-05-08 17:00:35 +02:00
if $ensure = = present and $uwsgi ! = undef and ! defined ( File [ $uwsgi_params]) {
2014-06-24 19:05:10 +02:00
file { $uwsgi_params:
2014-06-24 18:17:08 +02:00
ensure => present ,
mode => '0770' ,
content => template ( 'nginx/vhost/uwsgi_params.erb' ) ,
}
}
2014-04-13 04:29:20 +02:00
## Create stubs for vHost File Fragment Pattern
2013-05-26 14:24:40 +02:00
if ( $ssl_only ! = true ) {
2014-04-13 04:12:19 +02:00
$tmpFile=md5("${vhost_sanitized}-${priority}-${location_sanitized}")
2014-04-13 04:29:20 +02:00
2014-07-18 04:00:16 +02:00
concat : : fragment { $tmpFile:
2013-12-01 01:52:12 +01:00
target => $config_file,
2014-06-23 23:26:20 +02:00
content => join ( [
template ( 'nginx/vhost/location_header.erb' ) ,
$content_real,
template ( 'nginx/vhost/location_footer.erb' )
] , '' ) ,
2014-12-09 18:31:55 +01:00
order => $priority,
2012-10-18 15:33:07 +02:00
}
2011-06-15 21:40:42 +02:00
}
2014-04-13 04:29:20 +02:00
## Only create SSL Specific locations if $ssl is true.
2014-06-24 08:48:16 +02:00
if ( $ssl = = true or $ssl_only = = true ) {
2013-07-08 21:06:02 +02:00
$ssl_priority = $priority + 300
2014-04-13 04:12:19 +02:00
2014-04-13 04:29:20 +02:00
$sslTmpFile=md5("${vhost_sanitized}-${ssl_priority}-${location_sanitized}-ssl")
2014-07-18 04:00:16 +02:00
concat : : fragment { $sslTmpFile:
2013-12-01 01:52:12 +01:00
target => $config_file,
2014-06-23 23:26:20 +02:00
content => join ( [
template ( 'nginx/vhost/location_header.erb' ) ,
$content_real,
template ( 'nginx/vhost/location_footer.erb' )
] , '' ) ,
2014-12-09 18:31:55 +01:00
order => $ssl_priority,
2014-04-13 04:29:20 +02:00
}
}
}