voxpupuli/puppet-nginx

repository·master·Indexed 19 days ago

https://github.com/voxpupuli/puppet-nginx

A Puppet module for managing NGINX configurations, including virtual hosts, upstreams, locations, SSL, and specialized modules like Mail and Stream. It provides defined types such as nginx::resource::server, nginx::resource::location, and nginx::resource::upstream to build modular configurations. The module supports installation via distribution repositories or official NGINX project repositories (stable, mainline, or passenger) and allows resource definition via Hiera.

Tokens
17.8K
Snippets
40
Records
78
Agent score
67%

What's inside voxpupuli-puppet-nginx

  1. Configure complex upstreams with Hash members

    master

    Upstream members must be defined as a Hash where the key is the address and the value is a Hash of configuration options (like server, port, and weight). Note that the previous Array-based syntax is no longer supported.

    nginx::resource::upstream { 'openvox_rack_app':
      members => {
        'localhost:3000' => {
          server => 'localhost',
          port   => 3000,
          weight => 1,
        },
        'localhost:3001' => {
          server => 'localhost',
          port   => 3001,
          weight => 1,
        },
        'localhost:3002' => {
          server => 'localhost',
          port   => 3002,
          weight => 2,
          },
      },
    }
    
    nginx::resource::server { 'rack.voxpupuli.org':
      proxy => 'http://openvox_rack_app',
    }
  2. Configure locations for HTTP and HTTPS servers

    master

    When using ssl => true on a server, you must specify how locations attach to the HTTP or HTTPS listener:

    • HTTP only server (ssl => false): Do not set ssl => true on locations.
    • HTTP and HTTPS server:
      • HTTP only: Set ssl => false (default) on the location.
      • Both HTTP and HTTPS: Set ssl => true and ssl_only => false (default) on the location.
      • HTTPS only: Set ssl => true and ssl_only => true on the location.
    • HTTPS only server (Single listener): Set ssl => true and ssl_only => true on the location.
  3. Configure SSL for NGINX servers

    master

    By default, nginx::resource::server creates an HTTP server. To enable HTTPS:

    1. HTTP and HTTPS (Dual Stack): Set ssl => true. This creates an HTTP server on listen_port (default 80) and an HTTPS server on ssl_port (default 443).
    2. HTTPS Only: Set ssl => true and set listen_port to the same value as ssl_port. This disables the HTTP server.
  4. Install NGINX via the Puppet module

    master

    To install the NGINX package using the default software repositories of your Linux distribution, declare the nginx class in your Puppet manifest.

    If you want to use official NGINX project repositories instead of distribution-provided ones, set manage_repo to true and specify a package_source.

    Available package_source options:

    • nginx-stable: The current production-level release.
    • nginx-mainline: The active development branch.
    • passenger: Installs Phusion Passenger and an NGINX version built with Passenger support.

    Note for CentOS/RHEL users: This module has a soft dependency on EPEL. The module will fail if EPEL is not installed, but it will not configure EPEL for you.

    # Basic installation using OS repositories
    class {'nginx': }
    
    # Installation using official NGINX repositories
    class {'nginx':
        manage_repo => true,
        package_source => 'nginx-mainline'
    }
  5. Use exported upstream members with `nginx::resource::upstream::member`

    master

    To dynamically build an upstream group across multiple servers, use the nginx::resource::upstream::member defined type. This requires storeconfigs to be enabled on your Puppetserver.

    1. On the member server: Export the resource using the @@ prefix, typically using the node's certname or IP.
    2. On the NGINX server: Declare the nginx::resource::upstream resource without specifying members; it will automatically collect the exported members.
    # On the upstream member server:
    @@nginx::resource::upstream::member { $trusted['certname']:
      ensure   => present,
      upstream => 'proxypass',
      server   => $facts['networking']['ip'],
      port     => 3000,
    }
    
    # On the NGINX server:
    nginx::resource::upstream { 'proxypass':
      ensure => present,
    }
  6. Install NGINX using the `nginx` class

    master
    To install NGINX with sensible default settings, use the include nginx declaration in your Puppet manifest. The module handles package installation (EPEL/custom for RHEL, default/custom for Debian/Ubuntu/SuSE) and service management.
    include nginx
  7. Configure NGINX with Passenger (Debian/RHEL)

    master

    To use precompiled Phusion Passenger, set package_source => 'passenger' in the nginx class. This adds the Phusion Passenger repository to your package manager. You should also use http_cfg_append to set the passenger_root.

    class { 'nginx':
      package_source  => 'passenger',
      http_cfg_append => {
        'passenger_root' => '/usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini',
      }
    }
  8. Configure NGINX location for static content and autoindex

    master

    Use the nginx::resource::location class to configure locations for serving static files.

    Key parameters:

    • www_root: The disk location for files. Cannot be used with proxy.
    • index_files: An array of index files to look for (defaults to ['index.html', 'index.htm', 'index.php']).
    • autoindex: Set to 'on' to enable directory listing.
    • autoindex_exact_size: Set to 'on' or 'off' to control whether file sizes are exact or rounded.
    • autoindex_format: Set the listing format to 'html', 'xml', 'json', or 'jsonp'.
    • autoindex_localtime: Set to 'on' or 'off' to use local time instead of UTC.
    • try_files: An array of file locations to try in order.
    nginx::resource::location {
      'location_path': {
        www_root => '/var/www/html',
        autoindex => 'on',
        index_files => ['index.html'],
      }
    }
  9. Configure Server Name and SEO Rewrites

    master

    Manage how the server identifies itself and handles domain redirects:

    • server_name: List of servernames for which this server will respond (default: [$name]).
    • rewrite_www_to_non_www: Adds rules to rewrite www.domain.com to domain.com for SEO (default: false).
    • rewrite_non_www_to_www: Adds rules to rewrite domain.com to www.domain.com for SEO (default: false).
  10. Configure NGINX location as a reverse proxy

    master

    Use the proxy parameter in nginx::resource::location to set up a reverse proxy. This can be used in conjunction with nginx::resource::upstream.

    Key parameters:

    • proxy: The proxy server(s) to connect to.
    • proxy_set_header: An array of server headers to set.
    • proxy_hide_header: An array of server headers to hide.
    • proxy_pass_header: An array of server headers to pass.
    • proxy_ignore_header: An array of server headers to ignore.
    • proxy_method: Overrides the HTTP method of the request passed to the backend.
    • proxy_http_version: Sets the proxy HTTP version.
    • proxy_connect_timeout, proxy_read_timeout, proxy_send_timeout: Override default 90s timeouts using Nginx::Time format.
    • proxy_next_upstream: Specifies cases to pass a request to the next server in the upstream.
    • proxy_next_upstream_tries: Limit on number of tries for passing to the next server.
    • proxy_next_upstream_timeout: Limit on time for passing to the next server.
    nginx::resource::location {
      '/api': {
        proxy => 'my_upstream',
        proxy_set_header => ['Host $host', 'X-Real-IP $remote_addr'],
        proxy_connect_timeout => '5s',
      }
    }
  11. Define NGINX resources via Hiera

    master

    You can manage NGINX resources using Hiera by mapping them to the following keys:

    • nginx::nginx_upstreams: Upstream configurations.
    • nginx::nginx_servers: Server/Virtual Host configurations.
    • nginx::nginx_locations: Location configurations.
    • nginx::nginx_mailhosts: Mail host configurations.
    • nginx::nginx_streamhosts: Stream (TCP/UDP) host configurations.
    nginx::nginx_upstreams:
      'puppet_rack_app':
        ensure: present
        members:
          'localhost:3000':
            server: 'localhost'
            port: 3000
    nginx::nginx_servers:
      'voxpupuli.org':
        www_root: '/var/www/voxpupuli.org'
      'rack.voxpupuli.org':
        proxy: 'http://puppet_rack_app'
    nginx::nginx_locations:
      'static':
        location: '~ "^/static/[0-9a-fA-F]{8}\/(.*)$"'
        server: voxpupuli.org
        www_root: /var/www/html
    nginx::nginx_mailhosts:
      'smtp':
        auth_http: server2.example/cgi-bin/auth
        protocol: smtp
        listen_port: 587
        ssl_port: 465
        starttls: only