diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 3671dd97..feafefe0 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -19,7 +19,6 @@ import ( "path" "reflect" "regexp" - "strconv" "strings" "time" @@ -1164,7 +1163,7 @@ func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) switch req.Operate { case "create": _ = fileOp.DeleteFile(includePath) - case "update": + case "edit": _ = fileOp.WriteFile(includePath, bytes.NewReader(oldContent), 0755) } } @@ -1175,7 +1174,7 @@ func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) switch req.Operate { case "create": config = parser.NewStringParser(string(nginx_conf.Proxy)).Parse() - case "update": + case "edit": par, err = parser.NewParser(includePath) if err != nil { return @@ -1193,6 +1192,11 @@ func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) backPath := path.Join(includeDir, backName) _ = fileOp.Rename(includePath, backPath) return updateNginxConfig(constant.NginxScopeServer, nil, &website) + case "enable": + backName := fmt.Sprintf("%s.bak", req.Name) + backPath := path.Join(includeDir, backName) + _ = fileOp.Rename(backPath, includePath) + return updateNginxConfig(constant.NginxScopeServer, nil, &website) } config.FilePath = includePath directives := config.Directives @@ -1205,15 +1209,14 @@ func (w WebsiteService) OperateProxy(req request.WebsiteProxyConfig) (err error) location.UpdateDirective("proxy_set_header", []string{"Host", req.ProxyHost}) location.ChangePath(req.Modifier, req.Match) if req.Cache { - location.AddCache(strconv.Itoa(req.CacheTime) + req.CacheUnit) + location.AddCache(req.CacheTime, req.CacheUnit) } else { location.RemoveCache() } - if err = nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { return buserr.WithErr(constant.ErrUpdateBuWebsite, err) } - nginxInclude := path.Join("www", "sites", website.Alias, "proxy", "*.conf") + nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias) if err = updateNginxConfig(constant.NginxScopeServer, []dto.NginxParam{{Name: "include", Params: []string{nginxInclude}}}, &website); err != nil { return } @@ -1248,7 +1251,9 @@ func (w WebsiteService) GetProxies(id uint) (res []request.WebsiteProxyConfig, e config *components.Config ) for _, configFile := range fileList.Items { - proxyConfig := request.WebsiteProxyConfig{} + proxyConfig := request.WebsiteProxyConfig{ + ID: website.ID, + } parts := strings.Split(configFile.Name, ".") proxyConfig.Name = parts[0] if parts[1] == "conf" { @@ -1271,8 +1276,13 @@ func (w WebsiteService) GetProxies(id uint) (res []request.WebsiteProxyConfig, e } proxyConfig.ProxyPass = location.ProxyPass proxyConfig.Cache = location.Cache - //proxyConfig.CacheTime = location.CacheTime + if location.CacheTime > 0 { + proxyConfig.CacheTime = location.CacheTime + proxyConfig.CacheUnit = location.CacheUint + } proxyConfig.Match = location.Match + proxyConfig.Modifier = location.Modifier + proxyConfig.ProxyHost = location.Host res = append(res, proxyConfig) } return diff --git a/backend/utils/nginx/components/location.go b/backend/utils/nginx/components/location.go index 894ecaab..79e66ae9 100644 --- a/backend/utils/nginx/components/location.go +++ b/backend/utils/nginx/components/location.go @@ -1,13 +1,18 @@ package components -type Location struct { - Modifier string - Match string - Cache bool - ProxyPass string - Host string - CacheTime string +import ( + "regexp" + "strconv" +) +type Location struct { + Modifier string + Match string + Cache bool + ProxyPass string + Host string + CacheTime int + CacheUint string Comment string Directives []IDirective Line int @@ -40,7 +45,18 @@ func NewLocation(directive IDirective) *Location { dirs := dir.GetBlock().GetDirectives() for _, di := range dirs { if di.GetName() == "expires" { - location.CacheTime = di.GetParameters()[0] + re := regexp.MustCompile(`^(\d+)(\w+)$`) + matches := re.FindStringSubmatch(di.GetParameters()[0]) + if matches == nil { + continue + } + cacheTime, err := strconv.Atoi(matches[1]) + if err != nil { + continue + } + unit := matches[2] + location.CacheUint = unit + location.CacheTime = cacheTime } } } @@ -158,7 +174,7 @@ func (l *Location) ChangePath(Modifier string, Match string) { l.Match = Match } -func (l *Location) AddCache(cacheTime string) { +func (l *Location) AddCache(cacheTime int, cacheUint string) { l.RemoveDirective("add_header", []string{"Cache-Control", "no-cache"}) directives := l.GetDirectives() newDir := &Directive{ @@ -169,7 +185,7 @@ func (l *Location) AddCache(cacheTime string) { block := &Block{} block.Directives = append(block.Directives, &Directive{ Name: "expires", - Parameters: []string{cacheTime}, + Parameters: []string{strconv.Itoa(cacheTime) + cacheUint}, }) newDir.Block = block directives = append(directives, newDir) @@ -180,6 +196,7 @@ func (l *Location) AddCache(cacheTime string) { l.UpdateDirective("proxy_cache_valid", []string{"200", "304", "301", "302", "10m"}) l.Cache = true l.CacheTime = cacheTime + l.CacheUint = cacheUint } func (l *Location) RemoveCache() { @@ -191,6 +208,7 @@ func (l *Location) RemoveCache() { l.UpdateDirective("add_header", []string{"Cache-Control", "no-cache"}) - l.CacheTime = "" + l.CacheTime = 0 + l.CacheUint = "" l.Cache = false } diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 3f173935..d36056ba 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -423,9 +423,6 @@ const message = { remoteConn: 'External connection address', remoteConnHelper2: 'Use this address for non-container or external connections', localIP: 'Local IP', - userGroup: 'User/Group', - user: 'User', - uGroup: 'Group', }, container: { createContainer: 'Create container', @@ -1181,6 +1178,21 @@ const message = { 'Some programs need to specify a secondary directory as the running directory, such as ThinkPHP5, Laravel', runUserHelper: 'For websites deployed through the PHP runtime environment, all files, folder owners, and user groups under the index and subdirectories need to be set to 1000, command: chown -R 1000:1000 index', + userGroup: 'User/Group', + user: 'User', + uGroup: 'Group', + proxyPath: 'Proxy Path', + proxyPass: 'Target URL', + cache: 'Cache', + status: 'Status', + createProxy: 'Create reverse proxy', + editProxy: 'Edit reverse proxy', + cacheTime: 'Cache time', + enableCache: 'Open cache', + proxyHost: 'Send domain name', + disabled: 'Stopped', + startProxy: 'Start Reverse proxy', + stopProxy: 'Stop the Reverse proxy', }, php: { short_open_tag: 'Short tag support', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 8b85d7fa..ced583d6 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -1183,7 +1183,6 @@ const message = { userGroup: '运行用户/组', user: '用户', uGroup: '用户组', - addProxy: '添加反向代理', proxyPath: '代理目录', proxyPass: '目标URL', cache: '缓存', @@ -1194,6 +1193,8 @@ const message = { enableCache: '开启缓存', proxyHost: '发送域名', disabled: '已停止', + startProxy: '开启反向代理', + stopProxy: '关闭反向代理', }, php: { short_open_tag: '短标签支持', diff --git a/frontend/src/views/website/website/config/basic/proxy/create/index.vue b/frontend/src/views/website/website/config/basic/proxy/create/index.vue index 2b1b4aef..c6ab8944 100644 --- a/frontend/src/views/website/website/config/basic/proxy/create/index.vue +++ b/frontend/src/views/website/website/config/basic/proxy/create/index.vue @@ -1,24 +1,24 @@