location.pp 15 KB

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