From 8a32d8032f393c6366f823191e40603bf25eefd4 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:06:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E5=8F=8D=E5=90=91?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E7=BD=91=E7=AB=99=E5=88=9B=E5=BB=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20(#793)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/website.go | 2 +- backend/app/service/website_utils.go | 46 +++++++++++++++++-- frontend/src/lang/modules/en.ts | 29 +++++------- frontend/src/lang/modules/zh.ts | 31 ++++++------- .../config/basic/proxy/create/index.vue | 19 +++++--- 5 files changed, 84 insertions(+), 43 deletions(-) diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 69d9b88de..71acf85d1 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -1351,7 +1351,7 @@ func (w WebsiteService) UpdateAuthBasic(req request.NginxAuthUpdate) (err error) absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath) fileOp := files.NewFileOp() if !fileOp.Stat(path.Dir(absoluteAuthPath)) { - _ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 755) + _ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 0755) } if !fileOp.Stat(absoluteAuthPath) { _ = fileOp.CreateFile(absoluteAuthPath) diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index e581b4139..cce1fe97a 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -2,7 +2,9 @@ package service import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/1Panel-dev/1Panel/backend/utils/cmd" + "github.com/1Panel-dev/1Panel/backend/utils/nginx/components" "path" "strconv" "strings" @@ -84,6 +86,40 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error { return nil } +func createProxyFile(website *model.Website, runtime *model.Runtime) error { + nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + if err != nil { + return err + } + proxyFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias, "proxy") + filePath := path.Join(proxyFolder, "root.conf") + fileOp := files.NewFileOp() + if !fileOp.Stat(proxyFolder) { + if err := fileOp.CreateDir(proxyFolder, 0755); err != nil { + return err + } + } + if !fileOp.Stat(filePath) { + if err := fileOp.CreateFile(filePath); err != nil { + return err + } + } + config := parser.NewStringParser(string(nginx_conf.Proxy)).Parse() + config.FilePath = filePath + directives := config.Directives + location, ok := directives[0].(*components.Location) + if !ok { + return errors.New("error") + } + location.ChangePath("^~", "/") + location.UpdateDirective("proxy_pass", []string{website.Proxy}) + location.UpdateDirective("proxy_set_header", []string{"Host", "$host"}) + if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { + return buserr.WithErr(constant.ErrUpdateBuWebsite, err) + } + return nil +} + func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website, runtime *model.Runtime) error { nginxFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name) siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias) @@ -123,6 +159,11 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website, return err } } + if website.Type == constant.Proxy { + if err := createProxyFile(website, runtime); err != nil { + return err + } + } } return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf")) } @@ -167,9 +208,9 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a server.UpdateRootProxy([]string{proxy}) case constant.Static: server.UpdateRoot(rootIndex) - //server.UpdateRootLocation() case constant.Proxy: - server.UpdateRootProxy([]string{website.Proxy}) + nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias) + server.UpdateDirective("include", []string{nginxInclude}) case constant.Runtime: if runtime.Resource == constant.ResourceLocal { switch runtime.Type { @@ -192,7 +233,6 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { return err } - if err := opNginx(nginxInstall.ContainerName, constant.NginxCheck); err != nil { _ = deleteWebsiteFolder(nginxInstall, website) return err diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 574f5a496..fb1fa989b 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -1213,23 +1213,18 @@ const message = { startProxy: 'Start Reverse proxy', stopProxy: 'Stop the Reverse proxy', proxyFile: 'Source', - proxyHelper1: - 'Proxy directory: when accessing this directory, the content of the target URL will be returned and displayed', - proxyPassHelper: - 'Target URL: You can fill in the site you need to proxy, the target URL must be a URL that can be accessed normally, otherwise an error will be returned', - proxyHostHelper: - 'Send domain name: Add the domain name to the request header and pass it to the proxy server. The default is the target URL domain name. If it is not set properly, the proxy may not work properly', - replacementHelper: - 'Content replacement: you can add up to 3 replacement content, if you do not need to replace, please leave blank', - modifier: 'Path Match', - modifierHelper: - 'Path matching: Example: = exact match, ~ regular match, ^~ match the beginning of the path, etc', - replace: 'Content Replacement', - addReplace: 'Add content to replace', - replaced: 'The replaced text cannot be empty', - replaceText: 'Replacement text, can be empty', - replacedErr: 'The replaced text cannot be empty', - replacedErr2: 'The replaced text cannot be repeated', + proxyHelper1: 'When accessing this directory, the content of the target URL will be returned and displayed', + proxyPassHelper: 'The proxy site must be a URL that can be accessed normally', + proxyHostHelper: 'Add the domain name to the request header and pass it to the proxy server', + replacementHelper: 'Up to 5 replacements can be added, please leave blank if no replacement is required', + modifier: 'Matching rules', + modifierHelper: 'Example: = exact match, ~ regular match, ^~ match the beginning of the path, etc', + replace: 'Text Replacement', + addReplace: 'Add text replacement', + replaced: 'Search string (cannot be empty)', + replaceText: 'Replace with string', + replacedErr: 'The search string cannot be empty', + replacedErr2: 'The search string cannot be repeated', basicAuth: 'Password Access', editBasicAuthHelper: 'The password is asymmetrically encrypted and cannot be echoed. Editing needs to reset the password', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index cd70be025..21d2904ee 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -1203,32 +1203,31 @@ const message = { userGroup: '运行用户/组', user: '用户', uGroup: '用户组', - proxyPath: '代理目录', - proxyPass: '目标URL', + proxyPath: '前端请求路径', + proxyPass: '后端代理地址', cache: '缓存', status: '状态', createProxy: '创建反向代理', editProxy: '编辑反向代理', cacheTime: '缓存时间', enableCache: '开启缓存', - proxyHost: '发送域名', + proxyHost: '后端域名', disabled: '已停止', startProxy: '开启反向代理', stopProxy: '关闭反向代理', proxyFile: '源文', - proxyHelper1: '代理目录:访问这个目录时将会把目标URL的内容返回并显示', - proxyPassHelper: '目标URL:可以填写你需要代理的站点,目标URL必须为可正常访问的URL,否则将返回错误', - proxyHostHelper: - '发送域名:将域名添加到请求头传递到代理服务器,默认为目标URL域名,若设置不当可能导致代理无法正常运行', - replacementHelper: '内容替换:最多可以添加3条替换内容,如果不需要替换请留空', - modifier: '路径匹配', - modifierHelper: '路径匹配:例:= 精确匹配,~ 正则匹配,^~ 匹配路径开头 等', - replace: '内容替换', - addReplace: '添加内容替换', - replaced: '被替换的文本,不能为空', - replaceText: '替换的文本,可为空', - replacedErr: '被替换的文本不能为空', - replacedErr2: '被替换的文本不能重复', + proxyHelper1: '访问这个目录时将会把目标URL的内容返回并显示', + proxyPassHelper: '代理的站点,必须为可正常访问的URL', + proxyHostHelper: '将域名添加到请求头传递到代理服务器', + replacementHelper: '最多可以添加5条替换内容,如果不需要替换请留空', + modifier: '匹配规则', + modifierHelper: '例:= 精确匹配,~ 正则匹配,^~ 匹配路径开头 等', + replace: '文本替换', + addReplace: '添加文本替换', + replaced: '搜索字符串(不能为空)', + replaceText: '替换为字符串', + replacedErr: '搜索字符串不能为空', + replacedErr2: '搜索字符串不能重复', basicAuth: '密码访问', editBasicAuthHelper: '密码为非对称加密,无法回显,编辑需要重新设置密码', createPassword: '生成密码', 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 459b11306..f37c66b40 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 @@ -11,6 +11,9 @@ +
+ {{ $t('website.modifierHelper') }} +
@@ -36,11 +39,17 @@ +
+ {{ $t('website.proxyPassHelper') }} +
+
+ {{ $t('website.proxyHostHelper') }} +
@@ -68,16 +77,14 @@
- + {{ $t('website.addReplace') }} +
+ {{ $t('website.replacementHelper') }} +
- - - - -