diff --git a/app/models/site_construct.rb b/app/models/site_construct.rb index 781e7f3..3a82759 100644 --- a/app/models/site_construct.rb +++ b/app/models/site_construct.rb @@ -66,6 +66,7 @@ class SiteConstruct "}\n" all_ports = self.port.uniq server_blocks = [] + port_server_blocks_relation = {} if old_nginx_text.present? all_blocks = parse_nginx_text_to_server_blocks(old_nginx_text,true) server_blocks = all_blocks.select{|s| s.match(/\A[\s\r\n]*server\s*{/)} @@ -73,8 +74,20 @@ class SiteConstruct if upstream_block.present? sock_text = upstream_block + "\n" end + server_blocks.each_with_index do |server_block, i| + tmp_ports = server_block.gsub(/(^|\s+|;)listen\s+(\d+)/).map{$2} + tmp_ports.each do |port| + port_server_blocks_relation[port] = i + end + end end - nginx_text = sock_text + all_ports.map.with_index{|port,i| + all_ports.each do |port| + tmp = port_server_blocks_relation[port] + if tmp.nil? + port_server_blocks_relation[port] = 0 + end + end + nginx_text = sock_text + port_server_blocks_relation.map{|port,i| if server_blocks[i].present? generate_server_block(port,server_blocks[i]) else @@ -90,13 +103,13 @@ class SiteConstruct nginx_text_tmp = nginx_text.gsub(/({|})/m){|ff| res = ff;((ff == '{') ? (res = ff * num;num = num + 1) : (num = num - 1;res = ff * num;)); res} end_indices = match_exact_index(nginx_text_tmp,'}',level) start_indices = match_exact_index(nginx_text_tmp,'{',level) - start_indices = start_indices.map{|i| (i - nginx_text_tmp[0...i].reverse.index(/(}|;)/m)) rescue 0} + start_indices = start_indices.map.with_index{|i, j| (i - nginx_text_tmp[(j == 0 ? 0 : start_indices[j-1])...i].reverse.index(/(}|;|\n)/m)) rescue 0} all_blocks = (0...end_indices.count).map{|i| nginx_text_tmp[start_indices[i]..end_indices[i]]} all_blocks = all_blocks.map{|s| s.gsub(/[{}]+/){|ff| ff[0]}.strip} - server_blocks = all_blocks.select{|s| s.match(/\A[\s\r\n]*server\s*{/)} if get_all_blocks all_blocks else + server_blocks = all_blocks.select{|s| s.match(/\A[\s\r\n]*server\s*{/)} server_blocks end end @@ -243,7 +256,7 @@ class SiteConstruct def site_server SiteServer.where(server_name: self.server_type).first end - def get_default_domain + def get_default_domain(force_get=false) custom_default_domain_name = "" domain_names = domain_name.strip().split(" ") if default_domain_idx == -1 @@ -252,6 +265,9 @@ class SiteConstruct custom_default_domain_name = domain_names.select{|n| n.match(default_domain_name) }.first break if custom_default_domain_name.present? end + if force_get && custom_default_domain_name.blank? + custom_default_domain_name = domain_names[0] + end else custom_default_domain_name = domain_names[default_domain_idx] end