2011-06-07 01:05:27 +02:00
# NGINX Module
2014-09-23 17:27:21 +02:00
## INSTALLING OR UPGRADING
** Please note ** : This module is currently undergoing some structural
2014-09-23 17:35:59 +02:00
maintenance. Please take a look at [https://github.com/jfryman/puppet-nginx/blob/master/docs/hiera.md ](https://github.com/jfryman/puppet-nginx/blob/master/docs/hiera.md )
2014-09-23 17:27:21 +02:00
before upgrading or installing Version 0.1.0 or greater.
2015-04-07 20:38:16 +02:00
[![Puppet
Forge](http://img.shields.io/puppetforge/v/jfryman/nginx.svg)](https://forge.puppetlabs.com/jfryman/nginx)
2013-12-20 16:06:23 +01:00
[![Build Status ](https://travis-ci.org/jfryman/puppet-nginx.png )](https://travis-ci.org/jfryman/puppet-nginx)
2014-09-23 17:27:21 +02:00
2014-08-22 17:04:42 +02:00
* James Fryman < james @ frymanet . com >
* Matthew Haughton < matt @ 3flex . com . au >
2011-06-07 01:05:27 +02:00
2013-08-26 07:17:54 +02:00
This module manages NGINX configuration.
2014-03-17 21:49:28 +01:00
### Requirements
* Puppet-2.7.0 or later
2014-12-08 03:02:33 +01:00
* Facter 1.7.0 or later
2014-05-15 23:58:44 +02:00
* Ruby-1.9.3 or later (Support for Ruby-1.8.7 is not guaranteed. YMMV).
2014-03-17 21:49:28 +01:00
2015-10-10 21:29:32 +02:00
### Additional Documentation
2015-10-10 21:33:11 +02:00
* [A Quickstart Guide to the NGINX Puppet Module][quickstart]
[quickstart]: https://github.com/jfryman/puppet-nginx/blob/master/docs/quickstart.md
2015-10-10 21:29:32 +02:00
2013-08-26 07:17:54 +02:00
### Install and bootstrap an NGINX instance
```puppet
class { 'nginx': }
```
2016-01-28 04:17:49 +01:00
### A simple reverse proxy
```puppet
nginx::resource::vhost { 'kibana.myhost.com':
listen_port => 80,
proxy => 'http://localhost:5601',
}
```
### A virtual host with static content
2013-08-26 07:17:54 +02:00
```puppet
nginx::resource::vhost { 'www.puppetlabs.com':
www_root => '/var/www/www.puppetlabs.com',
}
```
2016-01-28 04:17:49 +01:00
### A more complex proxy example
2013-08-26 07:17:54 +02:00
```puppet
nginx::resource::upstream { 'puppet_rack_app':
2014-01-09 22:37:47 +01:00
members => [
'localhost:3000',
'localhost:3001',
'localhost:3002',
],
2013-08-26 07:17:54 +02:00
}
nginx::resource::vhost { 'rack.puppetlabs.com':
2014-01-09 22:37:47 +01:00
proxy => 'http://puppet_rack_app',
2013-08-26 07:17:54 +02:00
}
```
### Add a smtp proxy
```puppet
class { 'nginx':
2014-01-09 22:37:47 +01:00
mail => true,
2013-08-26 07:17:54 +02:00
}
nginx::resource::mailhost { 'domain1.example':
2014-01-09 22:37:47 +01:00
auth_http => 'server2.example/cgi-bin/auth',
protocol => 'smtp',
listen_port => 587,
ssl_port => 465,
starttls => 'only',
xclient => 'off',
ssl => true,
ssl_cert => '/tmp/server.crt',
ssl_key => '/tmp/server.pem',
2013-08-26 07:17:54 +02:00
}
```
2014-01-09 22:33:44 +01:00
## SSL configuration
By default, creating a vhost resource will only create a HTTP vhost. To also create a HTTPS (SSL-enabled) vhost, set `ssl => true` on the vhost. You will have a HTTP server listening on `listen_port` (port `80` by default) and a HTTPS server listening on `ssl_port` (port `443` by default). Both vhosts will have the same `server_name` and a similar configuration.
To create only a HTTPS vhost, set `ssl => true` and also set `listen_port` to the same value as `ssl_port` . Setting these to the same value disables the HTTP vhost. The resulting vhost will be listening on `ssl_port` .
### Locations
Locations require specific settings depending on whether they should be included in the HTTP, HTTPS or both vhosts.
#### HTTP only vhost (default)
2014-05-15 23:58:44 +02:00
If you only have a HTTP vhost (i.e. `ssl => false` on the vhost) make sure you don't set `ssl => true` on any location you associate with the vhost.
2014-01-09 22:33:44 +01:00
#### HTTP and HTTPS vhost
If you set `ssl => true` and also set `listen_port` and `ssl_port` to different values on the vhost you will need to be specific with the location settings since you will have a HTTP vhost listening on `listen_port` and a HTTPS vhost listening on `ssl_port` :
* To add a location to only the HTTP server, set `ssl => false` on the location (this is the default).
* To add a location to both the HTTP and HTTPS server, set `ssl => true` on the location, and ensure `ssl_only => false` (which is the default value for `ssl_only` ).
* To add a location only to the HTTPS server, set both `ssl => true` and `ssl_only => true` on the location.
#### HTTPS only vhost
If you have set `ssl => true` and also set `listen_port` and `ssl_port` to the same value on the vhost, you will have a single HTTPS vhost listening on `ssl_port` . To add a location to this vhost set `ssl => true` and `ssl_only => true` on the location.
2013-08-26 07:17:54 +02:00
## Hiera Support
Defining nginx resources in Hiera.
```yaml
2013-07-19 00:10:12 +02:00
nginx::nginx_upstreams:
'puppet_rack_app':
ensure: present
members:
- localhost:3000
- localhost:3001
2013-08-26 07:17:54 +02:00
- localhost:3002
2013-07-19 00:10:12 +02:00
nginx::nginx_vhosts:
'www.puppetlabs.com':
www_root: '/var/www/www.puppetlabs.com'
'rack.puppetlabs.com':
proxy: 'http://puppet_rack_app'
nginx::nginx_locations:
'static':
location: '~ "^/static/[0-9a-fA-F]{8}\/(.*)$"'
vhost: www.puppetlabs.com
2015-02-02 19:59:19 +01:00
www_root: /var/www/html
2013-07-19 00:10:12 +02:00
'userContent':
location: /userContent
vhost: www.puppetlabs.com
www_root: /var/www/html
2014-04-07 22:54:33 +02:00
nginx::nginx_mailhosts:
'smtp':
auth_http: server2.example/cgi-bin/auth
protocol: smtp
listen_port: 587
ssl_port: 465
starttls: only
2013-08-26 07:17:54 +02:00
```
2013-10-08 10:20:57 +02:00
## Nginx with precompiled Passenger
2015-01-23 15:11:42 +01:00
Currently this works only for Debian family and OpenBSD.
2013-10-08 10:20:57 +02:00
2015-01-23 15:11:42 +01:00
On Debian it might look like:
2014-01-09 22:37:47 +01:00
```puppet
class { 'nginx':
package_source => 'passenger',
http_cfg_append => {
'passenger_root' => '/usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini',
}
}
```
2013-10-08 10:20:57 +02:00
2015-01-23 15:11:42 +01:00
Here the example for OpenBSD:
```puppet
class { 'nginx':
package_flavor => 'passenger',
service_flags => '-u'
http_cfg_append => {
passenger_root => '/usr/local/lib/ruby/gems/2.1/gems/passenger-4.0.44',
passenger_ruby => '/usr/local/bin/ruby21',
passenger_max_pool_size => '15',
}
}
```
2013-10-08 10:20:57 +02:00
Package source `passenger` will add [Phusion Passenger repository ](https://oss-binaries.phusionpassenger.com/apt/passenger ) to APT sources.
For each virtual host you should specify which ruby should be used.
2014-01-09 22:37:47 +01:00
```puppet
nginx::resource::vhost { 'www.puppetlabs.com':
www_root => '/var/www/www.puppetlabs.com',
vhost_cfg_append => {
'passenger_enabled' => 'on',
'passenger_ruby' => '/usr/bin/ruby',
}
}
```
2013-10-08 10:20:57 +02:00
### Puppet master served by Nginx and Passenger
Virtual host config for serving puppet master:
2014-01-09 22:37:47 +01:00
```puppet
nginx::resource::vhost { 'puppet':
ensure => present,
server_name => ['puppet'],
listen_port => 8140,
ssl => true,
ssl_cert => '/var/lib/puppet/ssl/certs/example.com.pem',
ssl_key => '/var/lib/puppet/ssl/private_keys/example.com.pem',
ssl_port => 8140,
vhost_cfg_append => {
'passenger_enabled' => 'on',
'passenger_ruby' => '/usr/bin/ruby',
'ssl_crl' => '/var/lib/puppet/ssl/ca/ca_crl.pem',
'ssl_client_certificate' => '/var/lib/puppet/ssl/certs/ca.pem',
'ssl_verify_client' => 'optional',
'ssl_verify_depth' => 1,
},
www_root => '/etc/puppet/rack/public',
use_default_location => false,
access_log => '/var/log/nginx/puppet_access.log',
error_log => '/var/log/nginx/puppet_error.log',
passenger_cgi_param => {
'HTTP_X_CLIENT_DN' => '$ssl_client_s_dn',
'HTTP_X_CLIENT_VERIFY' => '$ssl_client_verify',
},
}
```
2014-01-12 20:15:14 +01:00
### Example puppet class calling nginx::vhost with HTTPS FastCGI and redirection of HTTP
2014-01-17 13:49:28 +01:00
```puppet
$full_web_path = '/var/www'
define web::nginx_ssl_with_redirect (
2014-01-12 20:15:14 +01:00
$backend_port = 9000,
$php = true,
$proxy = undef,
$www_root = "${full_web_path}/${name}/",
$location_cfg_append = undef,
) {
nginx::resource::vhost { "${name}.${::domain}":
ensure => present,
www_root => "${full_web_path}/${name}/",
location_cfg_append => { 'rewrite' => '^ https://$server_name$request_uri? permanent' },
}
if !$www_root {
$tmp_www_root = undef
} else {
$tmp_www_root = $www_root
}
2014-05-15 23:58:44 +02:00
2014-01-12 20:15:14 +01:00
nginx::resource::vhost { "${name}.${::domain} ${name}":
ensure => present,
listen_port => 443,
www_root => $tmp_www_root,
proxy => $proxy,
location_cfg_append => $location_cfg_append,
index_files => [ 'index.php' ],
ssl => true,
2015-07-03 03:04:06 +02:00
ssl_cert => '/path/to/wildcard_mydomain.crt',
ssl_key => '/path/to/wildcard_mydomain.key',
2014-01-12 20:15:14 +01:00
}
2014-05-15 23:58:44 +02:00
2014-01-12 20:15:14 +01:00
if $php {
nginx::resource::location { "${name}_root":
ensure => present,
2014-05-15 23:58:44 +02:00
ssl => true,
ssl_only => true,
2014-01-12 20:15:14 +01:00
vhost => "${name}.${::domain} ${name}",
2014-05-15 23:58:44 +02:00
www_root => "${full_web_path}/${name}/",
2014-01-12 20:15:14 +01:00
location => '~ \.php$',
index_files => ['index.php', 'index.html', 'index.htm'],
proxy => undef,
fastcgi => "127.0.0.1:${backend_port}",
fastcgi_script => undef,
2014-05-15 23:58:44 +02:00
location_cfg_append => {
2014-01-12 20:15:14 +01:00
fastcgi_connect_timeout => '3m',
fastcgi_read_timeout => '3m',
2014-05-15 23:58:44 +02:00
fastcgi_send_timeout => '3m'
2014-08-11 10:15:38 +02:00
}
2014-05-15 23:58:44 +02:00
}
}
}
```
2014-01-12 20:15:14 +01:00
2014-08-11 10:04:21 +02:00
## Add custom fastcgi_params
```puppet
nginx::resource::location { "some_root":
ensure => present,
location => '/some/url',
2014-08-11 10:12:09 +02:00
fastcgi => "127.0.0.1:9000",
2014-08-13 12:24:59 +02:00
fastcgi_param => {
2014-08-11 10:12:09 +02:00
'APP_ENV' => 'local',
2014-08-11 10:04:21 +02:00
},
}
```
2014-01-12 20:15:14 +01:00
# Call class web::nginx_ssl_with_redirect
2014-01-17 13:49:28 +01:00
```puppet
web::nginx_ssl_with_redirect { 'sub-domain-name':
2014-01-12 20:15:14 +01:00
backend_port => 9001,
}
```