This commit is contained in:
BoHung Chiu 2021-12-30 15:32:07 +08:00
parent dfa1935bd4
commit 731138f556
1 changed files with 10 additions and 9 deletions

View File

@ -106,7 +106,8 @@ class SiteConstruct
else else
port = port.to_s port = port.to_s
end end
redirect_default_text = get_redirect_default_text(port) default_domain = get_default_domain
redirect_default_text = get_redirect_default_text(port, default_domain)
port_text = port port_text = port
if port.to_i == 443 if port.to_i == 443
if self.site_cert.nil? if self.site_cert.nil?
@ -114,10 +115,13 @@ class SiteConstruct
end end
port_text += " ssl" port_text += " ssl"
end end
domain_name_str = self.domain_name
if default_domain.present?
domain_name_str = ([default_domain] + (domain_name_str.split(/\s+/) - [default_domain])).join(" ")
end
if old_server_block.present? if old_server_block.present?
new_server_block = old_server_block.gsub(/(listen\s+)[^;]+/){|ff| "#{$1}#{port_text}"} new_server_block = old_server_block.gsub(/(listen\s+)[^;]+/){|ff| "#{$1}#{port_text}"}
domain_name = self.domain_name new_server_block = new_server_block.gsub(/(server_name\s+)[^;]+/m){|ff| "#{$1}#{domain_name_str}"}
new_server_block = new_server_block.gsub(/(server_name\s+)[^;]+/m){|ff| "#{$1}#{domain_name}"}
new_server_block = new_server_block.gsub(/\s*ssl_certificate[^;]+;/,'') new_server_block = new_server_block.gsub(/\s*ssl_certificate[^;]+;/,'')
level_2_block = parse_nginx_text_to_server_blocks(old_server_block,true,2) level_2_block = parse_nginx_text_to_server_blocks(old_server_block,true,2)
get_redirect_block = level_2_block.select{|t| t.match(/\s*return\s+30[12]\s+https:\/\/\$host\$request_uri\s*;/)} get_redirect_block = level_2_block.select{|t| t.match(/\s*return\s+30[12]\s+https:\/\/\$host\$request_uri\s*;/)}
@ -167,7 +171,7 @@ class SiteConstruct
"}\n" : '') : '')+ "}\n" : '') : '')+
redirect_default_text + redirect_default_text +
' root '+self.full_site_path+'/public;\n\n'+ ' root '+self.full_site_path+'/public;\n\n'+
' server_name '+self.domain_name+';\n\n'+ ' server_name '+domain_name_str+';\n\n'+
' client_max_body_size 500m;\n\n'+ ' client_max_body_size 500m;\n\n'+
' location / {\n'+ ' location / {\n'+
' try_files \$uri \$uri/index.html \$uri.html @app;\n'+ ' try_files \$uri \$uri/index.html \$uri.html @app;\n'+
@ -186,9 +190,6 @@ class SiteConstruct
def display_port def display_port
self.port.map{|port| "<a href=\"#{get_domain_name(port)}\" title=\"#{port}\" target=\"_blank\">#{port}</a>"}.join("<br>").html_safe self.port.map{|port| "<a href=\"#{get_domain_name(port)}\" title=\"#{port}\" target=\"_blank\">#{port}</a>"}.join("<br>").html_safe
end end
def get_host_with_port(port)
self.domain_name.split().first
end
def get_port(idx=0) def get_port(idx=0)
self.port[idx] rescue "80" self.port[idx] rescue "80"
end end
@ -246,14 +247,14 @@ class SiteConstruct
def default_enable_redirect_default_domain def default_enable_redirect_default_domain
site_server.enable_redirect_default_domain rescue false site_server.enable_redirect_default_domain rescue false
end end
def get_redirect_default_text(port=80) def get_redirect_default_text(port=80,default_domain=nil)
port = port.to_i port = port.to_i
is_enable = get_enable_redirect_default_domain is_enable = get_enable_redirect_default_domain
scheme = (port == 443 ? "https" : "http") scheme = (port == 443 ? "https" : "http")
port_text = ((port == 80 || port == 443 || port.blank?) ? "" : ":#{port}") port_text = ((port == 80 || port == 443 || port.blank?) ? "" : ":#{port}")
text = "" text = ""
if is_enable if is_enable
default_domain = get_default_domain default_domain = get_default_domain if default_domain.nil?
if default_domain.present? if default_domain.present?
text = "\n\n if ($host !~* (^#{default_domain.gsub('.','\.')}$) ) {\n"+ text = "\n\n if ($host !~* (^#{default_domain.gsub('.','\.')}$) ) {\n"+
" return 302 #{scheme}://#{default_domain}#{port_text}$request_uri;\n"+ " return 302 #{scheme}://#{default_domain}#{port_text}$request_uri;\n"+