fix: 解决网站停止之后无法启动的问题 (#602)

This commit is contained in:
zhengkunwang223 2023-04-12 21:52:30 +08:00 committed by GitHub
parent 7f75ea06c2
commit f8432ba521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 72 additions and 39 deletions

View File

@ -16,6 +16,7 @@ type Website struct {
WebsiteGroupID uint `gorm:"type:integer" json:"webSiteGroupId"`
WebsiteSSLID uint `gorm:"type:integer" json:"webSiteSSLId"`
Proxy string `gorm:"type:varchar(128);not null" json:"proxy"`
ProxyType string `gorm:"type:varchar;" json:"proxyType"`
ErrorLog bool `json:"errorLog"`
AccessLog bool `json:"accessLog"`
DefaultServer bool `json:"defaultServer"`

View File

@ -166,6 +166,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
}
}
}()
var proxy string
switch create.Type {
case constant.Deployment:
@ -184,8 +185,9 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
return err
}
tx.Commit()
website.AppInstallID = install.ID
appInstall = install
website.AppInstallID = install.ID
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
} else {
var install model.AppInstall
install, err = appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID))
@ -194,6 +196,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
}
appInstall = &install
website.AppInstallID = appInstall.ID
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
}
case constant.Runtime:
runtime, err = runtimeRepo.GetFirst(commonRepo.WithByID(create.RuntimeID))
@ -226,23 +229,27 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
tx.Commit()
website.AppInstallID = install.ID
appInstall = install
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
} else {
website.ProxyType = create.ProxyType
if website.ProxyType == constant.RuntimeProxyUnix {
proxy = fmt.Sprintf("unix:%s", path.Join("/www/sites", website.Alias, "php-pool", "php-fpm.sock"))
}
if website.ProxyType == constant.RuntimeProxyTcp {
proxy = fmt.Sprintf("127.0.0.1:%d", create.Port)
}
website.Proxy = proxy
}
}
tx, ctx := helper.GetTxAndContext()
defer tx.Rollback()
if err = websiteRepo.Create(ctx, website); err != nil {
return err
}
var domains []model.WebsiteDomain
domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, WebsiteID: website.ID, Port: 80})
domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, Port: 80})
otherDomainArray := strings.Split(create.OtherDomains, "\n")
for _, domain := range otherDomainArray {
if domain == "" {
continue
}
domainModel, err := getDomain(domain, website.ID)
domainModel, err := getDomain(domain)
if err != nil {
return err
}
@ -251,12 +258,18 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
}
domains = append(domains, domainModel)
}
if len(domains) > 0 {
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
return err
}
if err != configDefaultNginx(website, domains, appInstall, runtime) {
return err
}
if err != configDefaultNginx(website, domains, appInstall, runtime, create.RuntimeConfig) {
tx, ctx := helper.GetTxAndContext()
defer tx.Rollback()
if err = websiteRepo.Create(ctx, website); err != nil {
return err
}
for i := range domains {
domains[i].WebsiteID = website.ID
}
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
return err
}
tx.Commit()

View File

