location.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # define: nginx::resource::location
  2. #
  3. # This definition creates a new location entry within a virtual host
  4. #
  5. # Parameters:
  6. # [*ensure*] - Enables or disables the specified location
  7. # (present|absent)
  8. # [*internal*] - Indicates whether or not this loation can be
  9. # used for internal requests only. Default: false
  10. # [*vhost*] - Defines the default vHost for this location
  11. # entry to include with
  12. # [*location*] - Specifies the URI associated with this location
  13. # entry
  14. # [*location_satisfy*] - Allows access if all (all) or at least one (any) of the auth modules allow access.
  15. # [*location_allow*] - Array: Locations to allow connections from.
  16. # [*location_deny*] - Array: Locations to deny connections from.
  17. # [*www_root*] - Specifies the location on disk for files to be
  18. # read from. Cannot be set in conjunction with $proxy
  19. # [*autoindex*] - Set it on 'on' to activate autoindex directory
  20. # listing. Undef by default.
  21. # [*index_files*] - Default index files for NGINX to read when
  22. # traversing a directory
  23. # [*proxy*] - Proxy server(s) for a location to connect to.
  24. # Accepts a single value, can be used in conjunction with
  25. # nginx::resource::upstream
  26. # [*proxy_redirect*] - sets the text, which must be changed in
  27. # response-header "Location" and "Refresh" in the response of the proxied
  28. # server.
  29. # [*proxy_read_timeout*] - Override the default the proxy read timeout
  30. # value of 90 seconds
  31. # [*proxy_connect_timeout*] - Override the default the proxy connect timeout
  32. # value of 90 seconds
  33. # [*proxy_set_header*] - Array of vhost headers to set
  34. # [*proxy_hide_header*] - Array of vhost headers to hide
  35. # [*fastcgi*] - location of fastcgi (host:port)
  36. # [*fastcgi_param*] - Set additional custom fastcgi_params
  37. # [*fastcgi_params*] - optional alternative fastcgi_params file to use
  38. # [*fastcgi_script*] - optional SCRIPT_FILE parameter
  39. # [*fastcgi_split_path*] - Allows settings of fastcgi_split_path_info so
  40. # that you can split the script_name and path_info via regex
  41. # [*uwsgi*] - location of uwsgi (host:port)
  42. # [*uwsgi_params*] - optional alternative uwsgi_params file to use
  43. # [*ssl*] - Indicates whether to setup SSL bindings for
  44. # this location.
  45. # [*ssl_only*] - Required if the SSL and normal vHost have the
  46. # same port.
  47. # [*location_alias*] - Path to be used as basis for serving requests
  48. # for this location
  49. # [*stub_status*] - If true it will point configure module
  50. # stub_status to provide nginx stats on location
  51. # [*raw_prepend*] - A single string, or an array of strings to
  52. # prepend to the location directive (after custom_cfg directives). NOTE:
  53. # YOU are responsible for a semicolon on each line that requires one.
  54. # [*raw_append*] - A single string, or an array of strings to
  55. # append to the location directive (after custom_cfg directives). NOTE:
  56. # YOU are responsible for a semicolon on each line that requires one.
  57. # [*location_custom_cfg*] - Expects a hash with custom directives, cannot
  58. # be used with other location types (proxy, fastcgi, root, or stub_status)
  59. # [*location_cfg_prepend*] - Expects a hash with extra directives to put
  60. # before anything else inside location (used with all other types except
  61. # custom_cfg)
  62. # [*location_custom_cfg_prepend*] - Expects a array with extra directives
  63. # to put before anything else inside location (used with all other types
  64. # except custom_cfg). Used for logical structures such as if.
  65. # [*location_custom_cfg_append*] - Expects a array with extra directives
  66. # to put after anything else inside location (used with all other types
  67. # except custom_cfg). Used for logical structures such as if.
  68. # [*location_cfg_append*] - Expects a hash with extra directives to put
  69. # after everything else inside location (used with all other types except
  70. # custom_cfg)
  71. # [*try_files*] - An array of file locations to try
  72. # [*option*] - Reserved for future use
  73. # [*proxy_cache*] - This directive sets name of zone for caching.
  74. # The same zone can be used in multiple places.
  75. # [*proxy_cache_key*] - Override the default proxy_cache_key of
  76. # $scheme$proxy_host$request_uri
  77. # [*proxy_cache_use_stale*] - Override the default proxy_cache_use_stale value
  78. # of off.
  79. # [*proxy_cache_valid*] - This directive sets the time for caching
  80. # different replies.
  81. # [*proxy_method*] - If defined, overrides the HTTP method of the
  82. # request to be passed to the backend.
  83. # [*proxy_set_body*] - If defined, sets the body passed to the backend.
  84. # [*auth_basic*] - This directive includes testing name and password
  85. # with HTTP Basic Authentication.
  86. # [*auth_basic_user_file*] - This directive sets the htpasswd filename for
  87. # the authentication realm.
  88. # [*priority*] - Location priority. Default: 500. User priority
  89. # 401-499, 501-599. If the priority is higher than the default priority,
  90. # the location will be defined after root, or before root.
  91. # [*mp4*] - Indicates whether or not this loation can be
  92. # used for mp4 streaming. Default: false
  93. # [*flv*] - Indicates whether or not this loation can be
  94. # used for flv streaming. Default: false
  95. #
  96. #
  97. # Actions:
  98. #
  99. # Requires:
  100. #
  101. # Sample Usage:
  102. # nginx::resource::location { 'test2.local-bob':
  103. # ensure => present,
  104. # www_root => '/var/www/bob',
  105. # location => '/bob',
  106. # vhost => 'test2.local',
  107. # }
  108. #
  109. # Custom config example to limit location on localhost,
  110. # create a hash with any extra custom config you want.
  111. # $my_config = {
  112. # 'access_log' => 'off',
  113. # 'allow' => '127.0.0.1',
  114. # 'deny' => 'all'
  115. # }
  116. # nginx::resource::location { 'test2.local-bob':
  117. # ensure => present,
  118. # www_root => '/var/www/bob',
  119. # location => '/bob',
  120. # vhost => 'test2.local',
  121. # location_cfg_append => $my_config,
  122. # }
  123. #
  124. # Add Custom fastcgi_params
  125. # nginx::resource::location { 'test2.local-bob':
  126. # ensure => present,
  127. # www_root => '/var/www/bob',
  128. # location => '/bob',
  129. # vhost => 'test2.local',
  130. # fastcgi_param => {
  131. # 'APP_ENV' => 'local',
  132. # }
  133. # }
  134. define nginx::resource::location (
  135. $ensure = present,
  136. $internal = false,
  137. $location = $name,
  138. $vhost = undef,
  139. $www_root = undef,
  140. $autoindex = undef,
  141. $index_files = [
  142. 'index.html',
  143. 'index.htm',
  144. 'index.php'],
  145. $proxy = undef,
  146. $proxy_redirect = $::nginx::config::proxy_redirect,
  147. $proxy_read_timeout = $::nginx::config::proxy_read_timeout,
  148. $proxy_connect_timeout = $::nginx::config::proxy_connect_timeout,
  149. $proxy_set_header = $::nginx::config::proxy_set_header,
  150. $proxy_hide_header = $::nginx::config::proxy_hide_header,
  151. $fastcgi = undef,
  152. $fastcgi_param = undef,
  153. $fastcgi_params = "${::nginx::config::conf_dir}/fastcgi_params",
  154. $fastcgi_script = undef,
  155. $fastcgi_split_path = undef,
  156. $uwsgi = undef,
  157. $uwsgi_params = "${nginx::config::conf_dir}/uwsgi_params",
  158. $ssl = false,
  159. $ssl_only = false,
  160. $location_alias = undef,
  161. $location_satisfy = undef,
  162. $location_allow = undef,
  163. $location_deny = undef,
  164. $option = undef,
  165. $stub_status = undef,
  166. $raw_prepend = undef,
  167. $raw_append = undef,
  168. $location_custom_cfg = undef,
  169. $location_cfg_prepend = undef,
  170. $location_cfg_append = undef,
  171. $location_custom_cfg_prepend = undef,
  172. $location_custom_cfg_append = undef,
  173. $include = undef,
  174. $try_files = undef,
  175. $proxy_cache = false,
  176. $proxy_cache_key = undef,
  177. $proxy_cache_use_stale = undef,
  178. $proxy_cache_valid = false,
  179. $proxy_method = undef,
  180. $proxy_set_body = undef,
  181. $auth_basic = undef,
  182. $auth_basic_user_file = undef,
  183. $rewrite_rules = [],
  184. $priority = 500,
  185. $mp4 = false,
  186. $flv = false,
  187. ) {
  188. $root_group = $::nginx::config::root_group
  189. File {
  190. owner => 'root',
  191. group => $root_group,
  192. mode => '0644',
  193. notify => Class['::nginx::service'],
  194. }
  195. validate_re($ensure, '^(present|absent)$',
  196. "${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
  197. validate_string($location)
  198. if ($vhost != undef) {
  199. validate_string($vhost)
  200. }
  201. if ($www_root != undef) {
  202. validate_string($www_root)
  203. }
  204. if ($autoindex != undef) {
  205. validate_string($autoindex)
  206. }
  207. validate_array($index_files)
  208. if ($proxy != undef) {
  209. validate_string($proxy)
  210. }
  211. if ($proxy_redirect != undef) {
  212. validate_string($proxy_redirect)
  213. }
  214. validate_string($proxy_read_timeout)
  215. validate_string($proxy_connect_timeout)
  216. validate_array($proxy_set_header)
  217. validate_array($proxy_hide_header)
  218. if ($fastcgi != undef) {
  219. validate_string($fastcgi)
  220. }
  221. if ($fastcgi_param != undef) {
  222. validate_hash($fastcgi_param)
  223. }
  224. validate_string($fastcgi_params)
  225. if ($fastcgi_script != undef) {
  226. validate_string($fastcgi_script)
  227. }
  228. if ($fastcgi_split_path != undef) {
  229. validate_string($fastcgi_split_path)
  230. }
  231. if ($uwsgi != undef) {
  232. validate_string($uwsgi)
  233. }
  234. validate_string($uwsgi_params)
  235. validate_bool($internal)
  236. validate_bool($ssl)
  237. validate_bool($ssl_only)
  238. if ($location_alias != undef) {
  239. validate_string($location_alias)
  240. }
  241. if ($location_satisfy != undef) {
  242. validate_re($location_satisfy, '^(any|all)$',
  243. "${$location_satisfy} is not supported for location_satisfy. Allowed values are 'any' and 'all'.")
  244. }
  245. if ($location_allow != undef) {
  246. validate_array($location_allow)
  247. }
  248. if ($location_deny != undef) {
  249. validate_array($location_deny)
  250. }
  251. if ($option != undef) {
  252. warning('The $option parameter has no effect and is deprecated.')
  253. }
  254. if ($stub_status != undef) {
  255. validate_bool($stub_status)
  256. }
  257. if ($raw_prepend != undef) {
  258. if (is_array($raw_prepend)) {
  259. validate_array($raw_prepend)
  260. } else {
  261. validate_string($raw_prepend)
  262. }
  263. }
  264. if ($raw_append != undef) {
  265. if (is_array($raw_append)) {
  266. validate_array($raw_append)
  267. } else {
  268. validate_string($raw_append)
  269. }
  270. }
  271. if ($location_custom_cfg != undef) {
  272. validate_hash($location_custom_cfg)
  273. }
  274. if ($location_cfg_prepend != undef) {
  275. validate_hash($location_cfg_prepend)
  276. }
  277. if ($location_cfg_append != undef) {
  278. validate_hash($location_cfg_append)
  279. }
  280. if ($include != undef) {
  281. validate_array($include)
  282. }
  283. if ($try_files != undef) {
  284. validate_array($try_files)
  285. }
  286. if ($proxy_cache != false) {
  287. validate_string($proxy_cache)
  288. }
  289. if ($proxy_cache_key != undef) {
  290. validate_string($proxy_cache_key)
  291. }
  292. if ($proxy_cache_use_stale != undef) {
  293. validate_string($proxy_cache_use_stale)
  294. }
  295. if ($proxy_cache_valid != false) {
  296. if !(is_array($proxy_cache_valid) or is_string($proxy_cache_valid)) {
  297. fail('$proxy_cache_valid must be a string or an array or false.')
  298. }
  299. }
  300. if ($proxy_method != undef) {
  301. validate_string($proxy_method)
  302. }
  303. if ($proxy_set_body != undef) {
  304. validate_string($proxy_set_body)
  305. }
  306. if ($auth_basic != undef) {
  307. validate_string($auth_basic)
  308. }
  309. if ($auth_basic_user_file != undef) {
  310. validate_string($auth_basic_user_file)
  311. }
  312. if !is_integer($priority) {
  313. fail('$priority must be an integer.')
  314. }
  315. validate_array($rewrite_rules)
  316. if (($priority + 0) < 401) or (($priority + 0) > 899) {
  317. fail('$priority must be in the range 401-899.')
  318. }
  319. # # Shared Variables
  320. $ensure_real = $ensure ? {
  321. 'absent' => absent,
  322. default => file,
  323. }
  324. ## Check for various error conditions
  325. if ($vhost == undef) {
  326. fail('Cannot create a location reference without attaching to a virtual host')
  327. }
  328. if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef) and ($fastcgi == undef) and ($uwsgi == undef) and ($location_custom_cfg == undef) and ($internal == false)) {
  329. fail('Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, uwsgi, stub_status, internal, or location_custom_cfg defined')
  330. }
  331. if (($www_root != undef) and ($proxy != undef)) {
  332. fail('Cannot define both directory and proxy in a virtual host')
  333. }
  334. # Use proxy, fastcgi or uwsgi template if $proxy is defined, otherwise use directory template.
  335. # fastcgi_script is deprecated
  336. if ($fastcgi_script != undef) {
  337. warning('The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
  338. }
  339. $vhost_sanitized = regsubst($vhost, ' ', '_', 'G')
  340. $config_file = "${::nginx::config::conf_dir}/sites-available/${vhost_sanitized}.conf"
  341. $location_sanitized_tmp = regsubst($location, '\/', '_', 'G')
  342. $location_sanitized = regsubst($location_sanitized_tmp, '\\\\', '_', 'G')
  343. # Use proxy or fastcgi template if $proxy is defined, otherwise use directory template.
  344. if ($proxy != undef) {
  345. $content_real = template('nginx/vhost/locations/proxy.erb')
  346. } elsif ($location_alias != undef) {
  347. $content_real = template('nginx/vhost/locations/alias.erb')
  348. } elsif ($stub_status != undef) {
  349. $content_real = template('nginx/vhost/locations/stub_status.erb')
  350. } elsif ($fastcgi != undef) {
  351. $content_real = template('nginx/vhost/locations/fastcgi.erb')
  352. } elsif ($uwsgi != undef) {
  353. $content_real = template('nginx/vhost/locations/uwsgi.erb')
  354. } elsif ($www_root != undef) {
  355. $content_real = template('nginx/vhost/locations/directory.erb')
  356. } else {
  357. $content_real = template('nginx/vhost/locations/empty.erb')
  358. }
  359. if $ensure == present and $fastcgi != undef and !defined(File[$fastcgi_params]) {
  360. file { $fastcgi_params:
  361. ensure => present,
  362. mode => '0770',
  363. content => template('nginx/vhost/fastcgi_params.erb'),
  364. }
  365. }
  366. if $ensure == present and $uwsgi != undef and !defined(File[$uwsgi_params]) {
  367. file { $uwsgi_params:
  368. ensure => present,
  369. mode => '0770',
  370. content => template('nginx/vhost/uwsgi_params.erb'),
  371. }
  372. }
  373. ## Create stubs for vHost File Fragment Pattern
  374. $location_md5 = md5($location)
  375. if ($ssl_only != true) {
  376. concat::fragment { "${vhost_sanitized}-${priority}-${location_md5}":
  377. target => $config_file,
  378. content => join([
  379. template('nginx/vhost/location_header.erb'),
  380. $content_real,
  381. template('nginx/vhost/location_footer.erb')
  382. ], ''),
  383. order => $priority,
  384. }
  385. }
  386. ## Only create SSL Specific locations if $ssl is true.
  387. if ($ssl == true or $ssl_only == true) {
  388. $ssl_priority = $priority + 300
  389. concat::fragment { "${vhost_sanitized}-${ssl_priority}-${location_md5}-ssl":
  390. target => $config_file,
  391. content => join([
  392. template('nginx/vhost/location_header.erb'),
  393. $content_real,
  394. template('nginx/vhost/location_footer.erb')
  395. ], ''),
  396. order => $ssl_priority,
  397. }
  398. }
  399. }