diff --git a/README.markdown b/README.markdown index b4de12a..f5dbc1b 100644 --- a/README.markdown +++ b/README.markdown @@ -232,12 +232,28 @@ define web::nginx_ssl_with_redirect ( fastcgi_connect_timeout => '3m', fastcgi_read_timeout => '3m', fastcgi_send_timeout => '3m' - } + }, + params => { + 'APP_ENV' => 'production', + }, } } } ``` +## Add custom fastcgi_params + +```puppet +nginx::resource::location { "some_root": + ensure => present, + location => '/some/url', + vhost => 'www.test.com', + params => { + 'APP_ENV' => 'production', + }, +} +``` + # Call class web::nginx_ssl_with_redirect ```puppet diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index 950eed4..6f0b039 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -65,6 +65,7 @@ # custom_cfg) # [*try_files*] - An array of file locations to try # [*option*] - Reserved for future use +# [*params*] - Set additional fastcgi_params # [*proxy_cache*] - This directive sets name of zone for caching. # The same zone can be used in multiple places. # [*proxy_cache_valid*] - This directive sets the time for caching @@ -107,6 +108,17 @@ # vhost => 'test2.local', # location_cfg_append => $my_config, # } +# +# Add Custom fastcgi_params +# nginx::resource::location { 'test2.local-bob': +# ensure => present, +# www_root => '/var/www/bob', +# location => '/bob', +# vhost => 'test2.local', +# params => { +# 'APP_ENV' => 'local', +# } +# } define nginx::resource::location ( $ensure = present, @@ -143,6 +155,7 @@ define nginx::resource::location ( $location_custom_cfg_prepend = undef, $location_custom_cfg_append = undef, $try_files = undef, + $params = undef, $proxy_cache = false, $proxy_cache_valid = false, $proxy_method = undef, @@ -239,6 +252,9 @@ define nginx::resource::location ( if ($try_files != undef) { validate_array($try_files) } + if ($params != undef) { + validate_hash($params) + } if ($proxy_cache != false) { validate_string($proxy_cache) } @@ -287,6 +303,9 @@ define nginx::resource::location ( if (($www_root != undef) and ($proxy != undef)) { fail('Cannot define both directory and proxy in a virtual host') } + if (($params != undef) and defined(File[$fastcgi_params])) { + fail('Cannot define both custom fastcgi_params and a fastcgi_params template') + } # Use proxy or fastcgi template if $proxy is defined, otherwise use directory template. if ($proxy != undef) { diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 019f208..99786b3 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -470,6 +470,30 @@ describe 'nginx::resource::location' do without_content(/^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+.+?;/) end end + + context "when params is {'CUSTOM_PARAM' => 'value'}" do + let :params do default_params.merge({ :params => {'CUSTOM_PARAM' => 'value'} }) end + it "should set custom fastcgi_params" do + should contain_file('/etc/nginx/fastcgi_params'). + with_content(%r|fastcgi_param\s+CUSTOM_PARAM\s+value;|) + end + it "should add comment # Enable custom fastcgi_params" do + should contain_file('/etc/nginx/fastcgi_params'). + with_content(%r|# Enable custom fastcgi_params\s+|) + end + end + + context "when params is not set" do + let :params do default_params end + it "should not set custom fastcgi_params" do + should contain_file('/etc/nginx/fastcgi_params'). + without_content(/fastcgi_param\s+CUSTOM_PARAM\s+.+?;/) + end + it "should not add comment # Enable custom fastcgi_params" do + should contain_file('/etc/nginx/fastcgi_params'). + without_content(/# Enable custom fastcgi_params\s+/) + end + end end describe "vhost_location_proxy template content" do diff --git a/templates/vhost/fastcgi_params.erb b/templates/vhost/fastcgi_params.erb index fe8bc20..cdf2baa 100644 --- a/templates/vhost/fastcgi_params.erb +++ b/templates/vhost/fastcgi_params.erb @@ -1,16 +1,16 @@ # This file managed by puppet on host <%= @fqdn %> -fastcgi_param QUERY_STRING $query_string; -fastcgi_param REQUEST_METHOD $request_method; -fastcgi_param CONTENT_TYPE $content_type; -fastcgi_param CONTENT_LENGTH $content_length; +fastcgi_param QUERY_STRING $query_string; +fastcgi_param REQUEST_METHOD $request_method; +fastcgi_param CONTENT_TYPE $content_type; +fastcgi_param CONTENT_LENGTH $content_length; -fastcgi_param SCRIPT_FILENAME $request_filename; +fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; -fastcgi_param DOCUMENT_URI $document_uri; -fastcgi_param DOCUMENT_ROOT $document_root; -fastcgi_param SERVER_PROTOCOL $server_protocol; +fastcgi_param DOCUMENT_URI $document_uri; +fastcgi_param DOCUMENT_ROOT $document_root; +fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; @@ -21,7 +21,14 @@ fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; -fastcgi_param HTTPS $https; +fastcgi_param HTTPS $https; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; + +<% if @params %> +# Enable custom fastcgi_params + <% params.each_pair do |key, val| -%> +fastcgi_param <%= key %> <%= val %>; + <% end -%> +<% end %> \ No newline at end of file diff --git a/tests/location_params.pp b/tests/location_params.pp new file mode 100644 index 0000000..1b2d3b5 --- /dev/null +++ b/tests/location_params.pp @@ -0,0 +1,12 @@ +include nginx + +nginx::resource::location { 'www.test.com-params': + ensure => present, + location => '/some/url', + vhost => 'www.test.com', + params => { + 'APP_ENV' => 'production', + 'APP_VERSION' => '0.1.10', + 'APP_SECRET' => 'hisfaihicasagfkjsa', + }, +}