vhost.pp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. # define: nginx::resource::vhost
  2. #
  3. # This definition creates a virtual host
  4. #
  5. # Parameters:
  6. # [*ensure*] - Enables or disables the specified vhost
  7. # (present|absent)
  8. # [*listen_ip*] - Default IP Address for NGINX to listen with this
  9. # vHost on. Defaults to all interfaces (*)
  10. # [*listen_port*] - Default IP Port for NGINX to listen with this
  11. # vHost on. Defaults to TCP 80
  12. # [*listen_options*] - Extra options for listen directive like
  13. # 'default' to catchall. Undef by default.
  14. # [*location_allow*] - Array: Locations to allow connections from.
  15. # [*location_deny*] - Array: Locations to deny connections from.
  16. # [*ipv6_enable*] - BOOL value to enable/disable IPv6 support
  17. # (false|true). Module will check to see if IPv6 support exists on your
  18. # system before enabling.
  19. # [*ipv6_listen_ip*] - Default IPv6 Address for NGINX to listen with
  20. # this vHost on. Defaults to all interfaces (::)
  21. # [*ipv6_listen_port*] - Default IPv6 Port for NGINX to listen with this
  22. # vHost on. Defaults to TCP 80
  23. # [*ipv6_listen_options*] - Extra options for listen directive like 'default'
  24. # to catchall. Template will allways add ipv6only=on. While issue
  25. # jfryman/puppet-nginx#30 is discussed, default value is 'default'.
  26. # [*add_header*] - Hash: Adds headers to the HTTP response when
  27. # response code is equal to 200, 204, 301, 302 or 304.
  28. # [*index_files*] - Default index files for NGINX to read when
  29. # traversing a directory
  30. # [*autoindex*] - Set it on 'on' or 'off 'to activate/deactivate
  31. # autoindex directory listing. Undef by default.
  32. # [*proxy*] - Proxy server(s) for the root location to connect
  33. # to. Accepts a single value, can be used in conjunction with
  34. # nginx::resource::upstream
  35. # [*proxy_read_timeout*] - Override the default the proxy read timeout value
  36. # of 90 seconds
  37. # [*proxy_redirect*] - Override the default proxy_redirect value of off.
  38. # [*resolver*] - Array: Configures name servers used to resolve
  39. # names of upstream servers into addresses.
  40. # [*fastcgi*] - location of fastcgi (host:port)
  41. # [*fastcgi_params*] - optional alternative fastcgi_params file to use
  42. # [*fastcgi_script*] - optional SCRIPT_FILE parameter
  43. # [*ssl*] - Indicates whether to setup SSL bindings for this
  44. # vhost.
  45. # [*ssl_cert*] - Pre-generated SSL Certificate file to reference
  46. # for SSL Support. This is not generated by this module.
  47. # [*ssl_client_cert*] - Pre-generated SSL Certificate file to reference
  48. # for client verify SSL Support. This is not generated by this module.
  49. # [*ssl_dhparam*] - This directive specifies a file containing
  50. # Diffie-Hellman key agreement protocol cryptographic parameters, in PEM
  51. # format, utilized for exchanging session keys between server and client.
  52. # [*ssl_key*] - Pre-generated SSL Key file to reference for SSL
  53. # Support. This is not generated by this module.
  54. # [*ssl_port*] - Default IP Port for NGINX to listen with this SSL
  55. # vHost on. Defaults to TCP 443
  56. # [*ssl_protocols*] - SSL protocols enabled. Defaults to 'TLSv1 TLSv1.1
  57. # TLSv1.2'.
  58. # [*ssl_ciphers*] - SSL ciphers enabled. Defaults to
  59. # 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'.
  60. # [*ssl_stapling*] - Bool: Enables or disables stapling of OCSP
  61. # responses by the server. Defaults to false.
  62. # [*ssl_stapling_file*] - String: When set, the stapled OCSP response
  63. # will be taken from the specified file instead of querying the OCSP
  64. # responder specified in the server certificate.
  65. # [*ssl_stapling_responder*] - String: Overrides the URL of the OCSP
  66. # responder specified in the Authority Information Access certificate
  67. # extension.
  68. # [*ssl_stapling_verify*] - Bool: Enables or disables verification of
  69. # OCSP responses by the server. Defaults to false.
  70. # [*ssl_session_timeout*] - String: Specifies a time during which a client
  71. # may reuse the session parameters stored in a cache. Defaults to 5m.
  72. # [*ssl_trusted_cert*] - String: Specifies a file with trusted CA
  73. # certificates in the PEM format used to verify client certificates and
  74. # OCSP responses if ssl_stapling is enabled.
  75. # [*spdy*] - Toggles SPDY protocol.
  76. # [*server_name*] - List of vhostnames for which this vhost will
  77. # respond. Default [$name].
  78. # [*www_root*] - Specifies the location on disk for files to be
  79. # read from. Cannot be set in conjunction with $proxy
  80. # [*rewrite_www_to_non_www*] - Adds a server directive and rewrite rule to
  81. # rewrite www.domain.com to domain.com in order to avoid duplicate
  82. # content (SEO);
  83. # [*try_files*] - Specifies the locations for files to be
  84. # checked as an array. Cannot be used in conjuction with $proxy.
  85. # [*proxy_cache*] - This directive sets name of zone for caching.
  86. # The same zone can be used in multiple places.
  87. # [*proxy_cache_valid*] - This directive sets the time for caching
  88. # different replies.
  89. # [*proxy_method*] - If defined, overrides the HTTP method of the
  90. # request to be passed to the backend.
  91. # [*proxy_set_body*] - If defined, sets the body passed to the backend.
  92. # [*auth_basic*] - This directive includes testing name and
  93. # password with HTTP Basic Authentication.
  94. # [*auth_basic_user_file*] - This directive sets the htpasswd filename for
  95. # the authentication realm.
  96. # [*client_max_body_size*] - This directive sets client_max_body_size.
  97. # [*client_body_timeout*] - Sets how long the server will wait for a
  98. # client body. Default is 60s
  99. # [*client_header_timeout*] - Sets how long the server will wait for a
  100. # client header. Default is 60s
  101. # [*raw_prepend*] - A single string, or an array of strings to
  102. # prepend to the server directive (after cfg prepend directives). NOTE:
  103. # YOU are responsible for a semicolon on each line that requires one.
  104. # [*raw_append*] - A single string, or an array of strings to
  105. # append to the server directive (after cfg append directives). NOTE:
  106. # YOU are responsible for a semicolon on each line that requires one.
  107. # [*location_raw_prepend*] - A single string, or an array of strings
  108. # to prepend to the location directive (after custom_cfg directives). NOTE:
  109. # YOU are responsible for a semicolon on each line that requires one.
  110. # [*location_raw_append*] - A single string, or an array of strings
  111. # to append to the location directive (after custom_cfg directives). NOTE:
  112. # YOU are responsible for a semicolon on each line that requires one.
  113. # [*vhost_cfg_append*] - It expects a hash with custom directives to
  114. # put after everything else inside vhost
  115. # [*vhost_cfg_prepend*] - It expects a hash with custom directives to
  116. # put before everything else inside vhost
  117. # [*vhost_cfg_ssl_append*] - It expects a hash with custom directives to
  118. # put after everything else inside vhost ssl
  119. # [*vhost_cfg_ssl_prepend*] - It expects a hash with custom directives to
  120. # put before everything else inside vhost ssl
  121. # [*rewrite_to_https*] - Adds a server directive and rewrite rule to
  122. # rewrite to ssl
  123. # [*include_files*] - Adds include files to vhost
  124. # [*access_log*] - Where to write access log. May add additional
  125. # options like log format to the end.
  126. # [*error_log*] - Where to write error log. May add additional
  127. # options like error level to the end.
  128. # [*passenger_cgi_param*] - Allows one to define additional CGI environment
  129. # variables to pass to the backend application
  130. # [*log_by_lua*] - Run the Lua source code inlined as the
  131. # <lua-script-str> at the log request processing phase.
  132. # This does not replace the current access logs, but runs after.
  133. # [*log_by_lua_file*] - Equivalent to log_by_lua, except that the file
  134. # specified by <path-to-lua-script-file> contains the Lua code, or, as from
  135. # the v0.5.0rc32 release, the Lua/LuaJIT bytecode to be executed.
  136. # [*gzip_types*] - Defines gzip_types, nginx default is text/html
  137. # [*owner*] - Defines owner of the .conf file
  138. # [*group*] - Defines group of the .conf file
  139. # [*mode*] - Defines mode of the .conf file
  140. # Actions:
  141. #
  142. # Requires:
  143. #
  144. # Sample Usage:
  145. # nginx::resource::vhost { 'test2.local':
  146. # ensure => present,
  147. # www_root => '/var/www/nginx-default',
  148. # ssl => true,
  149. # ssl_cert => '/tmp/server.crt',
  150. # ssl_key => '/tmp/server.pem',
  151. # }
  152. define nginx::resource::vhost (
  153. $ensure = 'present',
  154. $listen_ip = '*',
  155. $listen_port = '80',
  156. $listen_options = undef,
  157. $location_allow = [],
  158. $location_deny = [],
  159. $ipv6_enable = false,
  160. $ipv6_listen_ip = '::',
  161. $ipv6_listen_port = '80',
  162. $ipv6_listen_options = 'default ipv6only=on',
  163. $add_header = undef,
  164. $ssl = false,
  165. $ssl_listen_option = true,
  166. $ssl_cert = undef,
  167. $ssl_client_cert = undef,
  168. $ssl_dhparam = undef,
  169. $ssl_key = undef,
  170. $ssl_port = '443',
  171. $ssl_protocols = 'TLSv1 TLSv1.1 TLSv1.2',
  172. $ssl_ciphers = 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA',
  173. $ssl_cache = 'shared:SSL:10m',
  174. $ssl_stapling = false,
  175. $ssl_stapling_file = undef,
  176. $ssl_stapling_responder = undef,
  177. $ssl_stapling_verify = false,
  178. $ssl_session_timeout = '5m',
  179. $ssl_trusted_cert = undef,
  180. $spdy = $::nginx::config::spdy,
  181. $proxy = undef,
  182. $proxy_redirect = undef,
  183. $proxy_read_timeout = $::nginx::config::proxy_read_timeout,
  184. $proxy_connect_timeout = $::nginx::config::proxy_connect_timeout,
  185. $proxy_set_header = [],
  186. $proxy_cache = false,
  187. $proxy_cache_valid = false,
  188. $proxy_method = undef,
  189. $proxy_set_body = undef,
  190. $resolver = [],
  191. $fastcgi = undef,
  192. $fastcgi_params = "${::nginx::config::conf_dir}/fastcgi_params",
  193. $fastcgi_script = undef,
  194. $index_files = [
  195. 'index.html',
  196. 'index.htm',
  197. 'index.php'],
  198. $autoindex = undef,
  199. $server_name = [$name],
  200. $www_root = undef,
  201. $rewrite_www_to_non_www = false,
  202. $rewrite_to_https = undef,
  203. $location_custom_cfg = undef,
  204. $location_cfg_prepend = undef,
  205. $location_cfg_append = undef,
  206. $location_custom_cfg_prepend = undef,
  207. $location_custom_cfg_append = undef,
  208. $try_files = undef,
  209. $auth_basic = undef,
  210. $auth_basic_user_file = undef,
  211. $client_body_timeout = undef,
  212. $client_header_timeout = undef,
  213. $client_max_body_size = undef,
  214. $raw_prepend = undef,
  215. $raw_append = undef,
  216. $location_raw_prepend = undef,
  217. $location_raw_append = undef,
  218. $vhost_cfg_prepend = undef,
  219. $vhost_cfg_append = undef,
  220. $vhost_cfg_ssl_prepend = undef,
  221. $vhost_cfg_ssl_append = undef,
  222. $include_files = undef,
  223. $access_log = undef,
  224. $error_log = undef,
  225. $format_log = 'combined',
  226. $passenger_cgi_param = undef,
  227. $log_by_lua = undef,
  228. $log_by_lua_file = undef,
  229. $use_default_location = true,
  230. $rewrite_rules = [],
  231. $string_mappings = {},
  232. $geo_mappings = {},
  233. $gzip_types = undef,
  234. $owner = $::nginx::config::global_owner,
  235. $group = $::nginx::config::global_group,
  236. $mode = $::nginx::config::global_mode,
  237. $maintenance = false,
  238. ) {
  239. validate_re($ensure, '^(present|absent)$',
  240. "${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
  241. if !(is_array($listen_ip) or is_string($listen_ip)) {
  242. fail('$listen_ip must be a string or array.')
  243. }
  244. if !is_integer($listen_port) {
  245. fail('$listen_port must be an integer.')
  246. }
  247. if ($listen_options != undef) {
  248. validate_string($listen_options)
  249. }
  250. validate_array($location_allow)
  251. validate_array($location_deny)
  252. validate_bool($ipv6_enable)
  253. if !(is_array($ipv6_listen_ip) or is_string($ipv6_listen_ip)) {
  254. fail('$ipv6_listen_ip must be a string or array.')
  255. }
  256. if !is_integer($ipv6_listen_port) {
  257. fail('$ipv6_listen_port must be an integer.')
  258. }
  259. validate_string($ipv6_listen_options)
  260. if ($add_header != undef) {
  261. validate_hash($add_header)
  262. }
  263. validate_bool($ssl)
  264. if ($ssl_cert != undef) {
  265. validate_string($ssl_cert)
  266. }
  267. if ($ssl_client_cert != undef) {
  268. validate_string($ssl_client_cert)
  269. }
  270. validate_bool($ssl_listen_option)
  271. if ($ssl_dhparam != undef) {
  272. validate_string($ssl_dhparam)
  273. }
  274. if ($ssl_key != undef) {
  275. validate_string($ssl_key)
  276. }
  277. if !is_integer($ssl_port) {
  278. fail('$ssl_port must be an integer.')
  279. }
  280. validate_string($ssl_protocols)
  281. validate_string($ssl_ciphers)
  282. validate_string($ssl_cache)
  283. validate_bool($ssl_stapling)
  284. if ($ssl_stapling_file != undef) {
  285. validate_string($ssl_stapling_file)
  286. }
  287. if ($ssl_stapling_responder != undef) {
  288. validate_string($ssl_stapling_responder)
  289. }
  290. validate_bool($ssl_stapling_verify)
  291. validate_string($ssl_session_timeout)
  292. if ($ssl_trusted_cert != undef) {
  293. validate_string($ssl_trusted_cert)
  294. }
  295. validate_string($spdy)
  296. if ($proxy != undef) {
  297. validate_string($proxy)
  298. }
  299. validate_string($proxy_read_timeout)
  300. if ($proxy_redirect != undef) {
  301. validate_string($proxy_redirect)
  302. }
  303. validate_array($proxy_set_header)
  304. if ($proxy_cache != false) {
  305. validate_string($proxy_cache)
  306. }
  307. if ($proxy_cache_valid != false) {
  308. validate_string($proxy_cache_valid)
  309. }
  310. if ($proxy_method != undef) {
  311. validate_string($proxy_method)
  312. }
  313. if ($proxy_set_body != undef) {
  314. validate_string($proxy_set_body)
  315. }
  316. validate_array($resolver)
  317. if ($fastcgi != undef) {
  318. validate_string($fastcgi)
  319. }
  320. validate_string($fastcgi_params)
  321. if ($fastcgi_script != undef) {
  322. validate_string($fastcgi_script)
  323. }
  324. validate_array($index_files)
  325. if ($autoindex != undef) {
  326. validate_string($autoindex)
  327. }
  328. validate_array($server_name)
  329. if ($www_root != undef) {
  330. validate_string($www_root)
  331. }
  332. validate_bool($rewrite_www_to_non_www)
  333. if ($rewrite_to_https != undef) {
  334. validate_bool($rewrite_to_https)
  335. }
  336. if ($raw_prepend != undef) {
  337. if (is_array($raw_prepend)) {
  338. validate_array($raw_prepend)
  339. } else {
  340. validate_string($raw_prepend)
  341. }
  342. }
  343. if ($raw_append != undef) {
  344. if (is_array($raw_append)) {
  345. validate_array($raw_append)
  346. } else {
  347. validate_string($raw_append)
  348. }
  349. }
  350. if ($location_raw_prepend != undef) {
  351. if (is_array($location_raw_prepend)) {
  352. validate_array($location_raw_prepend)
  353. } else {
  354. validate_string($location_raw_prepend)
  355. }
  356. }
  357. if ($location_raw_append != undef) {
  358. if (is_array($location_raw_append)) {
  359. validate_array($location_raw_append)
  360. } else {
  361. validate_string($location_raw_append)
  362. }
  363. }
  364. if ($location_custom_cfg != undef) {
  365. validate_hash($location_custom_cfg)
  366. }
  367. if ($location_cfg_prepend != undef) {
  368. validate_hash($location_cfg_prepend)
  369. }
  370. if ($location_cfg_append != undef) {
  371. validate_hash($location_cfg_append)
  372. }
  373. if ($try_files != undef) {
  374. validate_array($try_files)
  375. }
  376. if ($auth_basic != undef) {
  377. validate_string($auth_basic)
  378. }
  379. if ($auth_basic_user_file != undef) {
  380. validate_string($auth_basic_user_file)
  381. }
  382. if ($vhost_cfg_prepend != undef) {
  383. validate_hash($vhost_cfg_prepend)
  384. }
  385. if ($vhost_cfg_append != undef) {
  386. validate_hash($vhost_cfg_append)
  387. }
  388. if ($vhost_cfg_ssl_prepend != undef) {
  389. validate_hash($vhost_cfg_ssl_prepend)
  390. }
  391. if ($vhost_cfg_ssl_append != undef) {
  392. validate_hash($vhost_cfg_ssl_append)
  393. }
  394. if ($include_files != undef) {
  395. validate_array($include_files)
  396. }
  397. if ($access_log != undef) {
  398. validate_string($access_log)
  399. }
  400. if ($error_log != undef) {
  401. validate_string($error_log)
  402. }
  403. if ($passenger_cgi_param != undef) {
  404. validate_hash($passenger_cgi_param)
  405. }
  406. if ($log_by_lua != undef) {
  407. validate_string($log_by_lua)
  408. }
  409. if ($log_by_lua_file != undef) {
  410. validate_string($log_by_lua_file)
  411. }
  412. if ($client_body_timeout != undef) {
  413. validate_string($client_body_timeout)
  414. }
  415. if ($client_header_timeout != undef) {
  416. validate_string($client_header_timeout)
  417. }
  418. if ($gzip_types != undef) {
  419. validate_string($gzip_types)
  420. }
  421. validate_bool($use_default_location)
  422. validate_array($rewrite_rules)
  423. validate_hash($string_mappings)
  424. validate_hash($geo_mappings)
  425. validate_string($owner)
  426. validate_string($group)
  427. validate_re($mode, '^\d{4}$',
  428. "${mode} is not valid. It should be 4 digits (0644 by default).")
  429. # Variables
  430. $vhost_dir = "${::nginx::config::conf_dir}/sites-available"
  431. $vhost_enable_dir = "${::nginx::config::conf_dir}/sites-enabled"
  432. $vhost_symlink_ensure = $ensure ? {
  433. 'absent' => absent,
  434. default => 'link',
  435. }
  436. $name_sanitized = regsubst($name, ' ', '_', 'G')
  437. $config_file = "${vhost_dir}/${name_sanitized}.conf"
  438. File {
  439. ensure => $ensure ? {
  440. 'absent' => absent,
  441. default => 'file',
  442. },
  443. notify => Class['::nginx::service'],
  444. owner => $owner,
  445. group => $group,
  446. mode => $mode,
  447. }
  448. # Add IPv6 Logic Check - Nginx service will not start if ipv6 is enabled
  449. # and support does not exist for it in the kernel.
  450. if ($ipv6_enable == true) and (!$::ipaddress6) {
  451. warning('nginx: IPv6 support is not enabled or configured properly')
  452. }
  453. # Check to see if SSL Certificates are properly defined.
  454. if ($ssl == true) {
  455. if ($ssl_cert == undef) or ($ssl_key == undef) {
  456. fail('nginx: SSL certificate/key (ssl_cert/ssl_key) and/or SSL Private must be defined and exist on the target system(s)')
  457. }
  458. }
  459. # This was a lot to add up in parameter list so add it down here
  460. # Also opted to add more logic here and keep template cleaner which
  461. # unfortunately means resorting to the $varname_real thing
  462. $access_log_real = $access_log ? {
  463. 'off' => 'off',
  464. undef => "${::nginx::config::log_dir}/${name_sanitized}.access.log ${format_log}",
  465. default => "${access_log} ${format_log}",
  466. }
  467. $error_log_real = $error_log ? {
  468. undef => "${::nginx::config::log_dir}/${name_sanitized}.error.log",
  469. default => $error_log,
  470. }
  471. concat { $config_file:
  472. owner => $owner,
  473. group => $group,
  474. mode => $mode,
  475. notify => Class['::nginx::service'],
  476. }
  477. $ssl_only = ($ssl == true) and ($ssl_port == $listen_port)
  478. if $use_default_location == true {
  479. # Create the default location reference for the vHost
  480. ::nginx::resource::location {"${name_sanitized}-default":
  481. ensure => $ensure,
  482. vhost => $name_sanitized,
  483. ssl => $ssl,
  484. ssl_only => $ssl_only,
  485. location => '/',
  486. location_allow => $location_allow,
  487. location_deny => $location_deny,
  488. proxy => $proxy,
  489. proxy_redirect => $proxy_redirect,
  490. proxy_read_timeout => $proxy_read_timeout,
  491. proxy_connect_timeout => $proxy_connect_timeout,
  492. proxy_cache => $proxy_cache,
  493. proxy_cache_valid => $proxy_cache_valid,
  494. proxy_method => $proxy_method,
  495. proxy_set_header => $proxy_set_header,
  496. proxy_set_body => $proxy_set_body,
  497. fastcgi => $fastcgi,
  498. fastcgi_params => $fastcgi_params,
  499. fastcgi_script => $fastcgi_script,
  500. try_files => $try_files,
  501. www_root => $www_root,
  502. autoindex => $autoindex,
  503. index_files => [],
  504. location_custom_cfg => $location_custom_cfg,
  505. notify => Class['::nginx::service'],
  506. rewrite_rules => $rewrite_rules,
  507. raw_prepend => $location_raw_prepend,
  508. raw_append => $location_raw_append
  509. }
  510. $root = undef
  511. } else {
  512. $root = $www_root
  513. }
  514. # Support location_cfg_prepend and location_cfg_append on default location created by vhost
  515. if $location_cfg_prepend {
  516. Nginx::Resource::Location["${name_sanitized}-default"] {
  517. location_cfg_prepend => $location_cfg_prepend }
  518. }
  519. if $location_cfg_append {
  520. Nginx::Resource::Location["${name_sanitized}-default"] {
  521. location_cfg_append => $location_cfg_append }
  522. }
  523. if $location_custom_cfg_prepend {
  524. Nginx::Resource::Location["${name_sanitized}-default"] {
  525. location_custom_cfg_prepend => $location_custom_cfg_prepend }
  526. }
  527. if $location_custom_cfg_append {
  528. Nginx::Resource::Location["${name_sanitized}-default"] {
  529. location_custom_cfg_append => $location_custom_cfg_append }
  530. }
  531. if $fastcgi != undef and !defined(File[$fastcgi_params]) {
  532. file { $fastcgi_params:
  533. ensure => present,
  534. mode => '0770',
  535. content => template('nginx/vhost/fastcgi_params.erb'),
  536. }
  537. }
  538. if ($listen_port != $ssl_port) {
  539. concat::fragment { "${name_sanitized}-header":
  540. ensure => present,
  541. target => $config_file,
  542. content => template('nginx/vhost/vhost_header.erb'),
  543. order => '001',
  544. }
  545. }
  546. # Create a proper file close stub.
  547. if ($listen_port != $ssl_port) {
  548. concat::fragment { "${name_sanitized}-footer":
  549. ensure => present,
  550. target => $config_file,
  551. content => template('nginx/vhost/vhost_footer.erb'),
  552. order => '699',
  553. }
  554. }
  555. # Create SSL File Stubs if SSL is enabled
  556. if ($ssl == true) {
  557. # Access and error logs are named differently in ssl template
  558. # This was a lot to add up in parameter list so add it down here
  559. # Also opted to add more logic here and keep template cleaner which
  560. # unfortunately means resorting to the $varname_real thing
  561. $ssl_access_log_real = $access_log ? {
  562. 'off' => 'off',
  563. undef => "${::nginx::config::log_dir}/ssl-${name_sanitized}.access.log ${format_log}",
  564. default => "${access_log} ${format_log}",
  565. }
  566. $ssl_error_log_real = $error_log ? {
  567. undef => "${::nginx::config::log_dir}/ssl-${name_sanitized}.error.log",
  568. default => $error_log,
  569. }
  570. concat::fragment { "${name_sanitized}-ssl-header":
  571. target => $config_file,
  572. content => template('nginx/vhost/vhost_ssl_header.erb'),
  573. order => '700',
  574. }
  575. concat::fragment { "${name_sanitized}-ssl-footer":
  576. target => $config_file,
  577. content => template('nginx/vhost/vhost_ssl_footer.erb'),
  578. order => '999',
  579. }
  580. #Generate ssl key/cert with provided file-locations
  581. $cert = regsubst($name,' ','_', 'G')
  582. # Check if the file has been defined before creating the file to
  583. # avoid the error when using wildcard cert on the multiple vhosts
  584. ensure_resource('file', "${::nginx::config::conf_dir}/${cert}.crt", {
  585. owner => $::nginx::config::daemon_user,
  586. mode => '0444',
  587. source => $ssl_cert,
  588. })
  589. ensure_resource('file', "${::nginx::config::conf_dir}/${cert}.client.crt", {
  590. owner => $::nginx::config::daemon_user,
  591. mode => '0444',
  592. source => $ssl_client_cert,
  593. })
  594. ensure_resource('file', "${::nginx::config::conf_dir}/${cert}.key", {
  595. owner => $::nginx::config::daemon_user,
  596. mode => '0440',
  597. source => $ssl_key,
  598. })
  599. if ($ssl_dhparam != undef) {
  600. ensure_resource('file', "${::nginx::config::conf_dir}/${cert}.dh.pem", {
  601. owner => $::nginx::config::daemon_user,
  602. mode => '0440',
  603. source => $ssl_dhparam,
  604. })
  605. }
  606. if ($ssl_stapling_file != undef) {
  607. ensure_resource('file', "${::nginx::config::conf_dir}/${cert}.ocsp.resp", {
  608. owner => $::nginx::config::daemon_user,
  609. mode => '0440',
  610. source => $ssl_stapling_file,
  611. })
  612. }
  613. if ($ssl_trusted_cert != undef) {
  614. ensure_resource('file', "${::nginx::config::conf_dir}/${cert}.trusted.crt", {
  615. owner => $::nginx::config::daemon_user,
  616. mode => '0440',
  617. source => $ssl_trusted_cert,
  618. })
  619. }
  620. }
  621. file{ "${name_sanitized}.conf symlink":
  622. ensure => $vhost_symlink_ensure,
  623. path => "${vhost_enable_dir}/${name_sanitized}.conf",
  624. target => $config_file,
  625. require => Concat[$config_file],
  626. notify => Class['::nginx::service'],
  627. }
  628. create_resources('::nginx::resource::map', $string_mappings)
  629. create_resources('::nginx::resource::geo', $geo_mappings)
  630. }