From 2fcc2306ada7ae1f66c9c26fa80310a8d35a94b6 Mon Sep 17 00:00:00 2001 From: Patrick Brideau Date: Tue, 15 Sep 2015 11:58:17 -0400 Subject: [PATCH 1/2] iterate server_name when rewrite_www_to_non_www is used --- templates/vhost/vhost_header.erb | 8 +++++--- templates/vhost/vhost_ssl_header.erb | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/templates/vhost/vhost_header.erb b/templates/vhost/vhost_header.erb index 4289d1606..43d7ce0cc 100644 --- a/templates/vhost/vhost_header.erb +++ b/templates/vhost/vhost_header.erb @@ -1,4 +1,5 @@ <% if @rewrite_www_to_non_www -%> +<%- @server_name.each do |s| -%> server { <%- if @listen_ip.is_a?(Array) then -%> <%- @listen_ip.each do |ip| -%> @@ -17,10 +18,11 @@ server { listen [<%= @ipv6_listen_ip %>]:<%= @ipv6_listen_port %> <% if @ipv6_listen_options %><%= @ipv6_listen_options %><% end %>; <%- end -%> <%- end -%> - server_name www.<%= @server_name[0].gsub(/^www\./, '') %>; - return 301 http://<%= @server_name[0].gsub(/^www\./, '') %>$request_uri; + server_name www.<%= s.gsub(/^www\./, '') %>; + return 301 http://<%= s.gsub(/^www\./, '') %>$request_uri; } +<% end -%> <% end -%> server { <%- if @listen_ip.is_a?(Array) then -%> @@ -40,7 +42,7 @@ server { listen [<%= @ipv6_listen_ip %>]:<%= @ipv6_listen_port %> <% if @ipv6_listen_options %><%= @ipv6_listen_options %><% end %>; <%- end -%> <%- end -%> - server_name <%= @rewrite_www_to_non_www ? @server_name[0].gsub(/^www\./, '') : @server_name.join(" ") %>; + server_name <%= @rewrite_www_to_non_www ? @server_name.join(" ").gsub(/(^| )(www\.)?(?=[a-z0-9])/, '') : @server_name.join(" ") %>; <%- if instance_variables.any? { |iv| iv.to_s.include? 'auth_basic' } -%> <%- if defined? @auth_basic -%> auth_basic "<%= @auth_basic %>"; diff --git a/templates/vhost/vhost_ssl_header.erb b/templates/vhost/vhost_ssl_header.erb index 707618271..86db3280f 100644 --- a/templates/vhost/vhost_ssl_header.erb +++ b/templates/vhost/vhost_ssl_header.erb @@ -1,4 +1,5 @@ <% if @rewrite_www_to_non_www -%> +<%- @server_name.each do |s| -%> server { <%- if @listen_ip.is_a?(Array) then -%> <%- @listen_ip.each do |ip| -%> @@ -17,13 +18,14 @@ server { listen [<%= @ipv6_listen_ip %>]:<%= @ssl_port %> ssl<% if @spdy == 'on' %> spdy<% end %><% if @ipv6_listen_options %> <%= @ipv6_listen_options %><% end %>; <%- end -%> <%- end -%> - server_name www.<%= @server_name[0].gsub(/^www\./, '') %>; - return 301 https://<%= @server_name[0].gsub(/^www\./, '') %>$request_uri; + server_name www.<%= s.gsub(/^www\./, '') %>; + return 301 https://<%= s.gsub(/^www\./, '') %>$request_uri; <%= scope.function_template(["nginx/vhost/vhost_ssl_settings.erb"]) %> } +<% end -%> <% end -%> server { <%- if @listen_ip.is_a?(Array) then -%> @@ -43,7 +45,7 @@ server { listen [<%= @ipv6_listen_ip %>]:<%= @ssl_port %> ssl<% if @spdy == 'on' %> spdy<% end %><% if @ipv6_listen_options %> <%= @ipv6_listen_options %><% end %>; <%- end -%> <%- end -%> - server_name <%= @rewrite_www_to_non_www ? @server_name[0].gsub(/^www\./, '') : @server_name.join(" ") %>; + server_name <%= @rewrite_www_to_non_www ? @server_name.join(" ").gsub(/(^| )(www\.)?(?=[a-z0-9])/, '') : @server_name.join(" ") %>; <%= scope.function_template(["nginx/vhost/vhost_ssl_settings.erb"]) %> From 56d4c142bf8f92728253e660f831dc032ad3dcc9 Mon Sep 17 00:00:00 2001 From: Patrick Brideau Date: Tue, 15 Sep 2015 15:08:40 -0400 Subject: [PATCH 2/2] Correct spec test --- spec/defines/resource_vhost_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/defines/resource_vhost_spec.rb b/spec/defines/resource_vhost_spec.rb index 8d419f5ba..1cc0c346d 100644 --- a/spec/defines/resource_vhost_spec.rb +++ b/spec/defines/resource_vhost_spec.rb @@ -711,8 +711,8 @@ } end - it "should set the server_name of the rewrite server stanza to the first server_name with 'www.' stripped" do - is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(/^\s+server_name\s+foo.com;/) + it "should set the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do + is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(/^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;/) end end @@ -726,8 +726,8 @@ } end - it "should set the server_name of the rewrite server stanza to the first server_name with 'www.' stripped" do - is_expected.to contain_concat__fragment("#{title}-header").with_content(/^\s+server_name\s+foo.com;/) + it "should set the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do + is_expected.to contain_concat__fragment("#{title}-header").with_content(/^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;/) end end