vhost.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # define: nginx::resource::vhost
  2. #
  3. # This definition creates a virtual host
  4. #
  5. # Parameters:
  6. # [*ensure*] - Enables or disables the specified vhost (present|absent)
  7. # [*listen_ip*] - Default IP Address for NGINX to listen with this vHost on. Defaults to all interfaces (*)
  8. # [*listen_port*] - Default IP Port for NGINX to listen with this vHost on. Defaults to TCP 80
  9. # [*listen_options*] - Extra options for listen directive like 'default' to catchall. Undef by default.
  10. # [*ipv6_enable*] - BOOL value to enable/disable IPv6 support (false|true). Module will check to see if IPv6
  11. # support exists on your system before enabling.
  12. # [*ipv6_listen_ip*] - Default IPv6 Address for NGINX to listen with this vHost on. Defaults to all interfaces (::)
  13. # [*ipv6_listen_port*] - Default IPv6 Port for NGINX to listen with this vHost on. Defaults to TCP 80
  14. # [*ipv6_listen_options*] - Extra options for listen directive like 'default' to catchall. Template will allways add ipv6only=on.
  15. # While issue jfryman/puppet-nginx#30 is discussed, default value is 'default'.
  16. # [*index_files*] - Default index files for NGINX to read when traversing a directory
  17. # [*proxy*] - Proxy server(s) for the root location to connect to. Accepts a single value, can be used in
  18. # conjunction with nginx::resource::upstream
  19. # [*proxy_read_timeout*] - Override the default the proxy read timeout value of 90 seconds
  20. # [*ssl*] - Indicates whether to setup SSL bindings for this vhost.
  21. # [*ssl_cert*] - Pre-generated SSL Certificate file to reference for SSL Support. This is not generated by this module.
  22. # [*ssl_key*] - Pre-generated SSL Key file to reference for SSL Support. This is not generated by this module.
  23. # [*ssl_port*] - Default IP Port for NGINX to listen with this SSL vHost on. Defaults to TCP 443
  24. # [*server_name*] - List of vhostnames for which this vhost will respond. Default [$name].
  25. # [*www_root*] - Specifies the location on disk for files to be read from. Cannot be set in conjunction with $proxy
  26. # [*rewrite_www_to_non_www*] - Adds a server directive and rewrite rule to rewrite www.domain.com to domain.com in order to avoid
  27. # duplicate content (SEO);
  28. # [*try_files*] - Specifies the locations for files to be checked as an array. Cannot be used in conjuction with $proxy.
  29. #
  30. # Actions:
  31. #
  32. # Requires:
  33. #
  34. # Sample Usage:
  35. # nginx::resource::vhost { 'test2.local':
  36. # ensure => present,
  37. # www_root => '/var/www/nginx-default',
  38. # ssl => 'true',
  39. # ssl_cert => '/tmp/server.crt',
  40. # ssl_key => '/tmp/server.pem',
  41. # }
  42. define nginx::resource::vhost (
  43. $ensure = 'enable',
  44. $listen_ip = '*',
  45. $listen_port = '80',
  46. $listen_options = undef,
  47. $ipv6_enable = false,
  48. $ipv6_listen_ip = '::',
  49. $ipv6_listen_port = '80',
  50. $ipv6_listen_options = 'default',
  51. $ssl = false,
  52. $ssl_cert = undef,
  53. $ssl_key = undef,
  54. $ssl_port = '443',
  55. $proxy = undef,
  56. $proxy_read_timeout = $nginx::params::nx_proxy_read_timeout,
  57. $proxy_set_header = [],
  58. $index_files = [
  59. 'index.html',
  60. 'index.htm',
  61. 'index.php'],
  62. $server_name = [$name],
  63. $www_root = undef,
  64. $rewrite_www_to_non_www = false,
  65. $location_cfg_prepend = undef,
  66. $location_cfg_append = undef,
  67. $try_files = undef) {
  68. File {
  69. ensure => $ensure ? {
  70. 'absent' => absent,
  71. default => 'file',
  72. },
  73. notify => Class['nginx::service'],
  74. owner => 'root',
  75. group => 'root',
  76. mode => '0644',
  77. }
  78. # Add IPv6 Logic Check - Nginx service will not start if ipv6 is enabled
  79. # and support does not exist for it in the kernel.
  80. if ($ipv6_enable == true) and ($ipaddress6) {
  81. warning('nginx: IPv6 support is not enabled or configured properly')
  82. }
  83. # Check to see if SSL Certificates are properly defined.
  84. if ($ssl == true) {
  85. if ($ssl_cert == undef) or ($ssl_key == undef) {
  86. fail('nginx: SSL certificate/key (ssl_cert/ssl_cert) and/or SSL Private must be defined and exist on the target system(s)')
  87. }
  88. }
  89. # Use the File Fragment Pattern to construct the configuration files.
  90. # Create the base configuration file reference.
  91. if ($listen_port != $ssl_port) {
  92. file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-001": content => template('nginx/vhost/vhost_header.erb'), }
  93. }
  94. if ($ssl == true) and ($ssl_port == $listen_port) {
  95. $ssl_only = true
  96. }
  97. # Create the default location reference for the vHost
  98. nginx::resource::location { "${name}-default":
  99. ensure => $ensure,
  100. vhost => $name,
  101. ssl => $ssl,
  102. ssl_only => $ssl_only,
  103. location => '/',
  104. proxy => $proxy,
  105. proxy_read_timeout => $proxy_read_timeout,
  106. try_files => $try_files,
  107. www_root => $www_root,
  108. notify => Class['nginx::service'],
  109. }
  110. # Support location_cfg_prepend and location_cfg_append on default location created by vhost
  111. if $location_cfg_prepend {
  112. Nginx::Resource::Location["${name}-default"] {
  113. location_cfg_prepend => $location_cfg_prepend }
  114. }
  115. if $location_cfg_append {
  116. Nginx::Resource::Location["${name}-default"] {
  117. location_cfg_append => $location_cfg_append }
  118. }
  119. # Create a proper file close stub.
  120. if ($listen_port != $ssl_port) {
  121. file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-699": content => template('nginx/vhost/vhost_footer.erb'), }
  122. }
  123. # Create SSL File Stubs if SSL is enabled
  124. if ($ssl) {
  125. file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-700-ssl": content => template('nginx/vhost/vhost_ssl_header.erb'), }
  126. file { "${nginx::config::nx_temp_dir}/nginx.d/${name}-999-ssl": content => template('nginx/vhost/vhost_footer.erb'), }
  127. }
  128. }