@ -20,10 +20,8 @@ import (
"gorm.io/gorm"
)
func getDomain(domainStr string, websiteID uint) (model.WebsiteDomain, error) {
domain := model.WebsiteDomain{
WebsiteID: websiteID,
}
func getDomain(domainStr string) (model.WebsiteDomain, error) {
domain := model.WebsiteDomain{}
domainArray := strings.Split(domainStr, ":")
if len(domainArray) == 1 {
domain.Domain = domainArray[0]
@ -123,7 +121,7 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website,
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf"))
}
func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, appInstall *model.AppInstall, runtime *model.Runtime, runtimeConfig request.RuntimeConfig) error {
func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, appInstall *model.AppInstall, runtime *model.Runtime) error {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil {
return err
@ -171,23 +169,15 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
switch runtime.Type {
case constant.RuntimePHP:
server.UpdateRoot(rootIndex)
proxy := ""
localPath := path.Join(nginxInstall.GetPath(), rootIndex, "index.php")
if runtimeConfig.ProxyType == constant.RuntimeProxyUnix {
proxy = fmt.Sprintf("unix:%s", path.Join("/www/sites", website.Alias, "php-pool", "php-fpm.sock"))
}
if runtimeConfig.ProxyType == constant.RuntimeProxyTcp {
proxy = fmt.Sprintf("127.0.0.1:%d", runtimeConfig.Port)
}
server.UpdatePHPProxy([]string{proxy}, localPath)
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
}
}
if runtime.Resource == constant.ResourceAppstore {
switch runtime.Type {
case constant.RuntimePHP:
server.UpdateRoot(rootIndex)
proxy := fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
server.UpdatePHPProxy([]string{proxy}, "")
server.UpdatePHPProxy([]string{website.Proxy}, "")
}
}
}
@ -457,8 +447,11 @@ func opWebsite(website *model.Website, operate string) error {
}
server := servers[0]
if operate == constant.StopWeb {
if website.Type != constant.Static {
server.RemoveDirective("location", []string{"/"})
if website.Type == constant.Deployment || website.Type == constant.Static || website.Type == constant.Proxy {
server.RemoveDirective("location", []string{"", "/"})
}
if website.Type == constant.Runtime {
server.RemoveDirective("location", []string{"~", "[^/]\\.php(/|$)"})
}
server.UpdateRoot("/usr/share/nginx/html/stop")
website.Status = constant.WebStopped
@ -479,6 +472,14 @@ func opWebsite(website *model.Website, operate string) error {
case constant.Proxy:
server.RemoveDirective("root", nil)
server.UpdateRootProxy([]string{website.Proxy})
case constant.Runtime:
rootIndex := path.Join("/www/sites", website.Alias, "index")
server.UpdateRoot(rootIndex)
localPath := ""
if website.ProxyType == constant.RuntimeProxyUnix {
localPath = path.Join(nginxInstall.Install.GetPath(), rootIndex, "index.php")
}
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
}
website.Status = constant.WebRunning
now := time.Now()

View File

@ -24,6 +24,7 @@ func Init() {
migrations.AddTableRuntime,
migrations.UpdateTableApp,
migrations.UpdateTableHost,
migrations.UpdateTableWebsite,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)

View File

@ -251,7 +251,7 @@ var AddDefaultGroup = &gormigrate.Migration{
var AddTableRuntime = &gormigrate.Migration{
ID: "20230406-add-table-runtime",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.Runtime{}, &model.Website{})
return tx.AutoMigrate(&model.Runtime{})
},
}
@ -274,3 +274,10 @@ var UpdateTableHost = &gormigrate.Migration{
return nil
},
}
var UpdateTableWebsite = &gormigrate.Migration{
ID: "20230406-update-table-website",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.Website{})
},
}

View File

@ -44,6 +44,7 @@ var repeatKeys = map[string]struct {
"set": {},
"if": {},
"proxy_set_header": {},
"location": {},
}
func IsRepeatKey(key string) bool {

View File

@ -24,8 +24,8 @@ func NewLocation(directive *Directive) *Location {
location.Match = directive.Parameters[0]
return location
} else if len(directive.Parameters) == 2 {
location.Modifier = directive.Parameters[0]
location.Match = directive.Parameters[1]
location.Modifier = directive.Parameters[0]
return location
}
return nil

View File

@ -116,14 +116,22 @@ func (s *Server) RemoveDirective(key string, params []string) {
directives := s.Directives
var newDirectives []IDirective
for _, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) && len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
if key == "location" {
if location, ok := dir.(*Location); ok {
if len(params) == 2 && location.Match == params[1] && location.Modifier == params[0] {
continue
}
}
} else {
if dir.GetName() == key {
if len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
continue
}
} else {
continue
}
}
newDirectives = append(newDirectives, dir)

View File

@ -55,6 +55,7 @@ export namespace Website {
webSiteGroupId: number;
otherDomains: string;
proxy: string;
proxyType: string;
}
export interface WebSiteUpdateReq {