location.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 (present|absent)
  7. # [*vhost*] - Defines the default vHost for this location entry to include with
  8. # [*location*] - Specifies the URI associated with this location entry
  9. # [*www_root*] - Specifies the location on disk for files to be read from. Cannot be set in conjunction with $proxy
  10. # [*index_files*] - Default index files for NGINX to read when traversing a directory
  11. # [*proxy*] - Proxy server(s) for a location to connect to. Accepts a single value, can be used in conjunction
  12. # with nginx::resource::upstream
  13. # [*proxy_read_timeout*] - Override the default the proxy read timeout value of 90 seconds
  14. # [*ssl*] - Indicates whether to setup SSL bindings for this location.
  15. # [*ssl_only*] - Required if the SSL and normal vHost have the same port.
  16. # [*location_alias*] - Path to be used as basis for serving requests for this location
  17. # [*stub_status*] - If true it will point configure module stub_status to provide nginx stats on location
  18. # [*location_cfg_prepend*] - It expects a hash with custom directives to put before anything else inside location
  19. # [*location_cfg_append*] - It expects a hash with custom directives to put after everything else inside location
  20. # [*try_files*] - An array of file locations to try
  21. # [*option*] - Reserved for future use
  22. #
  23. # Actions:
  24. #
  25. # Requires:
  26. #
  27. # Sample Usage:
  28. # nginx::resource::location { 'test2.local-bob':
  29. # ensure => present,
  30. # www_root => '/var/www/bob',
  31. # location => '/bob',
  32. # vhost => 'test2.local',
  33. # }
  34. #
  35. # Custom config example to limit location on localhost,
  36. # create a hash with any extra custom config you want.
  37. # $my_config = {
  38. # 'access_log' => 'off',
  39. # 'allow' => '127.0.0.1',
  40. # 'deny' => 'all'
  41. # }
  42. # nginx::resource::location { 'test2.local-bob':
  43. # ensure => present,
  44. # www_root => '/var/www/bob',
  45. # location => '/bob',
  46. # vhost => 'test2.local',
  47. # location_cfg_append => $my_config,
  48. # }
  49. define nginx::resource::location (
  50. $ensure = present,
  51. $vhost = undef,
  52. $www_root = undef,
  53. $index_files = [
  54. 'index.html',
  55. 'index.htm',
  56. 'index.php'],
  57. $proxy = undef,
  58. $proxy_read_timeout = $nginx::params::nx_proxy_read_timeout,
  59. $ssl = false,
  60. $ssl_only = false,
  61. $location_alias = undef,
  62. $option = undef,
  63. $stub_status = undef,
  64. $location_cfg_prepend = undef,
  65. $location_cfg_append = undef,
  66. $try_files = undef,
  67. $location) {
  68. File {
  69. owner => 'root',
  70. group => 'root',
  71. mode => '0644',
  72. notify => Class['nginx::service'],
  73. }
  74. # # Shared Variables
  75. $ensure_real = $ensure ? {
  76. 'absent' => absent,
  77. default => file,
  78. }
  79. # Use proxy template if $proxy is defined, otherwise use directory template.
  80. if ($proxy != undef) {
  81. $content_real = template('nginx/vhost/vhost_location_proxy.erb')
  82. } elsif ($location_alias != undef) {
  83. $content_real = template('nginx/vhost/vhost_location_alias.erb')
  84. } elsif ($stub_status != undef) {
  85. $content_real = template('nginx/vhost/vhost_location_stub_status.erb')
  86. } else {
  87. $content_real = template('nginx/vhost/vhost_location_directory.erb')
  88. }
  89. # # Check for various error condtiions
  90. if ($vhost == undef) {
  91. fail('Cannot create a location reference without attaching to a virtual host')
  92. }
  93. if (($www_root == undef) and ($proxy == undef) and ($location_alias == undef) and ($stub_status == undef)) {
  94. fail('Cannot create a location reference without a www_root, proxy, location_alias or stub_status defined')
  95. }
  96. if (($www_root != undef) and ($proxy != undef)) {
  97. fail('Cannot define both directory and proxy in a virtual host')
  98. }
  99. # # Create stubs for vHost File Fragment Pattern
  100. if (!$ssl_only) {
  101. file { "${nginx::config::nx_temp_dir}/nginx.d/${vhost}-500-${name}":
  102. ensure => $ensure_real,
  103. content => $content_real,
  104. }
  105. }
  106. # # Only create SSL Specific locations if $ssl is true.
  107. if ($ssl) {
  108. file { "${nginx::config::nx_temp_dir}/nginx.d/${vhost}-800-${name}-ssl":
  109. ensure => $ensure_real,
  110. content => $content_real,
  111. }
  112. }
  113. }