Defined Type: nginx::resource::upstream

Defined in:
manifests/resource/upstream.pp

Overview

define: nginx::resource::upstream

This definition creates a new upstream proxy entry for NGINX

Parameters:

[*members*]               - Array of member URIs for NGINX to connect to. Must follow valid NGINX syntax.
                            If omitted, individual members should be defined with nginx::resource::upstream::member
[*ensure*]                - Enables or disables the specified location (present|absent)
[*upstream_cfg_prepend*]  - It expects a hash with custom directives to put before anything else inside upstream
[*upstream_fail_timeout*] - Set the fail_timeout for the upstream. Default is 10 seconds - As that is what Nginx does normally.
[*upstream_max_fails*]    - Set the max_fails for the upstream. Default is to use nginx default value which is 1.

Actions:

Requires:

Sample Usage:

nginx::resource::upstream { 'proxypass':
  ensure  => present,
  members => [
    'localhost:3000',
    'localhost:3001',
    'localhost:3002',
  ],
}

Custom config example to use ip_hash, and 20 keepalive connections
create a hash with any extra custom config you want.
$my_config = {
  'ip_hash'   => '',
  'keepalive' => '20',
}
nginx::resource::upstream { 'proxypass':
  ensure              => present,
  members => [
    'localhost:3000',
    'localhost:3001',
    'localhost:3002',
  ],
  upstream_cfg_prepend => $my_config,
}

Parameters:

  • members (Any) (defaults to: undef)
  • ensure (Any) (defaults to: 'present')
  • upstream_cfg_prepend (Any) (defaults to: undef)
  • upstream_fail_timeout (Any) (defaults to: '10s')
  • upstream_max_fails (Any) (defaults to: undef)
  • upstream_context (Any) (defaults to: 'http')


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'manifests/resource/upstream.pp', line 42

define nginx::resource::upstream (
  $members = undef,
  $ensure = 'present',
  $upstream_cfg_prepend = undef,
  $upstream_fail_timeout = '10s',
  $upstream_max_fails = undef,
  $upstream_context = 'http',
) {

  if $members != undef {
    validate_array($members)
  }
  validate_re($ensure, '^(present|absent)$',
    "${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
  validate_re($upstream_context, '^(http|stream)$',
      "${upstream_context} is not supported for upstream_context. Allowed values are 'http' and 'stream'.")
  if ($upstream_cfg_prepend != undef) {
    validate_hash($upstream_cfg_prepend)
  }

  $root_group = $::nginx::config::root_group

  $ensure_real = $ensure ? {
    'absent' => absent,
    default  => present,
  }

  $conf_dir_real = $upstream_context ? {
    'stream' => 'conf.stream.d',
    default  => 'conf.d',
  }

  Concat {
    owner => 'root',
    group => $root_group,
    mode  => '0644',
  }

  concat { "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf":
    ensure => $ensure_real,
    notify => Class['::nginx::service'],
  }

  # Uses: $name, $upstream_cfg_prepend
  concat::fragment { "${name}_upstream_header":
    target  => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
    order   => '10',
    content => template('nginx/conf.d/upstream_header.erb'),
  }

  if $members != undef {
    # Uses: $members, $upstream_fail_timeout
    concat::fragment { "${name}_upstream_members":
      target  => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
      order   => '50',
      content => template('nginx/conf.d/upstream_members.erb'),
    }
  } else {
    # Collect exported members:
    ::Nginx::Resource::Upstream::Member <<| upstream == $name |>>
  }

  concat::fragment { "${name}_upstream_footer":
    target  => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
    order   => '90',
    content => "}\n",
  }
}