Defined Type: nginx::resource::upstream

Defined in:
manifests/resource/upstream.pp

Summary

Create a new upstream proxy entry for NGINX

Overview

Examples:

basic example with three members

nginx::resource::upstream { 'proxypass':
  ensure  => present,
  members => {
    'localhost:3001' => {
      server => 'localhost',
      port   => 3001,
    },
    'localhost:3002' => {
      server => 'localhost',
      port   => 3002,
    },
    'localhost:3003' => {
      server => 'localhost',
      port   => 3003,
    },
  },
}

Custom config example to use ip_hash, and 20 keepalive connections create a hash with any extra custom config you want.

nginx::resource::upstream { 'proxypass':
  ensure    => present,
  members   => {
    'localhost:3001' => {
      server => 'localhost',
      port   => 3001,
    },
    'localhost:3002' => {
      server => 'localhost',
      port   => 3002,
    },
    'localhost:3003' => {
      server => 'localhost',
      port   => 3003,
    },
  },
  ip_hash   => true,
  keepalive => 20,
}

Parameters:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'manifests/resource/upstream.pp', line 105

define nginx::resource::upstream (
  Enum['present', 'absent']           $ensure                 = 'present',
  Enum['http', 'stream']              $context                = 'http',
  Nginx::UpstreamMembers              $members                = {},
  Optional[String[1]]                 $members_tag            = undef,
  Nginx::UpstreamMemberDefaults       $member_defaults        = {},
  Optional[String[1]]                 $hash                   = undef,
  Boolean                             $ip_hash                = false,
  Optional[Integer[1]]                $keepalive              = undef,
  Optional[Integer[1]]                $keepalive_requests     = undef,
  Optional[Nginx::Time]               $keepalive_timeout      = undef,
  Boolean                             $least_conn             = false,
  Optional[Nginx::UpstreamLeastTime]  $least_time             = undef,
  Boolean                             $ntlm                   = false,
  Optional[Integer]                   $queue_max              = undef,
  Optional[Nginx::Time]               $queue_timeout          = undef,
  Optional[String[1]]                 $random                 = undef,
  Optional[Stdlib::Unixpath]          $statefile              = undef,
  Optional[Nginx::UpstreamSticky]     $sticky                 = undef,
  Optional[Nginx::UpstreamZone]       $zone                   = undef,
  Nginx::UpstreamCustomParameters     $cfg_append             = {},
  Nginx::UpstreamCustomParameters     $cfg_prepend            = {},
  Optional[Nginx::Time] $upstream_fail_timeout = undef, # 10s
  Optional[Integer] $upstream_max_fails        = undef, # 1
) {
  if ! defined(Class['nginx']) {
    fail('You must include the nginx base class before using any defined resources')
  }

  if $least_time {
    if $context == 'http' and ! ($least_time =~ Nginx::UpstreamLeastTimeHttp) {
      fail('The parameter "least_time" does not match the datatype "Nginx::UpstreamLeastTimeHttp"')
    }
    if $context == 'stream' and ! ($least_time =~ Nginx::UpstreamLeastTimeStream) {
      fail('The parameter "least_time" does not match the datatype "Nginx::UpstreamLeastTimeStream"')
    }
  }

  $conf_dir = $context ? {
    'stream' => "${nginx::config::conf_dir}/conf.stream.d",
    default  => "${nginx::config::conf_dir}/conf.d",
  }

  Concat {
    owner => 'root',
    group => $nginx::root_group,
    mode  => $nginx::global_mode,
  }

  concat { "${conf_dir}/${name}-upstream.conf":
    ensure  => $ensure,
    notify  => Class['nginx::service'],
    require => File[$conf_dir],
    tag     => 'nginx_config_file',
  }

  concat::fragment { "${name}_upstream_header":
    target  => "${conf_dir}/${name}-upstream.conf",
    order   => '10',
    content => epp('nginx/upstream/upstream_header.epp', {
        cfg_prepend => $cfg_prepend,
        name        => $name,
    }),
  }

  if ! empty($members) {
    $members.each |$member,$values| {
      $member_values = $member_defaults + $values + { 'upstream' => $name, 'context' => $context }

      if $context == 'stream' and $member_values['route'] {
        fail('The parameter "route" is not available for upstreams with context "stream"')
      }
      if $context == 'stream' and $member_values['state'] and $member_values['state'] == 'drain' {
        fail('The state "drain" is not available for upstreams with context "stream"')
      }

      nginx::resource::upstream::member { $member:
        * => $member_values,
      }
    }
  } else {
    # Collect exported members:
    if $members_tag {
      Nginx::Resource::Upstream::Member <<| upstream == $name and tag == $members_tag |>>
    } else {
      Nginx::Resource::Upstream::Member <<| upstream == $name |>>
    }
  }

  concat::fragment { "${name}_upstream_footer":
    target  => "${conf_dir}/${name}-upstream.conf",
    order   => '90',
    content => epp('nginx/upstream/upstream_footer.epp', {
        cfg_append         => $cfg_append,
        hash               => $hash,
        ip_hash            => $ip_hash,
        keepalive          => $keepalive,
        keepalive_requests => $keepalive_requests,
        keepalive_timeout  => $keepalive_timeout,
        least_conn         => $least_conn,
        least_time         => $least_time,
        ntlm               => $ntlm,
        queue_max          => $queue_max,
        queue_timeout      => $queue_timeout,
        random             => $random,
        statefile          => $statefile,
        sticky             => $sticky,
        zone               => $zone,
    }),
  }
}